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 package from broadcast

TiakTiak
2020-09-28
2021-03-03
  • TiakTiak - 2020-09-28

    Hello
    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

    VAR_GLOBAL
        gc_IP_SendTo : NBS.IP_ADDR := (sAddr := '192.168.15.67'); // My PLC's Ip address
        gc_uiPort : UINT := 10110; 
    END_VAR
    
    PROGRAM POU
    VAR
        Peer : nbs.UDP_Peer;
        Send : nbs.UDP_Send;
        Receive : nbs.UDP_Receive;
        xFirstPacket : BOOL;
        xOldActive : BOOL;
        udpData : UDPData;
    END_VAR
    
    Peer(xEnable := TRUE, ipAddr := gc_IP_SendTo, uiPort := gc_uiPort);
    IF Peer.xActive AND NOT xOldActive THEN
        xFirstPacket := TRUE;
    END_IF
    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;
    END_IF
    
    IF Peer.xError THEN //Connection closed
        Peer (xEnable := FALSE);
        Send(xExecute := FALSE);
        Receive(xEnable := FALSE);
    END_IF
    
     
  • Chris.O - 2020-09-29

    sure it should be 255.255.255.255 and not 255.255.255.0

    With 255.255.255.255 you have limited broadcast

     
  • asivakov - 2021-03-03

    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

     

    Last edit: asivakov 2021-03-03

Log in to post a comment.