-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimex_notebook_adapter.py
More file actions
executable file
·91 lines (66 loc) · 1.87 KB
/
timex_notebook_adapter.py
File metadata and controls
executable file
·91 lines (66 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python3
import serial
import time
import sys
"""
On reset the device can respond to 3 commands:
* 'x': Device detection, echo this so the PC knows you're there
* '?': Device ID query, reply with "M764" and a null byte ('\0')
* 'U': Reply with 'x' and enter transmit mode
In transmit mode, all bytes should be echoed back to the PC and the
commands above should not be answered. Transmit mode is only left upon
reset.
"""
if len(sys.argv) not in [3,4]:
print("Usage: {} <port> <log file> [bin|txt]".format(sys.argv[0]))
sys.exit(-1)
serialPort = sys.argv[1]
logFileName = sys.argv[2]
logText = False
if len(sys.argv) == 4:
if sys.argv[3] == "txt":
logText = True
# Used for rudimentary packet parsing
pastSync = 0
packetLeft = -1
with serial.Serial(serialPort, 9600) as sp:
# Used to control state of device
transmitState = False
def send(data):
sp.write(data)
while True:
inb = sp.read(1)
if len(inb) == 0:
break
# print("CTS: {}\tDSR: {}\tRI: {}\tCD: {}".format(sp.cts,sp.dsr,sp.ri,sp.cd))
if not transmitState:
if inb == b"x":
print("Received detection byte ('x')")
send(inb)
elif inb == b"?":
print("Received device ID query ('?')")
send("?M764\0".encode())
elif inb == b"U":
print("Received sync byte, entering transmit state")
transmitState = True
logfile = open(logFileName, 'wb')
send(inb)
else:
print("Received unknown byte ({}) outside of transmit state".format(inb))
else:
if pastSync == 0 and inb != b'U':
pastSync = 1
logfile.write("\n".encode())
if pastSync == 1 and inb != b'\xAA':
pastSync = 2
packetLeft = 0
if pastSync == 2 and packetLeft == 0:
packetLeft = ord(inb)
logfile.write("\n".encode())
if logText:
logfile.write(("0x{:02x} ".format(ord(inb))).encode())
if packetLeft > 0:
packetLeft -= 1
else:
logfile.write(inb)
send(inb)