The Meca500 uses TCP/IP socket communication over Port 10000 to receive the commands as ASCII strings and send its responses as ASCII strings as well. Each command must be appended with an ASCII NULL terminator. Commands sent without ASCII NULL will not be processed by the robot with the robot returning an error..
Why TCP/IP Sockets?
The Transmission Control Protocol (TCP):
Is reliable: packets dropped in the network are detected and retransmitted by the sender
Has in-order data delivery: data is read by your application in the order it was written by the sender
In contrast, User Datagram Protocol (UDP) socket aren’t reliable, and data read by the receiver can be out-of-order from the sender’s writes. Networks are a best-effort delivery system. There’s no guarantee that your data will reach its destination or that you’ll receive what’s been sent to you.
Network devices (for example, routers and switches), have finite bandwidth available and their own inherent system limitations. They have CPUs, memory, buses, and interface packet buffers, just like our clients and servers. TCP relieves you from having to worry about packet loss, data arriving out-of-order, and many other things that invariably happen when you’re communicating across a network
In this article we will explore this using Python's Socket module specifically the client part. Python’s Socket module provides an interface for working with TCP/IP sockets.
The client calls connect() to establish a connection to the server and initiate the three-way handshake. The handshake step is important since it ensures that each side of the connection is reachable in the network, in other words that the client can reach the server and vice-versa. It may be that only one host, client or server, can reach the other.
Once a connection has been established, data is exchanged between the client and server using calls to send() and recv(). In the case of the robot, the client will call on send() to send the robot commands as ASCII strings and will call on recv() to receive the responses from the robot. After all the data exchanges are completed, the client calls close() to close the socket.
The first step is to create TCP/IP socket object that is needed to connect to the robot. The socket object "robot" is created as IPv4 , TCP/IP type which is defined with socket.AF_INET and socket.SOCK_STREAM arguments respectively
#!/usr/bin/env python3 import socket import time ROBOT_IP = '192.168.0.100' # IP address of the robot ROBOT_PORT = 10000 # Port used by the robot to receive commands robot = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create the socket object robot
The second step is to connect this socket object with the server (Robot) with the IP address and Port of the robot. The default values are "192.168.0.100" and 10000. The connect() method of the socket class is used for this.
So far the socket has been created and we have connected it to the robot. To verify the connection, we now use the recv() method to read the data sent by the robot upon connection. If the robot accepted the connection, the response will be [Connected to Meca500 R3 v8.x.x.] where v8.x.x is the firmware version. If the robot is already connected to a client, the connect() will return an error and the robot will respond with [Another user is already connected, closing connection.]
response = robot.recv(1024).decode('ascii') print(response)
Once the connection has been established and verified, we can send a robot command and read its response.
cmd = "ActivateRobot" # Command to send to the robot robot.send(bytes(cmd+"\0",'ascii')) # Append ASCII NULL after the command and send as ASCII time.sleep(10) response = robot.recv(1024).decode('ascii') # Read the response print(response)
机器人现在应该激活并发送响应e [Motors activated.] or [Motors already activated.]. You could now use the send() method to send the commands and poll the responses with recv() method after the command has been sent.
After all the commands have been executed you can close the connection using the close() method
robot.close() # Close the socket
You can refer toPython Example - Simple TCP/IP Socket Clientfor the full code.