#5 UDP Send and Receive with Net Base Services SP16

Unlicensed
2020-05-15
2020-05-15
i-campbell
No

Requries Library: NetBaseSrv = Net Base Services, 3.5.16.0 using the Namespace NBS

PROGRAM PLC_PRG
VAR
    //Control
    xStart : BOOL; //Use this to toggle the system ON
    xSend  : BOOL; //Send data one time. needs a false to true transition

    //Settings
    uiSendToPort : UINT := 6454;
    uiReceivePort : UINT := 6454;
    theirIP : NBS.IPv4Address := (ipAddress := '10.0.0.10');
    myIP : NBS.IPv4Address := (ipAddress := '10.0.0.9');
    MySendData : ARRAY [0..529] OF BYTE := [TO_BYTE('A'),TO_BYTE('r'),TO_BYTE('t'),TO_BYTE('-'),
                                       TO_BYTE('N'),TO_BYTE('e'),TO_BYTE('t'),00,
                                       00,16#50,0,14,
                                       0,0,0,0,
                                       0,2,
                                       512(0)]; //SOME RANDOM DATA
    NumberOfBytesToSend : UDINT := 20;

    //Receive Data
    MyReceiveData : ARRAY [0..529] OF BYTE;
    WhoSentThisData : NBS.IIPAddress;

    //Just some function Blocks
    myUDP_Peer : NBS.UDP_Peer;
    myUDP_Send : NBS.UDP_Send;
    myUDP_Receive : NBS.UDP_Receive;
END_VAR
myUDP_Peer(
    xEnable:= xStart, 
    xBusy=> , 
    xError=> , 
    eErrorID=> , 
    itfAsyncProperty:= , 
    itfIPAddress:= myIP, 
    uiPort:= uiReceivePort, 
    itfMulticast:= , 
    xActive=> , 
    itfPeer=> );

myUDP_Send(
    xExecute:= xSend, 
    udiTimeOut:= 1000000, // 1second timeout
    xDone=> , 
    xBusy=> , 
    xError=> , 
    eErrorID=> , 
    itfTSNContext:= , 
    itfPeer:= myUDP_Peer.itfPeer, 
    itfIPAddress:= theirIP, 
    uiPort:= uiSendToPort, 
    pData:= ADR(MySendData), 
    udiSize:= MIN(NumberOfBytesToSend,SIZEOF(MySendData)), 
    udiCount=> );

xSend := FALSE;

myUDP_Receive(
    xEnable:= myUDP_Peer.xActive, 
    xBusy=> , 
    xError=> , 
    eErrorID=> , 
    itfPeer:= myUDP_Peer.itfPeer, 
    pData:= ADR(MyReceiveData), 
    udiSize:= SIZEOF(MyReceiveData), 
    xReady=> , 
    itfIPAddressFrom=> , 
    uiPortFrom=> , 
    udiCount=> );

Discussion


Log in to post a comment.