The Meca500 uses TCP/IP socket communication over Port 10,000 to receive commands as ASCII strings, and to 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 Use TCP/IP Sockets?

The Transmission Control Protocol (TCP):

The advantages of the TCP protocol are:

  • It is reliable: packets dropped in the network are detected and retransmitted by the sender
  • It has in-order data delivery: data is read by your application in the order it was written by the sender

By contrast, User Datagram Protocol (UDP) sockets are not reliable, and data read by the receiver can be out-of-order from the one written by the sender. Such networks are a best-effort delivery system. There is no guarantee that your data will reach its destination or that you will receive what has 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 clients and servers. Using TCP relieves you from having to worry about packet loss, data arriving out-of-order, and many other things that invariably happen when you are communicating across a network.

Server/Client communication:

Communication with the robot over TCP/IP sockets follows the client/server relationship model. Here the robot acts as a TCP/IP server that accepts only one connection at a given time from one client. The client could be anything, a Python script, a C++ or C# Application, JavaScript running on a web browser, a PLC or an embedded system. Irrespective of the type of client, the fundamental concept of a TCP/IP communication between a server and a client is the same.

In this article we will explore the Client-Server TCP/IP communication using a Python socket module, specifically the client portion. The Python 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 the 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 a TCP/IP socket object, which 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.

Code example
<script src="https://gist.github.com/navaneethakkrishnan/f7b1918a055ab16a65ade9674ecc28f6.js"></script>

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 10,000. The connect() method of the socket class is used.

Code example
<script src="https://gist.github.com/navaneethakkrishnan/4cae0954c89e94ee1b0522ae2ca4a025.js"></script>

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 [3000][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 [3001][Another user is already connected, closing connection.]

Code example
<script src="https://gist.github.com/navaneethakkrishnan/b07751740ab1ef83f22961a8a00f8fd9.js"></script>

Once the connection has been established and verified, we can send a robot command and read its response.

Code example
<script src="https://gist.github.com/navaneethakkrishnan/3ab1636005833aa58c4a8b6ee49dba83.js"></script>

The robot should now activate and send the response [2000][Motors activated.] or [2001][Motors already activated.]. You could now use the send() method to send the commands and poll the responses with the recv() method after the command has been sent.

After all the commands have been executed you can close the connection using the close() method.

Code example
<script src="https://gist.github.com/navaneethakkrishnan/2abf9f33a41bc6b5156f9382fe256158.js"></script>

You can refer to Python Example - Simple TCP/IP Socket Client for the full code.

Have other questions?

Contact Us