Hacked together a packetizing mode for hex serial utility

This commit is contained in:
jaseg 2018-11-06 21:29:40 +09:00
parent 51e08c3c89
commit 8ee5f4ce6a

View file

@ -15,7 +15,7 @@ def _print_line(write, ts, line, width=16):
asciifmt = ''.join(chr(c) if c is not None and chr(c) in string.printable else '.' for c in line)
write(f'\033[38;5;244m{timestamp} {hexcol}{hexfmt} \033[38;5;244m|\033[92m{asciifmt}\033[38;5;244m|\033[0m', flush=True, end='')
def hexcom(write, ser, width=16):
def hexcom(write, ser, width=16, split=False):
current_line = b''
start = time.time()
while ser.is_open:
@ -24,13 +24,20 @@ def hexcom(write, ser, width=16):
data = ser.read(1) # blocking
ts = time.time()
current_line += data
write('\033[2K\r', end='')
while len(current_line) > width:
chunk, current_line = current_line[:width], current_line[width:]
_print_line(write, ts-start, chunk, width=width)
write()
_print_line(write, ts-start, current_line, width=width)
current_line += data
foo = current_line.split(b'\0') if split else [current_line]
for i, packet in enumerate(foo):
if len(foo) > 1 and i < len(foo)-1:
packet += b'\0'
while len(packet) > width:
chunk, packet = packet[:width], packet[width:]
_print_line(write, ts-start, chunk, width=width)
write()
_print_line(write, ts-start, packet, width=width)
if i < len(foo)-1:
write()
current_line = packet
if __name__ == '__main__':
import argparse
@ -40,7 +47,8 @@ if __name__ == '__main__':
parser.add_argument('serial')
parser.add_argument('baudrate')
parser.add_argument('-w', '--width', type=int, default=16, help='Number of bytes to display in one line')
parser.add_argument('-s', '--split', action='store_true', help='Split output on null bytes')
args = parser.parse_args()
ser = serial.Serial(args.serial, args.baudrate)
hexcom(print, ser, width=args.width)
hexcom(print, ser, width=args.width, split=args.split)