forked from leonardean/dex-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DexOperator.py
565 lines (547 loc) · 21.6 KB
/
DexOperator.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import dexcrc16
import serial
from time import sleep
SOH = chr(0x01)
STX = chr(0x02)
ETX = chr(0x03)
EOT = chr(0x04)
DLE = chr(0x10)
ENQ = chr(0x05)
NAK = chr(0x15)
ETB = chr(0x17)
ACK0 = '0'
ACK1 = '1'
BLOCK_SIZE = 245
def printReceivedData(data):
#print "Received data:",data,"=",data.encode('hex')
return True
def chunkstring(string, length):
return (string[0+i:length+i] for i in range(0, len(string), length))
def printLog(content):
print (content)
class MasterReader:
def __init__(self, serialPath, communicationID):
self.serialPath = serialPath
self.communicationID = communicationID
self.ser = serial.Serial(self.serialPath, 9600, timeout=0.01)
self.content = ""
def read(self):
handshaker = Handshaker(self.ser, self.communicationID)
firstHandshakeDCMasterResult = handshaker.firstHandshakeDCMaster()
if firstHandshakeDCMasterResult == False:
return False
secondHandshakeVMDMasterResult = handshaker.secondHandshakeVMDMaster()
if secondHandshakeVMDMasterResult == False:
return False
self.content = handshaker.VMD2DCExchange()
self.ser.close()
return self.content
# class SlaveReader:
class MasterWriter:
def __init__(self, serialPath, communicationID):
self.serialPath = serialPath
self.communicationID = communicationID
self.ser = serial.Serial(self.serialPath, 9600, timeout=0.01)
def write(self, content):
handshaker = Handshaker(self.ser, self.communicationID)
dataExchanger = DataExchanger(self.ser)
firstHandshakeDCMasterResult = handshaker.firstHandshakeDCMaster()
if firstHandshakeDCMasterResult == False:
return False
secondHandshakeVMDMasterResult = handshaker.secondHandshakeVMDMaster()
if secondHandshakeVMDMasterResult == False:
return False
dataExchanger.DC2VMDExchange(content)
self.ser.close()
return True
class Handshaker:
def __init__(self, ser, communicationID):
self.ser = ser
self.communicationID = communicationID
self.content = ""
def firstHandshakeDCMaster(self):
print "Entering First Handshake DC as Master"
state = 0
retries = 5
self.ser.flushInput()
self.ser.write(ENQ)
self.ser.flush()
while retries > 0:
x = self.ser.read()
if len(x) > 0:
printReceivedData(x)
retries = 5
if state == 0:
print "State 0: Expecting DLE"
if x == DLE:
print "Got DLE"
state = 1
elif x == ENQ:
print "Got ENQ. TODO: slave handshake"
# start slaveHandshake
sleep(0.01)
self.ser.write(NAK)
self.ser.flush()
else:
print "Got something else. Sending ENQ to restart master handshake"
sleep(0.01)
self.ser.write(ENQ)
self.ser.flush()
elif state == 1:
print "State 1: Expecting '0'"
if x == '0':
print "Got second half of DLE"
print "Sending DLE"
sleep(0.01)
self.ser.write(DLE)
#print "Sending SOH"
self.ser.write(SOH)
# print "Sending communication ID"
for char in self.communicationID:
self.ser.write(char)
#print "Sending DLE"
self.ser.write(DLE)
#print "Sending ETX"
self.ser.write(ETX)
print self.communicationID
crc = dexcrc16.crcStr(self.communicationID + ETX)
print crc
self.ser.write(chr(crc & 0xFF))
self.ser.write(chr(crc >> 8))
self.ser.flush()
sleep(0.2)
state = 2
else:
#print "Something Wrong. Sending ENQ to restart master handshake"
sleep(0.01)
self.ser.write(ENQ)
self.ser.flush()
state = 0
elif state == 2:
print "State 2: Expecting DLE"
if x == DLE:
print "Got DLE"
state = 3
else:
print "Got something other than DLE. Bad"
print "Received data:",x,"=",x.encode('hex')
return False
elif state == 3:
print "State 3: Expecting '1' or '0'"
if x == '1' or x == '0':
print "Got second half of DLE. Sending EOT"
sleep(0.01)
self.ser.write(EOT)
self.ser.flush()
print "First Handshake DC as Master completed"
return True
else:
#print "Got something wrong. Sending NAK"
sleep(0.01)
self.ser.write(NAK)
self.ser.flush()
state = 0
else:
retries = retries - 1
print "***** trying again *****"
print "First Handshake DC as Master gave up"
return False
def secondHandshakeVMDMaster(self):
#print "Entering Second Handshake VMD as Master"
self.ser.flushInput()
state = 0
retries = 5
receivedData = ""
while retries > 0:
x = self.ser.read()
if len(x) > 0:
printReceivedData(x)
retries = 5
if state == 0:
#print "State 0: Waiting for Command"
if x == ENQ:
#print "Got ENQ. Replying with DLE '0'"
sleep(0.01)
self.ser.write(DLE)
self.ser.write('0')
self.ser.flush()
elif x == EOT:
#print "Second Handshake VMD as Master Completed"
return True
elif x == DLE:
#print "Got DLE. Receiving data now"
state = 1
elif state == 1:
#print "State 1: Expecting SOH"
if x == SOH:
#print "Got SOH"
receivedData = ""
state = 2
elif state == 2:
#print "State 2: Receiving data"
if x == DLE:
#print "Got DLE. End of Data"
state = 3
else:
receivedData += x
elif state == 3:
#print "State 3: Expecting ETX"
if x == ETX:
#print "Got ETX"
receivedData += x
state = 4
else:
#print "Got Something else. Resetting"
state = 0
elif state == 4:
#print "State 4: Receiving first CRC byte"
receivedData += x
state = 5
elif state == 5:
#print "State 5: Receiving second CRC byte"
receivedData += x
crc = dexcrc16.crcStr(receivedData)
#print "Calculated crc=",crc
if crc == 0:
#print "CRC is good. Sending DLE,1"
sleep(0.01)
self.ser.write(DLE)
self.ser.write('1')
self.ser.flush()
else:
#print "CRC failed. Sending NAK"
self.ser.write(NAK)
self.ser.flush()
state = 0
else:
retries = retries - 1
sleep(0.01)
#print "trying again"
#print "Second Handshake VMD as Master Gave up"
return False
def VMD2DCExchange(self):
#print "Exchanging data VMD to DC"
receivedData = ""
block = ""
state = 0
retries = 500
currentAck = ACK0
self.ser.flushInput()
while retries > 0:
x = self.ser.read()
printReceivedData(x)
if len(x) > 0:
retries = 500
if state == 0:
#print "State 0: Expecting ENQ"
if x == ENQ:
#print "Got ENQ. Replying ACK"
sleep(0.01)
self.ser.write(DLE)
self.ser.write(currentAck)
self.ser.flush()
if currentAck == ACK0:
currentAck = ACK1
elif currentAck == ACK1:
currentAck = ACK0
elif x == DLE:
#print "Got DLE. Start of block"
state = 1
elif state == 1:
#print "State 1: Expecting STX"
if x == STX:
#print "Got STX. Start block receiving"
state = 2
receivedData = ""
block = ""
elif state == 2:
#print "State 2: Receiving data block"
if x == DLE:
#print "Got DLE. end of data"
state = 3
else:
receivedData += x
block += x
elif state == 3:
#print "State 3: waiting for end of block"
if x == ETB:
#print "Got ETB, end of current block"
receivedData += x
state = 4
elif x == ETX:
#print "Got ETX, end of last block"
receivedData += x
state = 6
else:
#print "Got something other than end of block"
self.ser.write(NAK)
state = 0
elif state == 4:
#print "State 4 - Waiting for first half of CRC"
receivedData += x
state = 5
elif state == 5:
#print "State 5 - Waiting for second half of CRC"
receivedData += x
crc = dexcrc16.crcStr(receivedData)
#print "Got all data, crc=",crc
if crc == 0:
#print "CRC is good"
self.content += block
sleep(0.01)
self.ser.write(DLE)
self.ser.write(currentAck)
self.ser.flush()
if currentAck == ACK0:
currentAck = ACK1
elif currentAck == ACK1:
currentAck = ACK0
state = 0
else:
#print "CRC failed"
sleep(0.01)
self.ser.write(NAK)
self.ser.flush()
state = 0
elif state == 6:
#print "State 6 - Waiting for first half of CRC"
receivedData += x
state = 7
elif state == 7:
#print "State 7 - Waiting for second half of CRC"
receivedData += x
crc = dexcrc16.crcStr(receivedData)
#print "Got all data, crc=",crc
if crc == 0:
#print "CRC is good"
self.content += block
self.ser.write(DLE)
self.ser.write(currentAck)
state = 8
self.ser.flush()
if currentAck == ACK0:
currentAck = ACK1
elif currentAck == ACK1:
currentAck = ACK0
else:
#print "CRC failed"
self.ser.write(NAK)
state = 0
elif state == 8:
#print "State 8 - waiting for EOT"
if x == EOT:
#print "Got EOT, End of data exchange"
return self.content
else:
#print "Got something else."
sleep(0.01)
self.ser.write(NAK)
self.ser.flush()
state = 0
else:
retries = retries - 1
sleep(0.1)
#print "trying again"
#print "Exchanging data VMD to DC Gave Up"
return False
class DataExchanger:
def __init__(self, ser):
self.ser = ser
self.content = ""
def VMD2DCExchange(self):
#print "Exchanging data VMD to DC"
receivedData = ""
block = ""
state = 0
retries = 5
currentAck = ACK0
self.ser.flushInput()
while retries > 0:
x = self.ser.read()
printReceivedData(x)
if len(x) > 0:
retries = 5
if state == 0:
#print "State 0: Expecting ENQ"
if x == ENQ:
#print "Got ENQ. Replying ACK"
sleep(0.01)
self.ser.write(DLE)
self.ser.write(currentAck)
self.ser.flush()
if currentAck == ACK0:
currentAck = ACK1
elif currentAck == ACK1:
currentAck = ACK0
elif x == DLE:
#print "Got DLE. Start of block"
state = 1
elif state == 1:
#print "State 1: Expecting STX"
if x == STX:
#print "Got STX. Start block receiving"
state = 2
receivedData = ""
block = ""
elif state == 2:
#print "State 2: Receiving data block"
if x == DLE:
#print "Got DLE. end of data"
state = 3
else:
receivedData += x
block += x
elif state == 3:
#print "State 3: waiting for end of block"
if x == ETB:
#print "Got ETB, end of current block"
receivedData += x
state = 4
elif x == ETX:
#print "Got ETX, end of last block"
receivedData += x
state = 6
else:
#print "Got something other than end of block"
self.ser.write(NAK)
state = 0
elif state == 4:
#print "State 4 - Waiting for first half of CRC"
receivedData += x
state = 5
elif state == 5:
#print "State 5 - Waiting for second half of CRC"
receivedData += x
crc = dexcrc16.crcStr(receivedData)
#print "Got all data, crc=",crc
if crc == 0:
#print "CRC is good"
self.content += block
sleep(0.01)
self.ser.write(DLE)
self.ser.write(currentAck)
self.ser.flush()
if currentAck == ACK0:
currentAck = ACK1
elif currentAck == ACK1:
currentAck = ACK0
state = 0
else:
#print "CRC failed"
sleep(0.01)
self.ser.write(NAK)
self.ser.flush()
state = 0
elif state == 6:
#print "State 6 - Waiting for first half of CRC"
receivedData += x
state = 7
elif state == 7:
#print "State 7 - Waiting for second half of CRC"
receivedData += x
crc = dexcrc16.crcStr(receivedData)
#print "Got all data, crc=",crc
if crc == 0:
#print "CRC is good"
self.content += block
self.ser.write(DLE)
self.ser.write(currentAck)
state = 8
self.ser.flush()
if currentAck == ACK0:
currentAck = ACK1
elif currentAck == ACK1:
currentAck = ACK0
else:
#print "CRC failed"
self.ser.write(NAK)
state = 0
elif state == 8:
#print "State 8 - waiting for EOT"
if x == EOT:
#print "Got EOT, End of data exchange"
return self.content
else:
#print "Got something else."
sleep(0.01)
self.ser.write(NAK)
self.ser.flush()
state = 0
else:
retries = retries - 1
sleep(0.01)
#print "trying again"
#print "Exchanging data VMD to DC Gave Up"
return False
def DC2VMDExchange(self, content):
print "Exchanging data DC to VMD"
blocks = chunkstring(content, BLOCK_SIZE)
blockIterator = -1
state = 0
retries = 5
self.ser.flushInput()
self.ser.write(ENQ)
self.ser.flush()
while retries > 0:
x = self.ser.read()
if len(x) > 0 or state == 2 or state == 3:
printReceivedData(x)
retries = 5
if state == 0:
print "State 0: Expecting DLE"
if x == DLE:
print "Got DLE"
state = 1
else:
print "Got something else. Sending ENQ to restart data exchange"
sleep(0.01)
self.ser.write(ENQ)
self.ser.flush()
elif state == 1:
print "State 1: Expecting second half of DLE"
if x == '0' or x == '1':
if blockIterator == len(blocks) - 1:
#print "Reached end of content"
sleep(0.01)
self.ser.write(EOT)
self.ser.flush()
print "Data exchange DC to VMD completed"
return True
else:
print "Got second half of DLE. Sending next block"
blockIterator += 1
else:
print "Got something else. Resending block"
sleep(0.01)
self.ser.write(DLE)
self.ser.write(STX)
for char in blocks[blockIterator]:
self.ser.write(char)
if blockIterator < (len(blocks) - 1):
state == 2
elif blockIterator == (len(blocks) - 1):
state == 3
elif state == 2:
print "State 2: Sending block end"
self.ser.write(DLE)
self.ser.write(ETB)
crc = dexcrc16.crcStr(blocks[blockIterator] + ETB)
self.ser.write(chr(crc & 0xFF))
self.ser.write(chr(crc >> 8))
self.flush()
state = 0
elif state == 3:
print "State 3: Sending content end"
self.ser.write(DLE)
self.ser.write(ETX)
crc = dexcrc16.crcStr(blocks[blockIterator] + ETX)
self.ser.write(chr(crc & 0xFF))
self.ser.write(chr(crc >> 8))
self.flush()
state = 0
else:
retries = retries - 1
sleep(0.01)
print "tring again"
print "Exchanging data DC to VMD Gave Up"
return False