anonymous - 2024-06-03

Hi, I try to send and receive data using a UDP connection via SysSocket 3.5.17.0. While sending data works fine, I have problems with the receiving part.I am able to capture the data of client side in wireshark but i am unable to capture it in the codesys. Heres the below part of code of client side.

PROGRAM POU_udpclient_program
VAR
istep : INT := 1;//step variable for state machine
xStart: BOOL;// Flag to start the UDP protocol

iecSocketId: syssocket_interfaces.RTS_IEC_HANDLE;//socket handle for receiving
iecCreateResult: syssocket_interfaces.RTS_IEC_RESULT;
ipAddr: syssocket.SOCKADDRESS;//Socket address structure for receiving

sIpAddress : STRING := '192.168.0.2';
wPort: WORD:= 12346;
iecConnectResult : syssocket_interfaces.RTS_IEC_RESULT;//connect paramters

sDataRec : STRING[255];//Buffer for received data
xiRecBytes : __XINT;//number of bytes received
iecRecResult : syssocket_interfaces.RTS_IEC_RESULT;//receive data parameters

iecCloseResult : syssocket_interfaces.RTS_IEC_RESULT;

END_VAR

syssocket.SysSockInetAddr(sIpAddress,ADR(ipAddr.sin_addr));
ipAddr.sin_family := syssocket.SOCKET_AF_INET;
ipAddr.sin_port := syssocket.SysSockHtons(wPort);

CASE istep OF
1:
//create socket
IF xStart THEN
iecSocketId:= syssocket.SysSockCreate(syssocket.SOCKET_AF_INET,syssocket.SOCKET_DGRAM,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 using setoption 
 iecConnectResult := syssocket.SysSockSetOption(iecSocketId,syssocket.SOCKET_SOL,syssocket.SOCKET_SO_REUSEADDR,ADR(ipAddr),SIZEOF(ipAddr));
 istep := 3;

3:
//receive data
xiRecBytes := syssocket.SysSockRecvFrom(iecSocketId,ADR(sDataRec),SIZEOF(sDataRec),0,ADR(ipAddr),SIZEOF(ipAddr),ADR(iecRecResult));
istep := 4;

4:
//close socket
iecCloseResult:= syssocket.SysSockClose(iecSocketId);
xStart := FALSE;
istep := 1;

END_CASE

 

Last edit: anonymous 2024-06-03