-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpSerial.py
478 lines (435 loc) · 11.3 KB
/
rpSerial.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env python
import zlib
import time
import serial
import threading
import Queue
import sys
import serial
'''
Raspberry Pi Serial module
Useful sites:
https://sites.google.com/site/semilleroadt/raspberry-pi-tutorials/gpio
disable the process on the gpio pin
'''
# Open Serial Communication
# Send (0,0) coordinate,
# Wait for confimation of recieve
# Wait for ready payload
# Start Pulling from Queue Loop
# ...
# Verify Queue is done (check to see that neither
# rasterQ nor edQ threads are running)
# Close Serail Comm
# PI -> MSP430
# Startx: 0x02
# Command: 0x0B
# Escape Char if 03 occurs in payload or
# Payload 0xNNNNNNNN
# Checksum 0xNN
# EndX: 0x03
# Recieve MSP430 -> PI
# Start 0x02
# Acknow 0x06
# Command 0x0B
# ETx 0x03
# Ready for more MSP430 -> PI
# Start 0x02
# SendM 0x1B
# End 0x03
# Pi will respond PI -> MSP430
# Start 0x02
# Command 0x1B
# End 0x03
init = 0x01
startX = 0x02
endX = 0x03
acknow = 0x06
burn = 0x0B
emerg = 0x0d
endIm = 0x0F
startIm = 0x11
esc = 0x1B
error = 0x3f
readyB = 0x4d
startXc = "0x02"
endXc = "0x03"
acknowc = "0x06"
burnc = "0x0B"
escc = "0x1B"
errorc = "0x3f"
readyBc = "0x4d"
emergc = "0x0d"
def hexParse(rawMsg):
# Parsing
tempMsg = rawMsg.upper()
tempMsg = tempMsg.split('0X')
tempMsg = "".join(tempMsg)
tempMsg = tempMsg.split('\X')
tempMsg = "".join(tempMsg)
hexMsg = 0x00
if(len(tempMsg) > 2):
if ((tempMsg[0] == "0") and (tempMsg[1] == "X")):
tempMsg = tempMsg[2:]
hexVals = set('0123456789ABCDEF')
tempMsg = "".join(c for c in tempMsg if c in hexVals)
# Varibles used
hexMsg = ""
byteHold = 0x00
byteArr = []
# Start with most sig byte
sigBytePoint = len(tempMsg)% 2
if (sigBytePoint == 1):
# Pack the first Byte
if (tempMsg[0] == '0'):
byteHold = byteHold << 4
byteHold = byteHold | 0x00
elif (tempMsg[0] == '1'):
byteHold = byteHold << 4
byteHold = byteHold | 0x01
elif (tempMsg[0] == '2'):
byteHold = byteHold << 4
byteHold = byteHold | 0x02
elif (tempMsg[0] == '3'):
byteHold = byteHold << 4
byteHold = byteHold | 0x03
elif (tempMsg[0] == '4'):
byteHold = byteHold << 4
byteHold = byteHold | 0x04
elif (tempMsg[0] == '5'):
byteHold = byteHold << 4
byteHold = byteHold | 0x05
elif (tempMsg[0] == '6'):
byteHold = byteHold << 4
byteHold = byteHold | 0x06
elif (tempMsg[0] == '7'):
byteHold = byteHold << 4
byteHold = byteHold | 0x07
elif (tempMsg[0] == '8'):
byteHold = byteHold << 4
byteHold = byteHold | 0x08
elif (tempMsg[0] == '9'):
byteHold = byteHold << 4
byteHold = byteHold | 0x09
elif (tempMsg[0] == 'A'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0A
elif (tempMsg[0] == 'B'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0B
elif (tempMsg[0] == 'C'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0C
elif (tempMsg[0] == 'D'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0D
elif (tempMsg[0] == 'E'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0E
elif (tempMsg[0] == 'F'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0F
byteArr.append(byteHold)
# Rest of sting/bytes
byteHold = 0x00
action = 0
for i in range(sigBytePoint, len(tempMsg)):
if (tempMsg[i] == '0'):
byteHold = byteHold << 4
byteHold = byteHold | 0x00
elif (tempMsg[i] == '1'):
byteHold = byteHold << 4
byteHold = byteHold | 0x01
elif (tempMsg[i] == '2'):
byteHold = byteHold << 4
byteHold = byteHold | 0x02
elif (tempMsg[i] == '3'):
byteHold = byteHold << 4
byteHold = byteHold | 0x03
elif (tempMsg[i] == '4'):
byteHold = byteHold << 4
byteHold = byteHold | 0x04
elif (tempMsg[i] == '5'):
byteHold = byteHold << 4
byteHold = byteHold | 0x05
elif (tempMsg[i] == '6'):
byteHold = byteHold << 4
byteHold = byteHold | 0x06
elif (tempMsg[i] == '7'):
byteHold = byteHold << 4
byteHold = byteHold | 0x07
elif (tempMsg[i] == '8'):
byteHold = byteHold << 4
byteHold = byteHold | 0x08
elif (tempMsg[i] == '9'):
byteHold = byteHold << 4
byteHold = byteHold | 0x09
elif (tempMsg[i] == 'A'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0A
elif (tempMsg[i] == 'B'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0B
elif (tempMsg[i] == 'C'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0C
elif (tempMsg[i] == 'D'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0D
elif (tempMsg[i] == 'E'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0E
elif (tempMsg[i] == 'F'):
byteHold = byteHold << 4
byteHold = byteHold | 0x0F
action += 1
if (action == 2):
byteArr.append(byteHold)
byteHold = 0x00
action = 0
hexMsg = "".join(chr(x) for x in byteArr)
#print hexMsg
return hexMsg
def sendX(ser, messages):
#messages = hexParse(str(messages))
#print str(messages)
try:
ser.write(str(messages))
except:
print "Failed to send\t", messages, "\trpSerial.sendX"
return 1
return 0
def receiveX(ser, expectations):
i = 0
msgD = {}
msgArray = []
anything = False
while True:
try:
msg = ser.read()
except serial.serialutil.SerialException:
msg = ''
if (msg == '') :
i +=1
if (i >= 10):
break
else :
hexM = ":".join("{:02x}".format(ord(c)) for c in str(msg))
msgArray.append(hexM)
#print "In: \t", (str(msg))
msgD[msg] = 1
i = 0
anything = True
if (anything == False):
return 2
for j in range(len(expectations)):
if expectations[j] not in msgD:
#print msgArray
return 1
#print "MSG ARR", msgArray
return 0
def checkSum8(payload):
specialChar = [startX, endX, esc]
byte4 = (payload >> 24) & 0xFF
byte3 = (payload >> 16) & 0xFF
byte2 = (payload >> 8) & 0xFF
byte1 = payload & 0xFF
msgA = []
payloadEscaped = 0x00
#print hex(payload)
if (byte4 in specialChar):
msgA.append(esc)
msgA.append(byte4)
if (byte3 in specialChar):
msgA.append(esc)
msgA.append(byte3)
if (byte2 in specialChar):
msgA.append(esc)
msgA.append(byte2)
if (byte1 in specialChar):
msgA.append(esc)
msgA.append(byte1)
#for i in range(len(msgA)):
#payloadEscaped = (payloadEscaped << 8) | msgA[i]
checkSum = 256 - ((byte4 + byte3 + byte2 + byte1) & 0x00FF)
if (checkSum in specialChar):
msgA.append(esc)
checkSum = (esc << 8) | checkSum
msgA.append(checkSum & 0xff)
#print hex(payload), hex(checkSum), hex(byte4),hex(byte3),hex(byte2),hex(byte1)
# I hate doing this, it's ugly, but it will get this to work
#for i in range(len(msgA))
return msgA
def sendPix(ser, payload):
checksum = 0x00
#checksum32 = zlib.adler32(str(payload))
#checksum = checksum | (checksum32 & 0x000000FF)
payloadAr = checkSum8(payload)
#print payload
sendX(ser, chr(startX))
sendX(ser, chr(burn))
hexMsg = "".join(chr(x) for x in payloadAr)
sendX(ser, hexMsg)
sendX(ser, chr(endX))
#reciv = receiveX(ser, [startX, acknow, burn, endX] )
#print "Recieved\t", reciv
return
def logicFlow(ser, payload):
maxWait = 5
startTime = time.time()
anything = False
print "Payload"
waited = 0
while True:
if (waited < 1):
sendPix(ser, payload)
waited += 1
#print payload
#print (time.time() - startTime)
reciv = receiveX(ser, [chr(startX), chr(acknow), chr(burn), chr(endX), chr(readyB)] )
if reciv == 0:
break
if (reciv != 2):
#print reciv
anything = True
if ((time.time() - startTime) > maxWait):
if (anything == False):
print "Communication Lost"
return 1
#sys.exit()
break
# in the last five seconds we got something, try again
anything = False
startTime = time.time()
startTime = time.time()
print "waiting"
while True:
reciv = receiveX(ser, [chr(startX), chr(readyB), chr(endX)] )
#reciv = 0
if reciv == 0:
break
if (reciv != 2):
anything = True
if ((time.time() - startTime) > maxWait):
if (anything == False):
print "Communication Lost"
return 1
#sys.exit()
break
# in the last five seconds we got something, try again
anything = False
startTime = time.time()
sendX(ser, chr(startX))
sendX(ser, chr(acknow))
sendX(ser, chr(readyB))
sendX(ser, chr(endX))
return 0
def phase2(ser):
maxWait = 5
reciv = 2 # This is the return val for no serial response
startTime = time.time()
while reciv == 2:
reciv = receiveX(ser, [chr(startX), chr(acknow), chr(burn), chr(endX)] )
#if ((time.time() - startTime) > maxWait):
# I've waited for 5 seconds, I give up
#return 2
if reciv == 1:
# I recieved something, but it wasn't what I wanted
return 1
if reciv == 0:
return 0
else:
return 2
def phase3(ser):
maxWait = 5
reciv = 2 # This is the return val for no serial response
startTime = time.time()
while reciv == 2:
reciv = receiveX(ser, [chr(startX), chr(endX), chr(readyB)] )
#if ((time.time() - startTime) > maxWait):
# I've waited for 5 seconds, I give up
#return 2
if reciv == 1:
# I recieved something, but it wasn't what I wanted
return 1
if reciv == 0:
return 0
else:
return 2
def logicFlow2(ser, payload):
maxWait = 20
startTime = time.time()
p2 = 1
while p2 == 1:
# Phase 1 0x02PAYCHECK03 Pi->MSP
sendPix(ser, payload)
# Phase 2 0x02060b03 MSP->Pi
p2 = phase2(ser)
startTime = time.time()
if ((time.time() - startTime) > maxWait):
print "P2 took too long"
#break
if p2 == 2:
print "Communication Lost"
return 1
# Phase 3 0x024d03 MSP<-Pi
p3 = 1
while p3 == 1:
p3 = phase3(ser)
startTime = time.time()
if ((time.time() - startTime) > maxWait):
print "P2 took too long"
#break
if p3 == 2:
print "Communication Lost"
return 1
# Phase 4: 0x02064d03 Pi->MSP
sendX(ser, chr(startX))
sendX(ser, chr(acknow))
sendX(ser, chr(readyB))
sendX(ser, chr(endX))
return 0
def coolDown(oldepoch, runTime, sleepTime):
if time.time() - oldepoch > 60*runTime:
oldepoch = time.time()
print "**COOLING DOWN**"
while True:
if time.time() - oldepoch > 60*sleepTime:
break
time.sleep(5)
print int((time.time()-oldepoch)/60), ":",int((time.time()-oldepoch)%60), " Minutes left"
oldepoch = time.time()
return oldepoch
def rpSerialManager(q, ser):
pixCount = 0
i = 0
#connected = False
#while not connected:
#serin = ser.read()
#connected = True
receiveX(ser, [chr(startX), chr(error), chr(endX)])
#ser.write("HSDF")
# Run for n minutes, sleep for m minutes
oldepoch = time.time()
sleepTime = 5
runTime = 5
#ser.write("HEN")
while (i < 5):
time.sleep(0.1)
i += 1
while not q.empty():
payload = q.get()
check = logicFlow2(ser, payload)
#oldepoch = coolDown(oldepoch, runTime, sleepTime)
if check == 1:
# Communictation lost, return error code 1: Comm Lost
return 1
pixCount += 1
q.task_done()
print pixCount, q.qsize(), q.empty()
i = 0
print "********************* **********"
print "DONE :P ", pixCount," ", pixCount/1200
# for testing mode
#sys.exit('s')
return 0