Make the Notebook Adapter emulator work + add package parsing

Don't know why I had committed a broken version. This one runs,
and also parses packet length when in text mode, to add appropriate
newlines.
This commit is contained in:
Simon Larsson
2020-07-03 17:35:17 +02:00
parent 8355b8ebda
commit 36b496e9cb
+21 -3
View File
@@ -19,7 +19,7 @@ at the beginning of transmission to make sure the device is reset.
"""
if len(sys.argv) != 3 and len(sys.argv) != 4:
if len(sys.argv) not in [3,4]:
print("Usage: {} <port> <log file> [bin|txt]".format(sys.argv[0]))
sys.exit(-1)
@@ -29,9 +29,13 @@ logFileName = sys.argv[2]
logText = False
if len(sys.argv) == 4:
if sys.argv[4] == txt:
if sys.argv[3] == "txt":
logText = True
# Used for rudimentary packet parsing
pastSync = 0
packetLeft = 0
with serial.Serial(serialPort, 9600, timeout=0.1) as sp:
# Used to control state of device
@@ -44,6 +48,7 @@ with serial.Serial(serialPort, 9600, timeout=0.1) as sp:
while True:
while True:
# CTS control line is used for powering device, and therefore also reset it
# Inactivated due to not available on all USB-Serial converters
if False and sp.cts == False:
if transmitState:
print("Leaving transmit state")
@@ -74,8 +79,21 @@ with serial.Serial(serialPort, 9600, timeout=0.1) as sp:
print("Received unknown byte ({}) outside of transmit state".format(inb))
else:
if inb != b'U':
pastSync = 1
if pastSync and inb != b'\xAA':
if packetLeft == 0:
logfile.write("\n".encode())
pastSync = 2
if pastSync == 2 and packetLeft == 0:
packetLeft = ord(inb)
if logText:
logfile.write(("0x{:02x} \n".format(ord(inb))).encode())
logfile.write(("0x{:02x} ".format(ord(inb))).encode())
if pastSync==2:
if packetLeft > 0:
packetLeft -= 1
else:
logfile.write(inb)
send(inb)