FB_EcCoeSdoWrite

FB_EcCoeSdoWrite 1:

The FB_EcCoeSdoWrite function block permits an object from the object directory of an EtherCAT slave to be written by means of an SDO download. This requires the slave to have a mailbox and to support the "CANopen over EtherCAT" (CoE) protocol. The nSubIndex and nIndex parameters select the object the data should be written to.

VAR_INPUT

VAR_INPUT
    sNetId      : T_AmsNetId; 
    nSlaveAddr  : UINT; 
    nSubIndex   : BYTE; 
    nIndex      : WORD;
    pSrcBuf     : DWORD; 
    cbBufLen    : UDINT; 
    bExecute    : BOOL;
    tTimeout    : TIME := DEFAULT_ADS_TIMEOUT; 
END_VAR

sNetId: This is a string that contains the AMS network identifier of the EtherCAT master device.

nSlaveAddr: Fixed address of the EtherCAT slave to which the SDO download command should be sent.

nSubIndex:Sub-index of the object that should be written to.

nIndex: Index of the object that should be written to.

pSrcBuf:Address (pointer) of the send buffer.

cbBufLen:Number (in bytes) of data to be sent.

bExecute: The block is activated by a rising edge at this input.

tTimeout: Maximum time allowed for the execution of the function block.

VAR_OUTPUT

VAR_OUTPUT
    bBusy       : BOOL;
    bError      : BOOL;
    nErrId      : UDINT;
END_VAR

bBusy: This output is set when the function block is activated and remains set until an acknowledgement is received.

bError: This output is set up after the bBusy output has been reset if there has been an error in transmission of the command.

nErrId: Supplies the ADS error code associated with the most recently executed command if the bError output is set.

Example of an implementation in ST:

PROGRAM TEST_SdoWrite

VAR
    fbSdoWrite      : FB_EcCoESdoWrite;
    sNetId          : T_AmsNetId := '172.16.2.131.2.1'; (* NetId of EtherCAT Master *)
    nSlaveAddr      : UINT := 1005; (* Port Number of EtherCAT Slave *)
    nIndex          : WORD := 16#4062; (* CoE Object Index *)
    nSubIndex       : BYTE := 1; (* Subindex of CoE Object *)
    nValue          : UINT := 2; (* variable to be written to the CoE Object *)
    bExecute        : BOOL; (* rising edge starts writing to the CoE Object *)
    bError          : BOOL;
    nErrId          : UDINT;
END_VAR


fbSdoWrite(
    sNetId      := sNetId,
    nSlaveAddr  := nSlaveAddr,
    nIndex      := nIndex,
    nSubIndex   := nSubIndex,
    pSrcBuf     := ADR(nValue),
    cbBufLen    := SIZEOF(nValue),
    bExecute    := bExecute
);


IF NOT fbSdoWrite.bBusy THEN
    bExecute := FALSE;
    IF NOT bError THEN 
        (* write successful *)
        bError := FALSE;
        nErrId := 0;
    ELSE 
        (* write failed *)
        bError := fbSdoWrite.bError;
        nErrId := fbSdoWrite.nErrId;
    END_IF

    fbSdoWrite(bExecute := FALSE);
END_IF

 

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v2.10.0 or higher

PC or CX (x86)

TcEtherCAT.lib
( Standard.Lib; TcBase.Lib; TcSystem.Lib, TcUtilities.Lib are included automatically )

TwinCAT v2.10.0 Build >= 1301 or higher

CX (ARM)