FB_IolWrite

FB_IolWrite 1:

Function block for writing IO-Link parameters in a non-object-oriented solution.

FB_IolWrite 2:Inputs

Name

Type

Description

bExecute

BOOL

Rising edge to start the function block

sNetid

T-AmsNetid

AMS NETID of the IO-Link master

nlolPort

E_lolPort

IO-Link port of the IO-Link device

nIndex

WORD

Index of the IO-Link device parameter

nSubindex

BYTE

Subindex of the IO-Link device parameter

pSCRBuf

PVOID

Pointer to source buffer

cbBufLen

UDINT

Size of the data bytes to be read

tTimeout

TIME

Maximum allowed execution time

FB_IolWrite 3:Outputs

Name

Type

Description

bBusy

BOOL

Busy flag of the function block

bDone

BOOL

Done flag of the function block

bError

BOOL

Error flag of the function block

hResult

HRESULT

Return value of this function block

nADSError

E_AdsErr

ADS error code

nIolError

E_IolError

IO-Link error code

Sample

The following function block is part of the IoLink sample project. For more information, see the chapter Samples.

FUNCTION_BLOCK FB_Sample_02_IolWrite
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;
    fbIolWrite              : FB_IolWrite;
END_VAR
////
// 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:
    sApplicationTag := '';
    
    fbIolWrite(bExecute := FALSE);
    fbIolWrite.sNetId := F_CreateAmsNetId(GVL_IO.sTerm2AmsNetId);
    fbIolWrite.nIolPort := GVL_IO.nTerm2EPI1008;
    fbIolWrite.pSRCBuf := ADR(sApplicationTag);
    fbIolWrite.cbBufLen := INT_TO_UDINT(LEN(sApplicationTag));
    fbIolWrite.nIndex := 16#18;
    fbIolWrite.nSubindex := 0;
    fbIolWrite.bExecute := TRUE;
    
    nState := 20; // WAIT_FOR_DONE
    
// WAIT_FOR_DONE
20:
    IF fbIolWrite.bDone THEN
        bDone := TRUE;
        bBusy := FALSE;
        nState := 0; // IDLE
    END_IF
END_CASE

////
// call FBs
//
fbIolWrite();

////
// Error Handling
//
IF NOT bError AND bBusy THEN
    IF fbIolWrite.bError THEN
        nState := 0;
        bBusy := FALSE;
        bError := TRUE;
        nAdsError := fbIolWrite.nAdsError;
        nIolError := fbIolWrite.nIolError;
        sErrorTxt := F_IolGetErrorTxt(fbIolWrite.hresult);
    END_IF
END_IF

Requirements

Development environment

Required PLC library

TwinCAT from v3.1.4024.25

Tc3_IoLink from v3.3.4.0