Skip to content

Commit 0a4b9cf

Browse files
committed
Optimize serial port reading
1 parent 7338ab5 commit 0a4b9cf

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

pymycobot/common.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,23 @@ def write(self, command, method=None):
284284
self._serial_port.flush()
285285

286286
def read(self):
287-
# time.sleep(0.1)
288-
# t = time.time()
289287
datas = b''
288+
data_len = -1
290289
k = 0
291290
pre = 0
292291
while True:
293292
data = self._serial_port.read()
294-
# print(da)
295293
k+=1
296-
if data == b'\xfe':
294+
if data_len == 1 and data == b'\xfa':
295+
datas += data
296+
break
297+
elif len(datas) == 2:
298+
data_len = struct.unpack("b",data)[0]
299+
datas += data
300+
elif len(datas)>2 and data_len>0:
301+
datas += data
302+
data_len -= 1
303+
elif data == b'\xfe':
297304
if datas == b'':
298305
datas += data
299306
pre = k
@@ -303,18 +310,4 @@ def read(self):
303310
else:
304311
datas = b'\xfe'
305312
pre = k
306-
elif len(datas)>=2:
307-
print(datas)
308-
if data == b'\xfa':
309-
datas += data
310-
break
311-
datas += data
312-
# print(time.time()-t)
313-
# if self._serial_port.inWaiting() > 0:
314-
# data = self._serial_port.read(self._serial_port.inWaiting())
315-
# self.log.debug("_read: {}".format(data))
316-
# else:
317-
# self.log.debug("_read: no data can be read")
318-
# data = None
319-
# print(data)
320313
return datas

0 commit comments

Comments
 (0)