Udp and multicast

2017-03-21
2018-01-16
  • 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. 239.2.3.4).
    I'm able to send data to a "normal" IP address like 192.168.10.25 but not multicast or broadcast. I don't know what I wrong. I am new in Codesys v3.

    declarations:

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

    by using;

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

    UdpPeer returns "NO_ERROR" but UdpSend returns "UDP_SEND_ERROR"

    Instead, by using:

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

    UdpPeer status returns "WRONG_PARAMETER" .
    Same results by setting or ipAddr = '255.255.255.255' or '0.0.0.0' (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   := '255.255.255.255'; (* or 127.0.0.1 or other non multicast IP address *)
    PeerIpPort         := 49999; (* or other port not used *)
    SenderIpAddr.sAddr := '239.2.3.4';
    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;
    END_IF
    RTR_Send(CLK:=OneShotCmd); (* check rise of send command *)
    IF RTR_Send.Q AND UdpPeer.xActive AND NOT UdpPeer.xError THEN
       Send   := TRUE;
    END_IF
       
    (* UPD sender *)
    UdpSend(
       xExecute   := Send, 
       ipAddr   := SenderIpAddr,
       uiPort   := SenderIpPort,
       hPeer   := UdpPeer.hPeer,
       szSize   := SIZEOF(DataTx),
       pData   := ADR(DataTx),
       udiTimeOut   := 1000000, (* usec *)
    );
       
    IF UdpSend.xDone THEN
       Send   := FALSE;   
    END_IF
    IF UdpSend.xError THEN
       Send   := FALSE;
       UdpSend(xExecute:=FALSE);   
    END_IF
       
    

    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.