-
Notifications
You must be signed in to change notification settings - Fork 9
/
readWriteUart.py
232 lines (214 loc) · 9.39 KB
/
readWriteUart.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import threading
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class readWriteUart(QThread):
sig_uiRecvFromUart = pyqtSignal(str)
sig_ctrlRecvUartMsg = pyqtSignal(str)
def __init__(self,queue,parent):
# super(readWriteUart,self).__init__(parent)
super().__init__()
self.ui=parent
self.model=""
self.writemsg=""
self.lastmodel=""
self.queue=queue
self.maybeCN=b""
self.currentBoard="esp32"
# self.connect(self.ui,SIGNAL("changeCurrentBoard"),self.changeCurrentBoard)
self.ui.sig_changeCurrentBoard.connect(self.changeCurrentBoard)
def run(self):
readNum=0
sendData=""
execSendData=""
self.maybeCN=b""
while True:
if self.ui.myserial.ser.isOpen():
if not self.queue.empty():
cmd=self.queue.get()
print("cmd=%s"%cmd)
if cmd.find(":::")>=0:
cmd=cmd.split(":::")
self.model=cmd[0]
if type(cmd[1]) is bytes:
self.writemsg=cmd[1]
elif type(cmd[1]) is str:
self.writemsg=cmd[1].encode('utf-8')
else:
self.model=cmd
self.writemsg=""
if self.model=="close":
self.lastmodel=""
self.model=""
break
elif self.model=="uitouart":
if self.writemsg.decode()=="\x03":
self.lastmodel=""
elif self.lastmodel=="exec_":
# self.emit(SIGNAL("sig_uiRecvFromUart"),"program is running,do anything with stop it!1\n")
self.sig_uiRecvFromUart.emit("program is running,do anything with stop it!1\n")
continue
try:
self.ui.myserial.ser.write(self.writemsg)
if self.currentBoard != "microbit":
self.ui.myserial.ser.flush()
except Exception:
self.lastmodel=""
self.model=""
break
elif self.model=="ctrltouart":
if self.lastmodel=="exec_":
# self.emit(SIGNAL("sig_uiRecvFromUart"),"program is running,do anything with stop it!2\n")
self.sig_uiRecvFromUart.emit("program is running,do anything with stop it!2\n")
self.model="uitouart"
continue
try:
self.ui.myserial.ser.write(self.writemsg)
if self.currentBoard != "microbit":
self.ui.myserial.ser.flush()
except Exception:
self.lastmodel=""
self.model=""
break
elif self.model=="exec_":
if self.lastmodel=="exec_":
# self.emit(SIGNAL("sig_uiRecvFromUart"),"program is running,do anything with stop it!3\n")
self.sig_uiRecvFromUart.emit("program is running,do anything with stop it!3\n")
self.model="uitouart"
continue
self.lastmodel="exec_"
try:
self.ui.myserial.ser.write(self.writemsg)
if self.currentBoard != "microbit":
self.ui.myserial.ser.flush()
except Exception:
self.lastmodel=""
self.model=""
break
if self.model=="" or self.model=="uitouart":
try:
data=self.ui.myserial.ser.read(1)
except Exception as e:
break
if data==b'':
continue
if ord(data)>127:
pass
else:
data=data.decode()
if self.lastmodel=="exec_":
execSendData+=str(data)
if execSendData.find(">")<0:
execSendData=""
elif execSendData.find(">>> ")>=0:
execSendData=""
self.lastmodel=""
# self.emit(SIGNAL("sig_uiRecvFromUart"),data)
self.sig_uiRecvFromUart.emit(data)
#if self.ui.myserial.ser.inWaiting()>=128:
# self.ui.myserial.ser.flushInput()
elif self.model=="exec_":
try:
data=self.ui.myserial.ser.read(1)
except Exception:
break
try:
data=data.decode()
except Exception as e:
print(e)
execSendData+=str(data)
if execSendData.find(">")<0:
execSendData=""
elif execSendData.find(">>> ")>=0:
execSendData=""
self.lastmodel=""
# self.emit(SIGNAL("sig_uiRecvFromUart"),data)
self.sig_uiRecvFromUart.emit(data)
if self.ui.myserial.ser.inWaiting()>=128:
self.ui.myserial.ser.flushInput()
elif self.model=="ctrltouart":
try:
data=self.ui.myserial.ser.read(10)
except Exception:
break
if len(data)>2:
if str(data[-2:-1]).find("b'\\xe")>=0:
time.sleep(0.001)
recv=self.ui.myserial.ser.read(1)
if recv==b'':
data=data[0:-2]
else:
data+=recv
elif str(data[-1:]).find("b'\\xe")>=0:
time.sleep(0.001)
recv=self.ui.myserial.ser.read(2)
if recv==b'' or len(recv)!=2:
data=data[0:-1]
else:
data+=recv
else:
pass
elif len(data)==2:
if str(data[0:1]).find("b'\\xe")>=0:
time.sleep(0.001)
recv=self.ui.myserial.ser.read(1)
if recv==b'':
data=data[0:-2]
else:
data+=recv
elif str(data[1:]).find("b'\\xe")>=0:
time.sleep(0.001)
recv=self.ui.myserial.ser.read(2)
if recv==b'' or len(recv)!=2:
data=data[0:-1]
else:
data+=recv
else:
if data==b'':
pass
else:
if str(data).find("b'\\xe")>=0:
time.sleep(0.001)
recv=self.ui.myserial.ser.read(2)
if recv==b'' or len(recv)!=2:
data=data[0:-1]
else:
data+=recv
if self.maybeCN!=b'':
data=self.maybeCN+data
try:
data=data.decode()
self.maybeCN=b''
except Exception as e:
#print("data:%s"%data)
if len(self.maybeCN)>=3:
self.maybeCN=b''
continue
self.maybeCN=data
continue
self.maybeCN=b''
if data=="" or data==None:
readNum+=1
if readNum==6:
# self.emit(SIGNAL("sig_ctrlRecvUartMsg"),sendData)
self.sig_ctrlRecvUartMsg.emit(sendData)
sendData=""
readNum=0
else:
sendData+=data
readNum=0
else:
# self.emit(SIGNAL("sig_uiRecvFromUart"),"error read model")
self.sig_uiRecvFromUart.emit("error read model")
else:
time.sleep(0.01)
print("class read out")
self.lastmodel=""
self.model=""
self.exit()
def changeCurrentBoard(self,board):
self.currentBoard=board