Udp and multicast

  • tcastagnoli

    tcastagnoli - 2017-03-21

    Hi all,
    I'm trying to send data by UDP sockets but I get always error with multicast IP addresses (i.e.
    I'm able to send data to a "normal" IP address like but not multicast or broadcast. I don't know what I wrong. I am new in Codesys v3.


    IP_Addr:   NBS.IP_ADDR := (sAddr := '');
    IP_Multicast:   NBS.IP_ADDR := (sAddr := '');
    IP_Port:      UINT := 2053; 
    UdpPeer:      nbs.UDP_Peer;
    UdpSend:      nbs.UDP_Send;

    by using;

       xEnable   := TRUE,
       ipAddr   := IP_Multicast,
       uiPort   := IP_Port   );

    UdpPeer returns "NO_ERROR" but UdpSend returns "UDP_SEND_ERROR"

    Instead, by using:

       xEnable   := TRUE,
       (*ipAddr   := IP_Addr,*)
            ipMultiCast   := IP_Multicast,
       uiPort   := IP_Port   );

    UdpPeer status returns "WRONG_PARAMETER" .
    Same results by setting or ipAddr = '' or '' (see library doc)

    Has someone an idea ?
    Many thanks in advance

  • tcastagnoli

    tcastagnoli - 2017-03-22

    I solved (partially?) the issue by differentiating the ip address and port of Peer and sender.

    PeerIpAddr.sAddr   := ''; (* or or other non multicast IP address *)
    PeerIpPort         := 49999; (* or other port not used *)
    SenderIpAddr.sAddr := '';
    SenderIpPort      := 2053;
    RTR:   R_Trig;
    Send:    BOOL;
    DataTx:   STRING:='Hello Multicast !!';
    UdpPeer(xEnable := TRUE, ipAddr := PeerIpAddr, uiPort := PeerIpPort);
    IF UdpPeer.xError THEN
       UdpPeer(xEnable := FALSE);
       Send         := FALSE;
    RTR_Send(CLK:=OneShotCmd); (* check rise of send command *)
    IF RTR_Send.Q AND UdpPeer.xActive AND NOT UdpPeer.xError THEN
       Send   := TRUE;
    (* UPD sender *)
       xExecute   := Send, 
       ipAddr   := SenderIpAddr,
       uiPort   := SenderIpPort,
       hPeer   := UdpPeer.hPeer,
       szSize   := SIZEOF(DataTx),
       pData   := ADR(DataTx),
       udiTimeOut   := 1000000, (* usec *)
    IF UdpSend.xDone THEN
       Send   := FALSE;   
    IF UdpSend.xError THEN
       Send   := FALSE;

    Problem: it is the right way ? Is there any guidelines for using these libraries ?

  • tcastagnoli

    tcastagnoli - 2017-03-31

    OK I've fixex the data transmission. You Don't need to separate IPaddr. and IPport of Peer and Sender. You only need to setup in the right way the Gateway of the device.

  • tcastagnoli

    tcastagnoli - 2017-03-31

    The question is now:
    how can i read the UDP multicast/broadcast data sent from other devices ?

    Is it possible in codesysV3 ?

  • e.kislov

    e.kislov - 2018-01-16

    Dear Edwin, can you help with simple example UDP Multicast? (Sender/Receiver)

    Another topic with similar situation:
    https://forum-de.codesys.com/viewtopic. ... 324#p17085

    It would be nice to get simple example (for example, for Raspberry Pi) to undestand how it works in CDS V3.


Log in to post a comment.