This guide demonstrates how to implement a TCP socket client codesys to run on your PLC and connect to any TCP Socket server regardless of the implementation or hardware. The client is implemented using the Codesys socket library syssoket. To start, you should add the syssocket library in your library manager
The syssocket library implements several functions to work with all sorts of socket implementation. This case, we shall use only five of these functions to implement the TCP socket client application. these are
- The syssockcreate : This function creates a new socket and returns the socket handle
- The syssockconnect: This function connects to the socket server using the socket handle created
- The syssocksend: This function is used to send data to the server
- The syssockrecv: This function is used to receive data from the socket server
- The syssockclose: This function id used to close the socket after data exchange has been completed
The next step is to declare the support variables for the TCP socket implementation. The following variables have been used
PROGRAM POU_SOCKET_CLIENT
VAR
iStep : INT := 1;
xStart : BOOL;
iecSocketId : syssocket_interfaces.RTS_IEC_HANDLE; // socket handle
iecCreateResult : syssocket_interfaces.RTS_IEC_RESULT; // socket create result
ipAddr : syssocket.SOCKADDRESS; // soket address
sIpAddress : STRING; // server ip address
wPort : WORD; // server listening port
iecConnectResult : syssocket_interfaces.RTS_IEC_RESULT; // socket connect result
sDataSend : STRING; // send data buffer
xiSentBytes : __XINT; // number of bytes sent
iecSendResult : syssocket_interfaces.RTS_IEC_RESULT; // send result
sDataRec : STRING(255); // receive data buffer
xiRecBytes : __XINT; // number of bytes recieved
iecSRecResult : syssocket_interfaces.RTS_IEC_RESULT; // receive result
iecCloseResult : syssocket_interfaces.RTS_IEC_RESULT; // close result
xiRecvBytes : BOOL;
END_VAR
The TCP Socket Client is implemented in form of a Finite State machine with five states. In each state, an action is performed and if this is successful, the machine transitions to the next state. The five states are described as follows
- The Socket Create State: In this state, the client PLC attempts to create a TCP Socket. If this attempt is successful, the socket handle is stored and the machine transitions to the next state
- The Socket connect state: In this state, the client attempts to connect to the TCP socket server. One the connection has been accepted, the machine then transition to the next state
- The Send data State: In this state, the client send data to the server and then transitions to the next state
- The receive data State: One the data has been received by the server, the client will listen for any server responses form this state
- The close socket state: This state closes the socket and then goes back to the initial state
Creating the TCP Socket
The syssockcreate function from the syssocket library is used to create a new socket. The function takes in 4 parameters, namely, the Address Family, Socket Type the socket protocol and a pointer to a Result type. The function returns a socket handle if the socket has successfully been created otherwise it returns -1
iecSocketId := syssocket.SysSockCreate(syssocket.SOCKET_AF_INET,
syssocket.SOCKET_STREAM, syssocket.SOCKET_IPPROTO_IP,
ADR(iecCreateResult));
Connecting to the socket server
If a socket has been created successfully, the client is now ready to request connection to the TCP socket server. To do this, the clinet uses the syssockconnect function. The function takes in three parameters, namely, the socket handle, a pointer to a SOCKADDDRESS variable and the size of the SOCKADDDRESS variable. The SOCKADDDRESS is a defined STRUCT Type that is made up of the address family of the socket server, the socket port on which the socket server is listening and the ip-address of the socket server. there are two other functions in the syssocket library that help us compose the SOCKADDDRESS
syssocket.SysSockInetAddr(sIpAddress, ADR(ipAddr.sin_addr));
ipAddr.sin_family := syssocket.SOCKET_AF_INET;
ipAddr.sin_port := syssocket.SysSockHtons(wPort);
After composing the SOCKADDDRESS, the clinet is now ready to request for connection
iecConnectResult := syssocket.SysSockConnect(iecSocketId, ADR(ipAddr),
SIZEOF(ipAddr));
Send data to TCP Socket Server
Once the connection request has been accepted, the client is now ready to send data to the Socket server. We use the syssocksend function, which takes in as its parameters, the socket handle and a pointer to the data buffer (of type byte) to be sent to the server, the size of the data buffer and a pointer to a pointer to a result variable which will store the error number in case an error occurs. The function returns the number of bytes sent to the server.
xiSentBytes := syssocket.SysSockSend(iecSocketId, ADR(sDataSend),
SIZEOF(sDataSend), 1, ADR(iecSendResult));
Receive data from the Socket server
After data has been successfully sent to the server, the client can now listen for any incoming data. For this the syssockrecv function is used. The function takes in the socket handle, a pointer to a receive data buffer, the size of the buffer and a pointer to a result variable which will store the error number in case an error occurs. The function returns the number of bytes received form the server
xiRecBytes := syssocket.SysSockRecvFrom(iecSocketId, ADR(sDataRec),
SIZEOF(sDataRec), 0, ADR(ipAddr), SIZEOF(ipAddr), ADR(iecSRecResult));
Close the socket
The syssockclose function will now close the socket after all operations have been completed
iecCloseResult := syssocket.SysSockClose(iecSocketId);
Full Code
syssocket.SysSockInetAddr(sIpAddress, ADR(ipAddr.sin_addr));
ipAddr.sin_family := syssocket.SOCKET_AF_INET;
ipAddr.sin_port := syssocket.SysSockHtons(wPort);
IF xiRecvBytes AND xStart THEN
xiRecBytes := syssocket.SysSockRecvFrom(iecSocketId, ADR(sDataRec),
SIZEOF(sDataRec), 0, ADR(ipAddr), SIZEOF(ipAddr), ADR(iecSRecResult));
xiRecvBytes := FALSE;
END_IF
CASE iStep OF
1:
// create the socket
IF xStart THEN
iecSocketId := syssocket.SysSockCreate(syssocket.SOCKET_AF_INET,
syssocket.SOCKET_STREAM, syssocket.SOCKET_IPPROTO_IP,
ADR(iecCreateResult));
IF iecSocketId = syssocket_interfaces.RTS_INVALID_HANDLE THEN
xStart := FALSE;
iStep := 1;
ELSE
iStep := 2;
END_IF
END_IF
2:
// connect to socket server
iecConnectResult := syssocket.SysSockConnect(iecSocketId, ADR(ipAddr),
SIZEOF(ipAddr));
iStep := 3;
3:
// send date
xiSentBytes := syssocket.SysSockSend(iecSocketId, ADR(sDataSend),
SIZEOF(sDataSend), 1, ADR(iecSendResult));
xiRecvBytes := TRUE;
iStep := 5;
5:
// close socket
IF NOT xiRecvBytes THEN
iecCloseResult := syssocket.SysSockClose(iecSocketId);
xStart := FALSE;
iStep := 1;
END_IF
END_CASE
Simple TCP Socket Server implementation in python
import socket
import time
# create an INET, STREAMing socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to a public host, and a well-known port
serversocket.bind(('0.0.0.0', 8085))
# listen for connections
serversocket.listen(5)
# accept connections
(clientsocket, address) = serversocket.accept()
print(address)
# recieve data
byRecData = clientsocket.recv(1024)
print(byRecData)
# decode data
recData = byRecData.decode("utf-8")
print(recData)
# get data to send back
sendData = recData
print(sendData)
# encode string
bySendData = sendData.encode('utf-8')
print(bySendData)
# send back data
clientsocket.sendall(bySendData)
Video Tutorial