forked from mdisibio/dex-machinesim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdexmachinesim.py
227 lines (197 loc) · 7.26 KB
/
dexmachinesim.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
import serial
import dexcrc16
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)
class DexMachineSim:
def __init__(self,serialPath, dexContent, stayOpen=True):
self.path = serialPath
self.content = dexContent
self.stayOpen = stayOpen
self.ser = None
def run(self):
while True:
print "--------------------------------"
self.openConnection()
try:
self.waitConnection()
except serial.SerialException:
print "Serial exception"
print "Closing connection"
self.ser.close()
if not self.stayOpen:
return
def openConnection(self):
print "opening connection to " + self.path
self.ser = serial.Serial(self.path, 9600, timeout=0.01)
def waitConnection(self):
print "Waiting for connection"
while True:
x = self.ser.read()
if len(x) > 0:
self.printReceivedData(x)
for char in x:
if char == ENQ:
print "Received ENQ for init"
#master mode
self.ser.write(ENQ)
self.ser.flush()
self.slaveHandshake()
self.masterHandshake()
self.exchangeData()
# slave mode
#ser.write(DLE)
#masterHandshake()
#slaveHandshake()
#exchangeData()
while True:
x = self.ser.read()
if len(x) > 0:
self.printReceivedData(x)
def masterHandshake(self):
print "Entering Master Handshake"
receivedData = ""
print "Waiting for ready"
ready = False
while not ready:
for i in range(0,5):
x = self.ser.read()
if x == ENQ:
print "Received ENQ, replying with DLE,0.."
self.ser.write(DLE)
self.ser.write('0')
self.ser.flush()
ready = True
print "Received ready response"
print "Waiting Master check string"
while True:
x = self.ser.read()
if len(x) > 0:
self.printReceivedData(x)
receivedData += x
if len(receivedData) >= 4 and \
receivedData[-4] == DLE and \
receivedData[-3] == ETX:
print "Received master check string and CRC. Auto-confirming it."
self.ser.write(DLE)
self.ser.write('0')
if len(receivedData) >= 1 and receivedData[-1] == EOT:
print "Received EOT, end of master handshake."
return
def slaveHandshake(self):
print "Entering slave handshake"
self.ser.flushInput()
receivedData = ""
print "waiting for ready"
self.ser.write(ENQ)
self.ser.flush()
ready = False
while not ready:
for i in range(0,5):
x = self.ser.read()
if len(x) > 0:
receivedData += x
if len(receivedData) >= 2 and \
receivedData[-2] == DLE and \
receivedData[-1] == '0':
print "Recevied ready response"
ready = True
break
if not ready:
self.ser.write(ENQ)
self.ser.flush()
self.ser.write(EOT)
self.ser.flush()
#while True:
# x = self.ser.read()
# if len(x) > 0:
# self.printReceivedData(x)
# if x == ENQ:
# self.ser.write(ENQ)
# else:
# self.ser.write(EOT)
# self.ser.flush()
# return
print "end of slave handshake"
def printReceivedData(self, data):
print "Received data:",data,"=",data.encode('hex')
def uploadblock(self,data, final):
finalmarker = ETB
if final:
finalmarker = ETX
crc = dexcrc16.crcStr(data + finalmarker)
print "Writing block:", data.rstrip(),"CRC=",crc
self.ser.flushInput()
sleep(0.01)
self.ser.write(DLE)
self.ser.write(STX)
self.ser.write(data)
self.ser.write(DLE)
self.ser.write(finalmarker)
# Write checksum
self.ser.write(chr(crc & 0xFF))
self.ser.write(chr(crc >> 8))
receivedData = ""
while True:
x = self.ser.read()
if len(x) > 0:
self.printReceivedData(x)
receivedData += x
if len(receivedData) >= 2 and \
receivedData[-2] == DLE and \
(receivedData[-1] == '0' or receivedData[-1]=='1'):
print "received confirmation of block"
return
def waitForExchangeReady(self):
ready = False
print "Waiting for ready signal before exchanging data"
self.ser.flushInput()
sleep(0.01)
receivedData = ""
print "Writing ENQ"
self.ser.write(ENQ)
self.ser.flush()
while not ready:
for i in range(0,5):
x = self.ser.read()
if len(x) > 0:
self.printReceivedData(x)
receivedData += x
if len(receivedData) >= 2 and \
receivedData[-2] == DLE and \
receivedData[-1] == '0':
print "Received query response"
ready = True
break
if not ready:
print "Not ready yet. Writing ENQ"
self.ser.write(ENQ)
self.ser.flush()
def exchangeData(self):
print "Exchanging data"
self.waitForExchangeReady()
for i,block in enumerate(self.content):
final = i == len(self.content) -1
self.uploadblock(block, final)
print "writing end of transmission"
self.ser.write(EOT)
while True:
x = self.ser.read()
if x == NAK:
print "got a nack"
if len(x) > 0:
self.printReceivedData(x)
receivedData += x
if len(receivedData) >= 2 and \
receivedData[-2] == DLE:
print "received response"
break
self.ser.write(EOT)
self.ser.flush()
return