PDA

View Full Version : UDP Protocol with NETVARUDP_LIB_32.lib



vieirad
05-28-2009, 02:08 PM
First of all Before being able to exchange data via UDP you will have to unable UDP in the Target Settings Screen(TargetSettings.jpg).

Once set the library NETVARUDP_LIB_32.lib will appear in the library manager.
Below is the code you can copy, paste in a program to start communicating via UDP.

You will notice two port 2500 and 3000.
In case of PLC1 if the Send socket Port is Set to 2500
In PLC2 then the Receive Socket Port will be set to 2500

In case of PLC1 if the Receive Socket Port is set to 3000
In PLC2 then the Send Socket Port is set to 3000

If you don't use different port, let say you use only port 3000, what will happen if we loose connection with PLC2 for example then PLC1 receive will read the data of PLC1 transmit.

Hope this code help.

<<<<<<<<<<<<<<<<<<<<< SAMPLE CODE>>>>>>>>>>>>>>>>>>>>>>>>
<INIT SECTION>
PROGRAM SocketConnection
VAR
IdSendSocket: DINT;
IdReceiveSocket: DINT;
aDataToSend: ARRAY[0..9] OF BYTE:= 10,9,8,7,6,5,4,3,2,1;
aDataReceived: ARRAY[0..9] OF BYTE;
NbrOfBytesSent: UDINT;
ClientInfo: CLIENT_REPLY;
END_VAR

<CYCLIC SECTION>
IF init_1 = TRUE THEN
IdSendSocket:= UdpOpenSendSocket(2500);
IF IdSendSocket <> 0 THEN
IdReceiveSocket:= UdpOpenReceiveSocket(3000);
END_IF
END_IF

IF IdSendSocket <> 0 THEN
NbrOfBytesSent:= UdpSendData(IdSendSocket, 2500, '192.168.100.255', ADR(aDataToSend), SIZEOF(aDataToSend));
END_IF


IF IdReceiveSocket <> 0 THEN
ClientInfo:= UdpReceiveData(IdReceiveSocket, 3000, ADR(aDataReceived), SIZEOF(aDataReceived));
END_IF