From a74720faac4d872a2a994e8a8fdba63c0c72a59c Mon Sep 17 00:00:00 2001 From: weiquan Date: Wed, 5 Jan 2022 11:42:55 +0800 Subject: [PATCH] Fix m5 network communication bug --- CHANGELOG.md | 7 ++++++- docs/README.md | 7 ++++--- pymycobot/__init__.py | 2 +- pymycobot/common.py | 20 ++++++++++++++------ pymycobot/mycobotsocket.py | 11 ++++++++++- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55a11b2..c4ea7d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # ChangeLog for pymycobot -## v2.7.2.1 (2021-12-15) +## v2.7.4 (2021-12-15) + +- release v2.7.4 +- Fix m5 network communication bug + +## v2.7.3 (2021-12-15) - release v2.7.3 - fix get_tof_distance() no return problem. diff --git a/docs/README.md b/docs/README.md index 807621c..d58f3e5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -80,7 +80,7 @@ We support Python2, Python3.5 or later. - [utils (Module)](#utils-module) - [get_port_list](#get_port_list) - [detect_port_of_basic](#detect_port_of_basic) - - [MyCobotSocket](#mycobotsocket) +- [MyCobotSocket](#mycobotsocket) - [Client](#client) - [Server](#server) - [socket control](#socket-control) @@ -89,6 +89,7 @@ We support Python2, Python3.5 or later. - [set_gpio_out](#set_gpio_out) - [set_gpio_output](#set_gpio_output) - [get_gpio_in](#get_gpio_in) + @@ -788,10 +789,10 @@ from pymycobot import utils mycobot = MyCobot(port, 115200) ``` -## MyCobotSocket +# MyCobotSocket > Note: -> Only supports python3 +> raspberryPi version Only supports python3 > The robotic arm that uses this class of premise has a server and has been turned on. Use TCP/IP to control the robotic arm diff --git a/pymycobot/__init__.py b/pymycobot/__init__.py index 15e7152..1491b21 100644 --- a/pymycobot/__init__.py +++ b/pymycobot/__init__.py @@ -20,7 +20,7 @@ "MyCobotSocket" ] -__version__ = "2.7.3" +__version__ = "2.7.4" __author__ = "Elephantrobotics" __email__ = "weiquan.xu@elephantrobotics.com" __git_url__ = "https://github.com/elephantrobotics/pymycobot" diff --git a/pymycobot/common.py b/pymycobot/common.py index 3155b3a..57facbb 100644 --- a/pymycobot/common.py +++ b/pymycobot/common.py @@ -196,23 +196,31 @@ def _process_single(self, data): def write(self, command, method=None): if method == "socket": - if command[3] == 176 and len(command) > 5: + data = b"" + if len(command)>3 and command[3] == 176 and len(command) > 5: command = "'"+command[4]+"'"+"("+command[5]+")" command = command.encode() - self.sock.sendall(bytes(command)) - if command[-2] == 177: + if self.rasp: + self.sock.sendall(str(command).encode()) + else: + self.sock.sendall(bytes(command)) + + if len(command) > 3 and command[3] == 177: while True: data = self.sock.recv(1024) if b'password' in data: break - if command[-2] == 192: - data = b'' + elif len(command) > 3 and command[3] == 192: while True: data += self.sock.recv(1024) if len(data) == 6: break else: - data = self.sock.recv(1024) + try: + self.sock.settimeout(0.1) + data = self.sock.recv(1024) + except: + data = b'' return data else: self.log.debug("_write: {}".format(command)) diff --git a/pymycobot/mycobotsocket.py b/pymycobot/mycobotsocket.py index 5e9c6cd..7a36da1 100644 --- a/pymycobot/mycobotsocket.py +++ b/pymycobot/mycobotsocket.py @@ -58,9 +58,18 @@ def __init__(self, ip, netport=9000): self.calibration_parameters = calibration_parameters self.SERVER_IP = ip self.SERVER_PORT = netport + self.rasp = False self.sock = self.connect_socket() def connect(self, serialport="/dev/ttyAMA0", baudrate="1000000", timeout='0.1'): + """Interface to connect to the built-in system such as Raspberry Pi + Args: + serialport: + baudrate: + timeout: + + """ + self.rasp = True self._write(serialport, "socket") self._write(baudrate, "socket") self._write(timeout, "socket") @@ -86,7 +95,7 @@ def _mesg(self, genre, *args, **kwargs): MyCobotSocket, self)._mesg(genre, *args, **kwargs) # [254,...,255] data = self._write(self._flatten(real_command), "socket") - if data != b'None': + if data: res = self._process_received(data, genre) if genre in [ ProtocolCode.IS_POWER_ON,