Example
Based on the configuration described above you can now carry out a test with the following PLC project. In this example the server fetches a handle via ADS for a client variable and writes the value 500 to it. A useful application is shown in conjunction with ADS.
Server:
PROGRAM Server
VAR
state : UINT;
trigger : R_TRIG;
bExecute : BOOL;
bBusy : BOOL;
bError : BOOL;
uErrorID : UDINT;
fbWRI : ADSWRITE;
fbRW : ADSRDWRT;
uTransmit : UDINT := 500;
uHandle : UDINT;
sTest : STRING := 'MAIN.uReceive';
END_VAR
trigger(CLK := bExecute);
IF trigger.Q THEN
state := 1;
bBusy := TRUE;
END_IF
CASE state OF
0: (* Wait state *)
bBusy := FALSE;
1:
fbRW( WRTRD := FALSE);
fbRW( NETID := '172.16.2.94.2.1',
PORT := 801,
IDXGRP := 16#F003,
IDXOFFS := 16#0000,
WRITELEN := LEN(sTest)+1,
READLEN := SIZEOF(uHandle),
SRCADDR := ADR(sTest),
DESTADDR := ADR(uHandle),
WRTRD := TRUE,
TMOUT := t#5s,
BUSY => ,
ERR => bError,
ERRID => uErrorID);
state := 2;
2:
fbRW( WRTRD := FALSE);
IF NOT fbRW.BUSY THEN
IF NOT fbRW.ERR THEN
state := 3;
ELSE
state := 100;
END_IF
END_IF
3:
fbWRI( WRITE := FALSE);
fbWRI( NETID := '172.16.2.94.2.1',
PORT := 801,
IDXGRP := 16#F005,
IDXOFFS := uHandle,
LEN := SIZEOF(uTransmit),
SRCADDR := ADR(uTransmit),
WRITE := TRUE,
TMOUT := t#5s,
BUSY => ,
ERR => bError,
ERRID => uErrorID);
state := 4;
4:
fbWRI(WRITE := FALSE);
IF NOT fbWRI.BUSY ANDNOT fbWRI.ERR THEN
state := 0;
ELSIF fbWRI.ERR THEN
state := 100;
END_IF
100:
; (* Error *)
END_CASE
Client:
PROGRAM Client
VAR
uReceive : UDINT;
END_VAR
|