I've been trying to receive UDP packets with the UDP library (can be found in CAA Net Base Services) for quiet a while now, but I don't seem to get to a result. First of all, here's the code I'm working with.
PROGRAMPLC_PRGVAR  state:INT:=0;                //StateoftheSM  driver:UDP.UDPDriver;              //UDPDriver  port:UDP.Port;                  //UDPPort    (* The following parameters describe the socket on the SPS *)  src_ipAddr_ud:UDINT;              //SourceIP-AddressasUDINT  src_ipAddr_st:STRING:='192.168.200.99';      //SourceIP-Addressreadable  src_Port:UINT:=5005;              //SourcePort    (* The following parameters describe the destination for outgoing packets *)  dest_ipAddr_ud:UDINT;              //DestinationIP-AddressasUDINT  dest_ipAddr_st:STRING:='192.168.200.165';      //DestinationIP-Addressreadable  dest_Port:UINT:=5005;              //DestinationPort    result:SysTypes.RTS_IEC_RESULT;          //Resultofreceivefunction  bind:UDINT;                  //Resultofbinding  resultCreate:SysTypes.RTS_IEC_RESULT;        //Resultofcreatingtheport  rcvData:ARRAY[0..99]OFBYTE;          //Datawhichwasreceived  sendData:STRING:='HelloWorld';          //Datatobesent    END_VAR
CASEstateOF  0:  //InitialState    resultCreate:=driver.CreatePort(ADR(port));        state:=10;  10:  //BindSocket    src_ipAddr_ud:=UDP.IPSTRING_TO_UDINT(sIPAddress:=src_ipAddr_st);    port.IPAddress:=src_ipAddr_ud;    port.ReceivePort:=src_Port;    port.SendPort:=dest_Port;    bind:=port.Bind(udiIPAddress:=src_ipAddr_ud);    state:=20;  20://Sendpacket    dest_ipAddr_ud:=UDP.IPSTRING_TO_UDINT(sIPAddress:=dest_ipAddr_st);    port.Send(udiIPTo:=dest_ipAddr_ud,pbyData:=ADR(sendData),diDataSize:=SIZEOF(sendData),uiPortTo:=dest_Port);    state:=30;  30:    result:=port.Receive(pbyData:=ADR(rcvData),diDataSize:=SIZEOF(rcvData));    IFresult<>ERRORS.ERR_PENDINGTHEN      state:=40;      END_IF      40://Placeholder  END_CASE
Sending a packet works perfectly fine with that code. Using a python script on the receiver side (the IP participant with the IP Address 192.168.200.165 - let's call him 165 from here on) I receive the string "Hello World". However, the receive function on Codesys doesn't really seem to work.
With a python script I was able to send a simple byte from 165 to my machine. Tracking it with Wireshark was successfull, so it definitely arrived at my computer. The ressource monitor of Windows also lists IP 192.168.200.99, Port 5005 as reserved port for Codesys with no constraint from the firewall. When I use a python script on my computer which simply receives UDP packets, I even receive the packet with this. Still, the Codesys Receive-Function returns with ERR_PENDING.
Does anybody have any idea what I could have possibly missed?
By the way, I have been trying this with the SysSocket library as well and I again got all the way to the point where I want to receive a packet - with that library one can even check whether binding a socket was succesfull and according to Codesys it was - but I just don't receive it.
Grateful for your help,
Alex
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Didn't try your code, but I had good luck with the following FBs of Net Base Services:
NBS.UDP_Peer
NBS.UDP_Send
NBS.UDP_Receive
I think Net Base Services is build around on the UDP library. Don't know if you are supposed to use UDP directly. NBS was quite straight forward for me.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hey there,
I've been trying to receive UDP packets with the UDP library (can be found in CAA Net Base Services) for quiet a while now, but I don't seem to get to a result. First of all, here's the code I'm working with.
Sending a packet works perfectly fine with that code. Using a python script on the receiver side (the IP participant with the IP Address 192.168.200.165 - let's call him 165 from here on) I receive the string "Hello World". However, the receive function on Codesys doesn't really seem to work.
With a python script I was able to send a simple byte from 165 to my machine. Tracking it with Wireshark was successfull, so it definitely arrived at my computer. The ressource monitor of Windows also lists IP 192.168.200.99, Port 5005 as reserved port for Codesys with no constraint from the firewall. When I use a python script on my computer which simply receives UDP packets, I even receive the packet with this. Still, the Codesys Receive-Function returns with ERR_PENDING.
Does anybody have any idea what I could have possibly missed?
By the way, I have been trying this with the SysSocket library as well and I again got all the way to the point where I want to receive a packet - with that library one can even check whether binding a socket was succesfull and according to Codesys it was - but I just don't receive it.
Grateful for your help,
Alex
Didn't try your code, but I had good luck with the following FBs of Net Base Services:
NBS.UDP_Peer
NBS.UDP_Send
NBS.UDP_Receive
I think Net Base Services is build around on the UDP library. Don't know if you are supposed to use UDP directly. NBS was quite straight forward for me.