55 lines
1.2 KiB
Python
55 lines
1.2 KiB
Python
#-------------------------------------------------------------------------------
|
|
# simulator
|
|
# 05/07/2022
|
|
# tool script to print everything seen on a serial link as hex with a timestamp
|
|
#
|
|
# usage ex: python sniffer_hex
|
|
#-------------------------------------------------------------------------------
|
|
|
|
import serial
|
|
import sys
|
|
import time
|
|
|
|
ports = {"obs1": "/dev/ttyS20"}
|
|
|
|
def main():
|
|
|
|
port = ports["obs1"]
|
|
speed = 9600
|
|
|
|
# parse argv
|
|
args = sys.argv
|
|
if len(args) == 1:
|
|
pass
|
|
elif len(args) == 2:
|
|
if args[1] in ports:
|
|
port = ports[args[1]]
|
|
else:
|
|
print("unknown port")
|
|
return 1
|
|
elif len(args) == 3:
|
|
speed = int(args[1])
|
|
else:
|
|
print("too many arguments")
|
|
return 1
|
|
|
|
# open serial and identify board
|
|
ser = serial.Serial(port, speed, timeout=0.1)
|
|
print("openned port", ser.name)
|
|
while (True):
|
|
raw_msg = ser.read(size=100)
|
|
if len(raw_msg) != 0:
|
|
print(timestamp() + raw_msg.hex())
|
|
return 0
|
|
|
|
def timestamp():
|
|
timestamp = time.strftime("[%H:%M:%S.", time.localtime())
|
|
timestamp += str(int((time.time_ns() / 1e6) % 1000)).rjust(3, '0')
|
|
timestamp += "] "
|
|
return timestamp
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|