forked from airuoshahen/python_uart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.py
399 lines (328 loc) · 11.7 KB
/
uart.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import operator
import serial
import serial.tools.list_ports
import _thread
import time
from tkinter import *
global openSerialList
global serialToTagNumList
global lastRecvStrecStrList
openSerialList = []
serialToTagNumList = []
lastRecvStrecStrList = []
def btnResetCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('RESET\r\n'.encode())
except:
print(openSerialList[i].portstr+' Reset failed!')
else:
print(openSerialList[i].portstr+' Reset success!')
def btnMpuonCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('MPUON\r\n'.encode())
except:
print(openSerialList[i].portstr+' Mpuon failed!')
else:
print(openSerialList[i].portstr+' Mpuon success!')
def btnMpuofCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('MPUOF\r\n'.encode())
except:
print(openSerialList[i].portstr+' Mpuof failed!')
else:
print(openSerialList[i].portstr+' Mpuof success!')
def btnSlotCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('SLOT\r\n'.encode())
except:
print(openSerialList[i].portstr+' Slot failed!')
else:
print(openSerialList[i].portstr+' Slot success!')
def btnSmagCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('SMAG\r\n'.encode())
except:
print(openSerialList[i].portstr+' Smag failed!')
else:
print(openSerialList[i].portstr+' Smag success!')
def btnSteponCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('STEPON\r\n'.encode())
except:
print(openSerialList[i].portstr+' Stepon failed!')
else:
print(openSerialList[i].portstr+' Stepon success!')
def btnStepofCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('STEPOF\r\n'.encode())
except:
print(openSerialList[i].portstr+' Stepof failed!')
else:
print(openSerialList[i].portstr+' Stepof success!')
def btnDisonCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('DISON\r\n'.encode())
except:
print(openSerialList[i].portstr+' Dison failed!')
else:
print(openSerialList[i].portstr+' Dison success!')
def btnDisofCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('DISOF\r\n'.encode())
except:
print(openSerialList[i].portstr+' Disof failed!')
else:
print(openSerialList[i].portstr+' Disof success!')
def btnMagonCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('MAGON\r\n'.encode())
except:
print(openSerialList[i].portstr+' Magon failed!')
else:
print(openSerialList[i].portstr+' Magon success!')
def btnMagofCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('MAGOF\r\n'.encode())
except:
print(openSerialList[i].portstr+' Magof failed!')
else:
print(openSerialList[i].portstr+' Magof success!')
def btnUwbonCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('UWBON\r\n'.encode())
except:
print(openSerialList[i].portstr+' Uwbon failed!')
else:
print(openSerialList[i].portstr+' Uwbon success!')
def btnUwbofCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
# if serialToTagNumList[i] > 0 and serialToTagNumList[i] < 5:
try:
openSerialList[i].write('UWBOF\r\n'.encode())
except:
print(openSerialList[i].portstr+' Uwbof failed!')
else:
print(openSerialList[i].portstr+' Uwbof success!')
def btnCloseCallback():
if len(openSerialList) <= 0:
print('len <= 0')
return
else:
for i in range(0,len(openSerialList)):
try:
openSerialList[i].close()
except:
print(openSerialList[i].portstr+' Close failed!')
else:
print(openSerialList[i].portstr+' Close success!')
def guiShow():
root = Tk()
root.title('HansonCom')
root.geometry("700x550")
btnReset = Button(text="RESET", borderwidth="2", font=("微软雅黑", 15), background="white")
btnReset['command'] = btnResetCallback
btnReset.place(x=10, y=10, width=100, height=40)
btnMpuon = Button(text="MPUON", borderwidth="2", font=("微软雅黑", 15), background="white")
btnMpuon['command'] = btnMpuonCallback
btnMpuon.place(x=10, y=60, width=100, height=40)
btnMpuof = Button(text="MPUOF", borderwidth="2", font=("微软雅黑", 15), background="white")
btnMpuof['command'] = btnMpuofCallback
btnMpuof.place(x=10, y=110, width=100, height=40)
btnSlot = Button(text="SLOT", borderwidth="2", font=("微软雅黑", 15), background="white")
btnSlot['command'] = btnSlotCallback
btnSlot.place(x=10, y=160, width=100, height=40)
btnSmag = Button(text="SMAG", borderwidth="2", font=("微软雅黑", 15), background="white")
btnSmag['command'] = btnSmagCallback
btnSmag.place(x=10, y=210, width=100, height=40)
btnStepon = Button(text="STEPON", borderwidth="2", font=("微软雅黑", 15), background="white")
btnStepon['command'] = btnSteponCallback
btnStepon.place(x=10, y=260, width=100, height=40)
btnStepof = Button(text="STEPOF", borderwidth="2", font=("微软雅黑", 15), background="white")
btnStepof['command'] = btnStepofCallback
btnStepof.place(x=10, y=310, width=100, height=40)
btnDison = Button(text="DISON", borderwidth="2", font=("微软雅黑", 15), background="white")
btnDison['command'] = btnDisonCallback
btnDison.place(x=10, y=360, width=100, height=40)
btnDisof = Button(text="DISOF", borderwidth="2", font=("微软雅黑", 15), background="white")
btnDisof['command'] = btnDisofCallback
btnDisof.place(x=10, y=410, width=100, height=40)
btnMagon = Button(text="MAGON", borderwidth="2", font=("微软雅黑", 15), background="white")
btnMagon['command'] = btnMagonCallback
btnMagon.place(x=10, y=460, width=100, height=40)
btnMagof = Button(text="MAGOF", borderwidth="2", font=("微软雅黑", 15), background="white")
btnMagof['command'] = btnMagofCallback
btnMagof.place(x=10, y=510, width=100, height=40)
btnUwbon = Button(text="UWBON", borderwidth="2", font=("微软雅黑", 15), background="white")
btnUwbon['command'] = btnUwbonCallback
btnUwbon.place(x=120, y=10, width=100, height=40)
btnUwbof = Button(text="UWBOF", borderwidth="2", font=("微软雅黑", 15), background="white")
btnUwbof['command'] = btnUwbofCallback
btnUwbof.place(x=120, y=60, width=100, height=40)
btnClose = Button(text="CLOSE", borderwidth="2", font=("微软雅黑", 15), background="white")
btnClose['command'] = btnCloseCallback
btnClose.place(x=120, y=110, width=100, height=40)
root.mainloop()
def save_to_file(file_name,contents):
try:
fh = open(file_name,'r+')
fh.seek(0,2)
fh.write(contents)
fh.close()
except:
fh = open(file_name,'w')
fh.write(contents)
fh.close()
def comHandle():
plist = list(serial.tools.list_ports.comports())
if len(plist) <= 0:
print('The Serial port can not find!')
else:
for i in range(0,len(plist)):
plist_0 = list(plist[i])
serialName = plist_0[0]
print(serialName)
try:
serialFd = serial.Serial(serialName,115200,timeout=0.02)
print('check which port was really used >',serialFd.name)
if serialFd.isOpen():
print(serialFd.name,' is open!')
serialFd.WriteTimeout = 1
openSerialList.append(serialFd)
serialToTagNumList.append(5)
lastRecvStrecStrList.append(['a'])
else:
print(serialFd.name,' is close!')
except:
print(serialName,' open failed!')
print(serialToTagNumList)
print(openSerialList)
time.sleep(10)
#下面19--25行的程序是如何运用串口读取发送数据
while True:
if len(openSerialList)==0:
print('no open serial!')
break
for i in range(0,len(openSerialList)):
readBytes = openSerialList[i].readline()
try:
readStr = bytes.decode(readBytes)
except:
print(openSerialList[i].portstr,readBytes)
else:
if len(readStr) != 0:
lastRecvStr = ','.join(lastRecvStrecStrList[i])
if operator.ne(lastRecvStr,readStr):
print(lastRecvStr,'is last')
lastRecvStrecStrList.insert(i,readStr.split(','))
lastRecvStrecStrList.pop(i+1)
print(readStr,'is new')
if 'slot' in readStr:
serialToTagNumList.insert(i,int(readStr[-3],base=10))
serialToTagNumList.pop(i+1)
fileName = 'tag'+readStr[-3]+'.txt'
print('fileName is '+fileName)
save_to_file(fileName,'tag'+readStr[-3]+' save file test!\n')
else:
if serialToTagNumList[i] == 1:
save_to_file('tag1.txt',readStr)
elif serialToTagNumList[i] == 2:
save_to_file('tag2.txt',readStr)
elif serialToTagNumList[i] == 3:
save_to_file('tag3.txt',readStr)
elif serialToTagNumList[i] == 4:
save_to_file('tag4.txt',readStr)
else:
print(openSerialList[i].portstr,'not get tag num!')
else:
print(openSerialList[i].portstr,'recv same str!')
print(openSerialList[i].portstr,'recv',readStr)
# else:
# print(openSerialList[i].portstr,'recv nothing!')
# print(serialToTagNumList)
if __name__ == '__main__':
try:
_thread.start_new_thread(comHandle,())
_thread.start_new_thread(guiShow,())
except:
print('Error:无法启动线程!')
while True:
pass