I have some problems to get my test code to work to receive UDP packages.
I have a broadcaster that sends UDP packages on 192.168.15.X
But I can't get the code to receive the packages.
When I use a test server on my pc like packet sender and I send UDP directly to my plc then I receive the packages.
But also here when I broadcast from the pc and send to 255.255.255.255 I don't the packages
My controller has the IP address 192.168.15.67
Hope someone can help me out what is wront
gc_IP_SendTo : NBS.IP_ADDR := (sAddr := '192.168.15.67'); // My PLC's Ip address
gc_uiPort : UINT := 10110;
Peer : nbs.UDP_Peer;
Send : nbs.UDP_Send;
Receive : nbs.UDP_Receive;
xFirstPacket : BOOL;
xOldActive : BOOL;
udpData : UDPData;
Peer(xEnable := TRUE, ipAddr := gc_IP_SendTo, uiPort := gc_uiPort);
IF Peer.xActive AND NOT xOldActive THEN
xFirstPacket := TRUE;
xOldActive := Peer.xActive;
Receive(xEnable := Peer.xActive, hPeer := Peer.hPeer, szSize := SIZEOF(udpData), pData := ADR(udpData));
IF (Receive.xReady AND Receive.szSize = SIZEOF(udpData)) OR xFirstPacket THEN
xFirstPacket := FALSE;
IF Peer.xError THEN //Connection closed
Peer (xEnable := FALSE);
Send(xExecute := FALSE);
Receive(xEnable := FALSE);
sure it should be 255.255.255.255 and not 255.255.255.0
With 255.255.255.255 you have limited broadcast
nbs.UDP_Receive block works only once after xExecute triggered.
So, right after "Receive.xReady = TRUE", you should set "Receive(xEnable := FALSE)" and then "Receive(xEnable := TRUE)" to get next package.
Also I see in the IF statement "Receive.szSize = SIZEOF(udpData)" - maybe you should set "Receive.szSize = Receive.udiCount" or "Receive.udiCount = SIZEOF(udpData)" instead, if you checking received package size
Log in to post a comment.