Socket communication, full name Socket Messaging, enables RAPID programmers to transfer application data between different computers through the TCP/IP network protocol. Sockets, as communication endpoints, provide a universal channel that is independent of specific network protocols. The concept of “socket communication” originated from the Berkeley Software Distribution (BSD) version of the Unix operating system, and in addition to Unix systems, other platforms such as Microsoft Windows also widely support this standard. Using Socket Messaging, RAPID programs running on the robot controller can exchange data and communicate with programs written in programming languages such as C/C++ on another computer.
ABB robot Socket communication enables data exchange between the robot and external devices. It uses TCP/IP protocol to establish connections. The robot can send and receive various types of information, like control commands and status data. This communication method is flexible and reliable, facilitating seamless integration with other systems in industrial automation.
- Socket communication implementation steps
(1) Create a socket on the client and server respectively. The robot controller can act as both a client and a server. (2) Execute SocketBind and SocketListen on the corresponding server to prepare for the upcoming connection request.
(3) Instruct the relevant server to accept socket connection requests from the outside.
(4) The relevant client initiates a socket connection request.
(5) Send and receive data between the client and the server. 2. Socket communication diagram
Note:
(1) No string terminator: When sending a data message, the system automatically adds a no string terminator to the end of the message. The number of data bytes sent is consistent with the value returned by the strlen(str) function in the C language. (2) Unexpected message merging: If two messages are sent consecutively without a delay, the second message may be appended to the end of the first message, forming a long message instead of two independent messages. To prevent this from happening, the receiver should use a confirmation mechanism when receiving messages.
(3) Unprintable characters: If a non-RAPID task client needs to receive unprintable characters (such as binary data) from a string of a RAPID task, it can use the following sample code to receive it:SocketSend socket1 \Str:=”\0D\0A”;For more information, see the Technical Reference Manual – RAPID Language Kernel String Literals section. - Code Implementation
The following example contains the program code required for the client and server to communicate with each other.
The server will be written on the FlexPendant:
Client wrote – Hello server
Client wrote – Shutdown connection
The client will be written on the FlexPendant:
Server wrote – Message acknowledged
Server wrote – Shutdown acknowledged
In this example, both the client and the server use RAPID programs, but in practice, people often run one of these programs on a PC
(or similar computer) and write the program in another programming language.
Client code example, contacting the server with IP address 192.168.0.2:
! WaitTime to delay start of client.
! Server application should start first.
WaitTime 5;
VAR socketdev socket1;
VAR string received_string;
PROC main()
SocketCreate socket1;
SocketConnect socket1, “192.168.0.2”, 1025;
! Communication
SocketSend socket1 \Str:=”Hello server”;
SocketReceive socket1 \Str:=received_string;
TPWrite “Server wrote – ” + received_string;
received_string := “”;
! Continue sending and receiving
…
! Shutdown the connection
SocketSend socket1 \Str:=”Shutdown connection”;
SocketReceive socket1 \Str:=received_string;
TPWrite “Server wrote – ” + received_string;
SocketClose socket1;
ENDPROC
Server code example (with IP Address 192.168.0.2): VAR socketdev temp_socket; VAR socketdev client_socket; VAR string received_string; VAR bool keep_listening := TRUE; PROC main() SocketCreate temp_socket; SocketBind temp_socket, “192.168.0.2”, 1025; SocketListen temp_socket; WHILE keep_listening DO ! Waiting for a connection request SocketAccept temp_socket, client_socket; ! Communication SocketReceive client_socket \Str:=received_string; TPWrite “Client wrote – ” + received_string; received_string := “”; SocketSend client_socket \Str:=”Message acknowledged”; ! Shutdown the connection SocketReceive client_socket \Str:=received_string; TPWrite “Client wrote – ” + received_string; SocketSend client_socket \Str:=”Shutdown acknowledged”; SocketClose client_socket; ENDWHILE SocketClose temp_socket; ENDPROC
