-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_talk.py
248 lines (226 loc) · 7.99 KB
/
serial_talk.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import serial
import queue
import time
import memoryParse
import sys
import AM_properties
def read_serial():
ser = serial.Serial(AM_properties.serialPort , AM_properties.baudRate, timeout=1, writeTimeout=0) #ensure non-blocking
q = queue.LifoQueue(maxsize=45)
q.put("getid,3#")
q.put("rd,3,0x000FFFF6#")
q.put("Read SNUM 3#")
q.put("rd,3,0x000FFFEE#")
q.put("Read DATE 3#")
q.put("rd,3,0x000FFFE6#")
q.put("Read MAX 3#")
q.put("status2,3#")
q.put("status1,3#")
q.put("testread,3#")
q.put("debug#")
recent_milis = int(time.time())
timeout = 3
apt = {
"snum AM":"",
"maximum AM":0,
"current AM":0,
"date": int(time.time())}
while True:
data = ser.readline()[:-2] #the last bit gets rid of the new-line chars
if not q.empty() and (time.time() - recent_milis > timeout):
send_data = q.get()
print(send_data)
ser.write(send_data.encode('ascii'))
recent_milis = int(time.time())
if data:
try:
data_r = data.decode('ascii')
if data_r.startswith('SNUM'):
apt['snum AM'] = int(data_r.split(' ')[1],16)
elif data_r.startswith('DATE'):
apt['date'] = int(data_r.split(' ')[1])
elif data_r.startswith('MAXI'):
apt['maximum AM'] = int(data_r.split(' ')[1])
elif data_r.startswith('cs[3] read'):
parsed = data_r.split(' ')
try:
if int(parsed[4],16) == int(AM_properties.snum_adress,16):
apt['snum AM'] = int(parsed[2].rstrip(':'),16)
except IndexError:
pass
except ValueError:
if int(parsed[5],16) == int(AM_properties.snum_adress,16):
apt['snum AM'] = int(parsed[2].rstrip(':'),16)
if not "VMDP" in data_r:
print(data_r)
except UnicodeDecodeError:
print(data)
if q.empty() and (time.time() - recent_milis > timeout):
break
ser.close()
return apt
def write_serial(apt_struct, erase):
ser = serial.Serial(AM_properties.serialPort , AM_properties.baudRate, timeout=1, writeTimeout=0) #ensure non-blocking
q = queue.LifoQueue(maxsize=50)
q.put("!!!WRITE COMPLETE!!!#")
q.put("snum,3,{}#".format(apt_struct["snum AM"]))
q.put("maxi,3,{}#".format(apt_struct["maximum AM"]))
q.put("date,3,{}#".format(apt_struct["date"]))
if erase:
q.put("scan,3,0#")
q.put("scan,3,0#")
q.put("scan,3,0#")
for j in range(10):
q.put("NOP#")
q.put("nuke,3#")
q.put("set registers readonly#")
q.put("status2,3#")
q.put("status1,3#")
q.put("statusw,3,8001#")
q.put("clear registers#")
q.put("status2,3#")
q.put("status1,3#")
q.put("statusw,3,0000#")
q.put("status2,3#")
q.put("status1,3#")
q.put("testread,3#")
q.put("debug#")
recent_milis = int(time.time())
timeout = 3
while True:
data = ser.readline()[:-2] #the last bit gets rid of the new-line chars
if not q.empty() and (time.time() - recent_milis > timeout):
send_data = q.get()
print(send_data)
ser.write(send_data.encode('ascii'))
recent_milis = int(time.time())
if data:
try:
data_r = data.decode('ascii')
if not "VMDP" in data_r:
print(data_r)
except UnicodeDecodeError:
print(data)
if q.empty() and (time.time() - recent_milis > timeout):
break
# print(time.time() - int(recent_milis))
ser.close()
def read_all_mem():
ser = serial.Serial(AM_properties.serialPort , AM_properties.baudRate, timeout=1, writeTimeout=0) #ensure non-blocking
q = queue.LifoQueue(maxsize=5)
q.put("dump#")
recent_milis = int(time.time())
timeout = 1
q.put("debug#")
send_data = q.get()
print(send_data)
ser.write(send_data.encode('ascii'))
data = ser.readline()
time.sleep(3)
f = open("memory.txt", "w")
log_ = open("mem_log.txt", "w")
try:
while True:
data = ser.readline()[:-2] #the last bit gets rid of the new-line chars
if data:
try:
data_r = data.decode('ascii')
if data_r.startswith('0x'):
f.write(data_r)
f.write('\n')
if data_r == "cs[dump] Done!":
break
if not "VMDP" in data_r:
print(data_r)
log_.write(data_r)
except UnicodeDecodeError:
print(data)
elif not q.empty() and (time.time() - recent_milis > timeout):
send_data = q.get()
print(send_data)
ser.write(send_data.encode('ascii'))
recent_milis = int(time.time())
elif q.empty() and (time.time() - recent_milis > 1200000):
break
# print(time.time() - int(recent_milis))
ser.close()
f.close()
log_.close()
memoryParse.translate_tobin()
except KeyboardInterrupt:
f.close()
log_.close()
memoryParse.translate_tobin()
def find_offset():
ser = serial.Serial(AM_properties.serialPort , AM_properties.baudRate, timeout=1, writeTimeout=0) #ensure non-blocking
q = queue.LifoQueue(maxsize=20)
"""
apt = {"snum MCU":"",
"snum APTx":"",
"snum AM":"",
"maximum AM":0,
"current AM":0,
"init done":True,
"date": int(time.time())}
"""
q.put("!!!READ COMPLETE!!!#")
q.put("find,3#")
q.put("debug#")
recent_milis = int(time.time())
timeout = 3
offset = {"1":-1, "2":-1, "3":-1}
while True:
data = ser.readline()[:-2] #the last bit gets rid of the new-line chars
if not q.empty() and (time.time() - recent_milis > timeout):
send_data = q.get()
print(send_data)
ser.write(send_data.encode('ascii'))
recent_milis = int(time.time())
if data:
try:
data_r = data.decode('ascii')
if data_r.startswith("pulses "):
parsed = data_r.split(' ')
# "FIND %d %lu time offset %d"
print("pulses is {}".format(parsed[2]))
offset["2"] = parsed[2]
if not "VMDP" in data_r:
print(data_r)
except UnicodeDecodeError:
print(data)
if q.empty() and (time.time() - recent_milis > timeout):
print(offset)
ser.close()
try:
temp = int(offset["2"])
return temp
except TypeError:
return -1
def enumerate_ports():
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
if __name__ == "__main__":
COMPORT = enumerate_ports()[-1]
print(read_serial())