Fixup protocol reset problem
This commit is contained in:
parent
3f6848c4c6
commit
da3a283593
2 changed files with 45 additions and 45 deletions
|
|
@ -50,7 +50,7 @@ void usart_dma_reset() {
|
|||
usart_tx_buf.wr_idx = 0;
|
||||
usart_tx_buf.xfr_next = 0;
|
||||
usart_tx_buf.wraparound = false;
|
||||
usart_tx_buf.ack = false;
|
||||
usart_tx_buf.ack = true;
|
||||
|
||||
for (size_t i=0; i<ARRAY_LEN(usart_tx_buf.packet_end); i++)
|
||||
usart_tx_buf.packet_end[i] = -1;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
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
|
||||
|
||||
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()
|
||||
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='')
|
||||
# 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
|
||||
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 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:
|
||||
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))
|
||||
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()
|
||||
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
|
||||
except Exception as e:
|
||||
print(e, len(data))
|
||||
ser.write(ctrl_ack(0)) # FIXME delet this
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue