• Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming
Brighter Side Tech
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming
No Result
View All Result
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming
No Result
View All Result
Brighter Side Tech
No Result
View All Result
Home Codesys eCockpit

TCP Socket Client Implementation in Codesys

Mwesigwa Peter by Mwesigwa Peter
January 11, 2024
in Codesys eCockpit
0
TCP Socket Client Implementation in Codesys

TCP Socket Client Implementation in Codesys

0
SHARES
1.6k
VIEWS
Share on FacebookShare on Twitter

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

Tags: industrial automationpythontcp socket client
Previous Post

Modbus TCP Master Simulator Tool – Free Download

Next Post

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

Mwesigwa Peter

Mwesigwa Peter

Related Posts

Parsing JSON Data in Codesys
Automation

Parsing JSON Data in Codesys

Parsing JSON Data in Codesys is quite an important operation especially when exchanging data over the REST API. The data...

by Mwesigwa Peter
January 25, 2025
How to Initialize an Array in Codesys
Codesys eCockpit

How to Initialize an Array in Codesys

Here is how you can Declare and Initialize an Array in Codesys using distinct values for each element of the...

by Mwesigwa Peter
January 13, 2023
Next Post
ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

ARM error: no member named 'IP' in 'NVIC_Type' Solved

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Parsing JSON Data in Codesys
Automation

Parsing JSON Data in Codesys

by Mwesigwa Peter
January 25, 2025
0

Parsing JSON Data in Codesys is quite an important operation especially when exchanging data over the REST API. The data...

Read moreDetails
Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

October 7, 2024
ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

July 18, 2024
TCP Socket Client Implementation in Codesys

TCP Socket Client Implementation in Codesys

January 11, 2024
Modbus TCP Master Simulator

Modbus TCP Master Simulator Tool – Free Download

October 14, 2023
Parsing JSON Data in Codesys

Parsing JSON Data in Codesys

by Mwesigwa Peter
January 25, 2025
0

Parsing JSON Data in Codesys is quite an important operation especially when exchanging data over the REST API. The data...

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

by Mwesigwa Peter
October 7, 2024
0

Introduction Codesys Runtime for Linux ARM SL allows you to program your Raspberry Pi according t the IEC 61131-6 standard...

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

by Mwesigwa Peter
July 18, 2024
0

This is how to solve the error: no member named 'IP' in 'NVIC_Type' Open the misc.c file : AppData\Local\Arm\Packs\Keil\STM32F1xx_DFP\2.4.1\Device\StdPeriph_Driver\src\misc.c, if you are...

TCP Socket Client Implementation in Codesys

TCP Socket Client Implementation in Codesys

by Mwesigwa Peter
January 11, 2024
0

This is a TCP socket client implementation in codesys to run on your PLC and connect to any TCP Socket...

About Brighterside Tech

Brighter Side Tech

As a hobbyist Engineer and Programmer , i love to share my learnings and solutions to technical challenges that i face during work and studying. My website gives me the ability to express my ideas and such solutions to the wider audience

Contact: admin@brightersidetech.com

Browse by Category

  • Automation
  • Codesys eCockpit
  • Docker
  • Embedded Programming
  • Python
  • Raspberry Pi
https://youtu.be/Lkrma5f60rs

Recent News

Parsing JSON Data in Codesys

Parsing JSON Data in Codesys

January 25, 2025
Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

October 7, 2024
ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

July 18, 2024
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming

© 2024 Brighterside Tech.

No Result
View All Result
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming

© 2024 Brighterside Tech.