From 424310d957cb04bfacba9c711ee7f074fd41f962 Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 27 Nov 2020 13:38:56 +0100 Subject: demo fw WIP --- prototype/fw/tools/ser_test.py | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 prototype/fw/tools/ser_test.py (limited to 'prototype/fw/tools') diff --git a/prototype/fw/tools/ser_test.py b/prototype/fw/tools/ser_test.py new file mode 100644 index 0000000..191124b --- /dev/null +++ b/prototype/fw/tools/ser_test.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +import struct +import itertools +import binascii +import string +import serial +import time +import zlib + +from cobs import cobs + +if __name__ == '__main__': + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('port') + parser.add_argument('-b', '--baudrate', type=int, default=115200) + args = parser.parse_args() + + ser = serial.Serial(args.port, args.baudrate, timeout=0) + + byte_count = 0 + line = b'' + packet = b'' + start_time = time.time() + seq = 0 + + make_color = lambda x: f'\033[38;5;{x}m' + field_colors = [ make_color(x) for x in [ 48, 48, 48, 48, 220, 220, 220, 220, 207, 207, 207, 207 ] ] + last_tx = time.time() + while True: + data = ser.read() + for c in data: + if byte_count == 0: + print(f'\033[38;5;244m{time.time() - start_time: 8.3f} \033[0m', end='') + + col = '\033[91m' if c == 0 else '\033[0m' + print(f'{col}{c:02x}', end=' ') + line += bytes([c]) + byte_count += 1 + if c == 0: + print(' ' * (16 - byte_count), end='\033[0m\n') + byte_count = 16 + try: + payload = cobs.decode(packet) + if len(payload) > 4: + crc = zlib.crc32(payload[:-4]) + ref_crc = struct.pack('?@[\\]^_`{|}~ ' + if byte_count == 16: + printable = ''.join( chr(c) if isprint(c) else '.' for c in line ) + print(f'\033[93m | {printable}\033[0m') + byte_count = 0 + line = b'' + + + if time.time() - last_tx > 0.01: + data = struct.pack('