diff options
author | jaseg <git@jaseg.net> | 2020-01-30 16:45:11 +0100 |
---|---|---|
committer | jaseg <git@jaseg.net> | 2020-01-30 16:45:11 +0100 |
commit | da3a2835935173173da8843ecc4800961856ca44 (patch) | |
tree | 130392f434145c3a757e7d61bfa9136c1aa6bd80 /gm_platform/fw/tw_test.py | |
parent | 3f6848c4c68c137d8e3202af623d676a66b4bc53 (diff) | |
download | master-thesis-da3a2835935173173da8843ecc4800961856ca44.tar.gz master-thesis-da3a2835935173173da8843ecc4800961856ca44.tar.bz2 master-thesis-da3a2835935173173da8843ecc4800961856ca44.zip |
Fixup protocol reset problem
Diffstat (limited to 'gm_platform/fw/tw_test.py')
-rw-r--r-- | gm_platform/fw/tw_test.py | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/gm_platform/fw/tw_test.py b/gm_platform/fw/tw_test.py index a05a44b..cde59f0 100644 --- a/gm_platform/fw/tw_test.py +++ b/gm_platform/fw/tw_test.py @@ -74,56 +74,56 @@ if __name__ == '__main__': while True: #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 - - try: - if len(data) <= 1: # delimiting zero for retransmission - cur.execute('INSERT INTO errors(run_id, rx_ts, type) VALUES (?, ?, "retransmission")', - (run_id, int(time()*1000))) - continue - crc32, payload = unpack_head('I', cobs.decode(data[:-1])) - pid, seq, data = unpack_head('xBH', payload) - ts = time() - - # Calculate byte-wise CRC32 - our_crc = zlib.crc32(bytes(b for x in payload for b in (0, 0, 0, x))) - #log.append((time(), seq, crc32, our_crc, pid, data)) - print(f'{ts:>7.3f} {seq:05d} {crc32:08x} {our_crc:08x} {pid} {hexlify(data).decode()}', end='') - - error = False - suppress_ack = False - if crc32 != our_crc: - print(' CRC ERROR', end='') - suppress_ack = True - error = True - - if last_pid is not None and pid != (last_pid+1)%8: - print(' PID ERROR', end='') - error = True - else: - last_pid = pid - - if not suppress_ack: - ser.write(ctrl_ack(pid)) - ser.flushOutput() - - if not error: - cur.execute('INSERT INTO measurements VALUES (?, ?, ?, ?)', (run_id, int(ts*1000), seq, data)) - else: - cur.execute('INSERT INTO errors VALUES (?, ?, "pid", ?, ?, ?, ?, ?, ?)', - (run_id, int(ts*1000), seq, pid, (last_pid+1)%8, crc32, our_crc, data)) - - print() - lines_written += 1 - if lines_written == 80: - lines_written = 0 - print('\033[2J\033[H', end='') - db.commit() - - except Exception as e: - print(e, len(data)) - ser.write(ctrl_ack(0)) # FIXME delet this + for data in data.split(b'\0')[:-1]: # data always ends on \0 due to read_until, so split off the trailing empty bytes() + try: + if not data: + #print(f'{time():>7.3f} Timeout: resetting') + #ser.write(cobs.encode(b'\x01\xff') + b'\0') # reset + ser.write(ctrl_ack(0)) # FIXME delet this + cur.execute('INSERT INTO errors(run_id, rx_ts, type) VALUES (?, ?, "retransmission")', + (run_id, int(time()*1000))) + continue + + crc32, payload = unpack_head('I', cobs.decode(data)) + pid, seq, data = unpack_head('xBH', payload) + ts = time() + + # Calculate byte-wise CRC32 + our_crc = zlib.crc32(bytes(b for x in payload for b in (0, 0, 0, x))) + #log.append((time(), seq, crc32, our_crc, pid, data)) + print(f'{ts:>7.3f} {seq:05d} {crc32:08x} {our_crc:08x} {pid} {hexlify(data).decode()}', end='') + + error = False + suppress_ack = False + if crc32 != our_crc: + print(' CRC ERROR', end='') + suppress_ack = True + error = True + + if last_pid is not None and pid != (last_pid+1)%8: + print(' PID ERROR', end='') + error = True + else: + last_pid = pid + + if not suppress_ack: + ser.write(ctrl_ack(pid)) + ser.flushOutput() + + if not suppress_ack: + cur.execute('INSERT INTO measurements VALUES (?, ?, ?, ?)', (run_id, int(ts*1000), seq, data)) + if error: + cur.execute('INSERT INTO errors VALUES (?, ?, "pid", ?, ?, ?, ?, ?, ?)', + (run_id, int(ts*1000), seq, pid, (last_pid+1)%8, crc32, our_crc, data)) + + print() + lines_written += 1 + if lines_written == 80: + lines_written = 0 + print('\033[2J\033[H', end='') + db.commit() + + except Exception as e: + print(e, len(data)) + ser.write(ctrl_ack(0)) # FIXME delet this |