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;                // State of the SM
  driver : UDP.UDPDriver;              // UDP Driver
  port : UDP.Port;                  // UDP Port
 Â
  (*ThefollowingparametersdescribethesocketontheSPS*)
  src_ipAddr_ud : UDINT;              // Source IP-Address as UDINT
  src_ipAddr_st : STRING :='192.168.200.99';      // Source IP-Address readable
  src_Port : UINT :=5005;              // Source Port
 Â
  (*Thefollowingparametersdescribethedestinationforoutgoingpackets*)
  dest_ipAddr_ud : UDINT;              // Destination IP-Address as UDINT
  dest_ipAddr_st : STRING :='192.168.200.165';      // Destination IP-Address readable
  dest_Port : UINT :=5005;              // Destination Port
 Â
  result : SysTypes.RTS_IEC_RESULT;          // Result of receive function
  bind : UDINT;                  // Result of binding
  resultCreate : SysTypes.RTS_IEC_RESULT;        // Result of creating the port
  rcvData : ARRAY[0..99] OFBYTE;          // Data which was received
  sendData : STRING :='Hello World';          // Data to be sent
 Â
  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.