Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Receive UDP Packets via Port.Receive

alex-j
2019-06-05
2019-06-06
  • alex-j - 2019-06-05

    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.

    PROGRAM PLC_PRG
    VAR
       state :INT := 0;                        // State of the SM
       driver : UDP.UDPDriver;                     // UDP Driver
       port : UDP.Port;                           // UDP Port
       
       (* The following parameters describe the socket on the SPS *)
       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
       
       (* The following parameters describe the destination for outgoing packets *)
       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] OF BYTE;               // Data which was received 
       sendData : STRING := 'Hello World';               // Data to be sent
       
       END_VAR
    
    CASE state OF
       0:   // Initial State
          resultCreate:=driver.CreatePort(ADR(port));      
          state:=10;
       10:   // Bind Socket
          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: // Send packet
          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));
          IF result<>ERRORS.ERR_PENDING THEN
             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

     
  • jago85 - 2019-06-06

    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.

     

Log in to post a comment.