Example with AdsRead function block
The example demonstrates the use of the ADSREAD function block in an ADS Client application.
The complete sources of the ADS Client application can be unpacked here: AdsReadReqClientExample.zip
Declaration Part
PROGRAM MAIN
VAR
fbReadReq : ADSREADEX := ( NETID := '', PORT := 851, TMOUT := DEFAULT_ADS_TIMEOUT );
bIncrement : BOOL;(* Rising edge at this variable starts command execution *)
bDecrement : BOOL;(* Rising edge at this variable starts command execution *)
bReset : BOOL;(* Rising edge at this variable starts command execution *)
bOther : BOOL;(* Rising edge at this variable starts command execution *)
nState : BYTE;
bBusy : BOOL;
bError : BOOL;
nErrID : UDINT;
cbRead : UDINT;
nCounter : INT;(* Server data to be read *)
END_VAR
Implementation
CASE nState OF
0:
IF bIncrement OR bDecrement OR bReset OR bOther THEN
bBusy := TRUE;
bError := FALSE;
nErrID := 0;
fbReadReq( READ := FALSE );
IF bIncrement THEN(* Incement counter value *)
bIncrement := FALSE;
fbReadReq( IDXGRP := 16#80000001, IDXOFFS := 0, LEN := SIZEOF(nCounter), DESTADDR := ADR(nCounter), READ := TRUE );
ELSIF bDecrement THEN(* Decrement counter value *)
bDecrement := FALSE;
fbReadReq( IDXGRP := 16#80000002, IDXOFFS := 0, LEN := SIZEOF(nCounter), DESTADDR := ADR(nCounter), READ := TRUE );
ELSIF bReset THEN(* Reset counter value *)
bReset := FALSE;
fbReadReq( IDXGRP := 16#80000003, IDXOFFS := 0, LEN := SIZEOF(nCounter), DESTADDR := ADR(nCounter), READ := TRUE );
ELSIF bOther THEN(* Call unsupported function *)
bOther := FALSE;
fbReadReq( IDXGRP := 16#80000004, IDXOFFS := 0, LEN := SIZEOF(nCounter), DESTADDR := ADR(nCounter), READ := TRUE );
END_IF
nState := 1;
END_IF
1:
fbReadReq( READ := FALSE, BUSY=>bBusy, ERR=>bError, ERRID=>nErrID, COUNT_R=>cbRead );
IF NOT bBusy THEN
IF NOT bError THEN
nState := 0;(* Success *)
ELSE
nState := 100;(* Error *)
END_IF
END_IF
100:(* TODO::Implement error handler *)
nState := 0;
END_CASE