diff options
author | jaseg <git@jaseg.net> | 2020-01-29 23:53:24 +0100 |
---|---|---|
committer | jaseg <git@jaseg.net> | 2020-01-29 23:53:34 +0100 |
commit | b77815173460f25bc37a5fac191e50ef8fd14017 (patch) | |
tree | 4e21fa71fc8874f4472c9bed73de91eb33d9ffa4 /gm_platform/fw/tw_test.py | |
parent | e6e49ee4818cd05be08551b6d07b1fd14ed28d78 (diff) | |
download | master-thesis-b77815173460f25bc37a5fac191e50ef8fd14017.tar.gz master-thesis-b77815173460f25bc37a5fac191e50ef8fd14017.tar.bz2 master-thesis-b77815173460f25bc37a5fac191e50ef8fd14017.zip |
test working
Diffstat (limited to 'gm_platform/fw/tw_test.py')
-rw-r--r-- | gm_platform/fw/tw_test.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/gm_platform/fw/tw_test.py b/gm_platform/fw/tw_test.py new file mode 100644 index 0000000..0380f95 --- /dev/null +++ b/gm_platform/fw/tw_test.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +from time import time +from binascii import hexlify +import enum +import struct +import zlib +import sys + +import serial +from cobs import cobs + + +class CtrlPacketTypes(enum.Enum): + RESET = 1 + ACK = 2 + RETRANSMIT = 3 + +def unpack_head(fmt, data): + split = struct.calcsize(fmt) + return *struct.unpack(fmt, data[:split]), data[split:] + +def ctrl_packet(ptype, pid=0): + return cobs.encode(struct.pack('BB', ptype.value, pid)) + b'\0' + +ctrl_reset = lambda: ctrl_packet(CtrlPacketTypes.RESET) +ctrl_ack = lambda pid: ctrl_packet(CtrlPacketTypes.ACK, pid) +ctrl_retransmit = lambda pid: ctrl_packet(CtrlPacketTypes.RETRANSMIT, pid) + + +ser = serial.Serial('/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0', 250000, timeout=1.0) +ser.write(b'foobar'*32) +sys.exit(0) + +log = [] +ser.flushInput() +ser.write(ctrl_reset()) +ser.flushOutput() +for _ in range(100): + #ser.write(cobs.encode(b'\x01\xff') + b'\0') + data = ser.read_until(b'\0') + if not data or data[-1] != 0x00: + #print(f'{time():>7.3f} Timeout: resetting') + #ser.write(cobs.encode(b'\x01\xff') + b'\0') # reset + continue + + crc32, payload = unpack_head('I', cobs.decode(data[:-1])) + pid, seq, data = unpack_head('xBH', payload) + ser.write(ctrl_ack(pid)) + ser.flushOutput() + + # Calculate byte-wise CRC32 + #our_crc = zlib.crc32(bytes(b for x in payload for b in (0, 0, 0, x))) + our_crc = 0 + #log.append((time(), seq, crc32, our_crc, pid, data)) + +for time, seq, crc32, our_crc, pid, data in log: + print(f'{time:>7.3f} {seq:05d} {crc32:08x} {our_crc:08x} {pid} {hexlify(data).decode()}') |