diff --git a/pymycobot/common.py b/pymycobot/common.py index 28a2cb2..dc2e506 100644 --- a/pymycobot/common.py +++ b/pymycobot/common.py @@ -1199,6 +1199,9 @@ def read(self, genre, method=None, command=None, _class=None, timeout=None, real break elif len(datas) == 2: data_len = struct.unpack("b", data)[0] + if data_len < 0 or data_len > 30: + data_len = -1 + continue datas += data elif len(datas) > 2 and data_len > 0: datas += data diff --git a/pymycobot/error.py b/pymycobot/error.py index dd8ea94..782f9df 100644 --- a/pymycobot/error.py +++ b/pymycobot/error.py @@ -418,7 +418,7 @@ def public_check( check_0_or_1( parameter, value, - [19, 22, 23, 33], + [19, 22, 23, 33, 39], value_type, exception_class, int, diff --git a/pymycobot/mycobot280.py b/pymycobot/mycobot280.py index fcc2482..40573e0 100644 --- a/pymycobot/mycobot280.py +++ b/pymycobot/mycobot280.py @@ -150,12 +150,18 @@ def _res(self, real_command, has_reply, genre): data = self._read(genre) else: try_count = 0 + expected_genre = genre while try_count < 3: + self._serial_port.reset_input_buffer() self._write(self._flatten(real_command)) data = self._read(genre) - if data is not None and data != b'': - break - try_count += 1 + if not data or len(data) < 4: + try_count += 1 + continue + if data[3] != expected_genre: + try_count += 1 + continue + break else: return -1 if genre == ProtocolCode.SET_SSID_PWD: diff --git a/pymycobot/mycobot280socket.py b/pymycobot/mycobot280socket.py index 40f7d45..21f0132 100644 --- a/pymycobot/mycobot280socket.py +++ b/pymycobot/mycobot280socket.py @@ -140,12 +140,17 @@ def _res(self, real_command, has_reply, genre): data = self._read(genre, method='socket') else: try_count = 0 + expected_genre = genre while try_count < 3: self._write(self._flatten(real_command), "socket") data = self._read(genre, method='socket') - if data is not None and data != b'': - break - try_count += 1 + if not data or len(data) < 4: + try_count += 1 + continue + if data[3] != expected_genre: + try_count += 1 + continue + break else: return -1 if genre == ProtocolCode.SET_SSID_PWD: