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 allow the object that is to be written to be selected. The function block FB_EcCoeSdoWriteEx must be used for access to the complete parameter, including subelements.

FB_EcCoeSdoWrite 2: Inputs

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

Name

Type

Description

sNetId

T_Ams
NetId

String containing the AMS network ID of the EtherCAT master device. (Type: T_AmsNetId)

nSlave
Addr

UINT

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

nSubIndex

BYTE

Subindex of the object that is supposed to be written.

nIndex

WORD

Index of the object that is supposed to be written.

pSrcBuf

PVOID

Address (pointer) to the transmit buffer

cbBufLen

UDINT

Number of date to be sent in bytes

bExecute

BOOL

The function block is activated by a positive edge at this input.

tTimeout

TIME

Maximum time allowed for the execution of the function block.

FB_EcCoeSdoWrite 3: Outputs

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

Name

Type

Description

bBusy

BOOL

This output is set when the function block is activated, and remains set until a feedback is received.

bError

BOOL

This output is set after the bBusy output has been reset when an error occurs in the transmission of the command.

nErrId

UDINT

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 fbSdoWrite.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 platform

PLC libraries to include

TwinCAT v3.1.0

PC or CX (x86, x64, ARM)

Tc2_EtherCAT