Sample
The use of individual functions can look like this. In this sample, the ApplicationSpecificTag is written via Write and then read and checked via Read method.
The following function block is part of the IoLink sample project. For more information, see the chapter Samples.
FUNCTION_BLOCK FB_Sample_03_IolSlave
VAR_INPUT
bExecute : BOOL := FALSE;
sApplicationTag : STRING(32) := '';
END_VAR
VAR_OUTPUT
bBusy : BOOL := FALSE;
bDone : BOOL := FALSE;
bError : BOOL := FALSE;
nAdsError : E_AdsErr := E_AdsErr.NOERR;
nIolError : E_IolError := E_IolError.NoError;
sErrorTxt : STRING(255) := '';
END_VAR
VAR
bExecuteOld : BOOL;
nState : INT := 0;
fbIolSlave : FB_IolSlave;
bInitDone : BOOL := FALSE;
hr : HRESULT;
sApplicationTagRead : STRING(32) := ''
END_VAR
////
// Init
//
IF NOT bInitDone THEN
bInitDone := TRUE;
fbIolSlave.MasterAoeNetId := F_CreateAmsNetId(GVL_IO.sTerm2AmsNetId);
fbIolSlave.Port := GVL_IO.nTerm2EPI1008;
fbIolSlave.Name := 'EPI1008';
END_IF
////
// Execute/Busy State Machine
//
IF bExecute AND NOT bExecuteOld AND NOT bBusy THEN
bBusy := TRUE;
nState := 10; // WRITE
ELSIF NOT bExecute THEN
bBusy := FALSE;
bError := FALSE;
bDone := FALSE;
nAdsError := E_AdsErr.NOERR;
nIolError := E_IolError.NoError;
sErrorTxt := '';
END_IF
bExecuteOld := bExecute;
////
// FB State Machine
//
CASE nState OF
// IDLE
0:;
// WRITE
10:
hr := fbIolSlave.Write(16#0018, 0, ADR(sApplicationTag), 32);
IF NOT F_IolIsBusy(hr) THEN
IF FAILED(hr) THEN
bBusy := FALSE;
bError := TRUE;
sErrorTxt := fbIolSlave.ErrorTxt;
nAdsError := F_IolGetAdsError(hr);
nIolError := F_IolGetIolError(hr);
nState := 0; // IDLE;
ELSE
nState := 20; // READ;
END_IF
END_IF
// READ
20:
hr := fbIolSlave.Read(16#0018, 16#00, ADR(sApplicationTagRead), 32);
IF NOT F_IolIsBusy(hr) THEN
IF FAILED(hr) THEN
bBusy := FALSE;
bError := TRUE;
sErrorTxt := fbIolSlave.ErrorTxt;
nAdsError := F_IolGetAdsError(hr);
nIolError := F_IolGetIolError(hr);
nState := 0; // IDLE;
ELSE
nState := 30; // CHECK;
END_IF
END_IF
// CHECK
30:
IF sApplicationTag <> sApplicationTagRead THEN
bBusy := FALSE;
bError := TRUE;
sErrorTxt := 'sApplicationTag and sApplicationTagRead are not equal!';
ELSE
bBusy := FALSE;
bDone := TRUE;
nState := 0;
END_IF
END_CASE
////
// call FBs
//
fbIolSlave();