FB_CoEWrite

FB_CoEWrite 1:

With the function block FB_CoEWrite, an object from the object directory of an EtherCAT slave can be written via an SDO (Service Data Object) download. This requires the slave to have a mailbox and to support the CoE (CANopen over EtherCAT) protocol. With the help of the SubIndex and Index parameters a selection is made as to which object should be written. Via CompleteAccess := TRUE the parameter can be written with sub-elements.

FB_CoEWrite 2: Inputs

VAR_INPUT
   NetId          : T_AmsNetID;(*netID of PC with NC*)
   Index          : WORD;(*CoE object index*)
   SubIndex       : BYTE;(*CoE sub index*)
   pSrcBuf        : PVOID;(*Contains the address of the buffer containing the data to be send*)
   BufLen         : UDINT;(*Contains the max. number of bytes to be received*)
   Execute        : BOOL;(*Function block execution is triggered by a rising edge at this input.*)
   Timeout        : TIME := DEFAULT_ADS_TIMEOUT;(*States the time before the function is cancelled.*)
   CompleteAccess : BOOL;(*Function block reads the complete object with all sub index*)
END_VAR

Name

Type

Description

NetId

T_AmsNetID

String which contains the AMS Network ID of the PC (type: T_AmsNetId).

Index

WORD

Index of the object that is supposed to be written.

SubIndex

BYTE

Subindex of the object that is supposed to be written.

pDstBuf

PVOID

Address (pointer) to the transmit buffer

BufLen

UDINT

Amount of data to be sent in bytes

Execute

BOOL

The function block is enabled via a positive edge at this input.

Timeout

TIME

Maximum time allowed for the execution of the function block.

CompleteAccess

BOOL

The complete object can be accessed all at once via Complete Access.

FB_CoEWrite 3:

Index and SubIndex can be taken from the corresponding drive documentation. For the AX8000 from Beckhoff see AX8000 object description.

FB_CoEWrite 4: Inputs/outputs

VAR_IN_OUT
   Axis : AXIS_REF;
END_VAR

Name

Type

Description

Axis

AXIS_REF

Axis data structure that unambiguously addresses an axis in the system. Among other parameters it contains the current axis status, including position, velocity or error state.

FB_CoEWrite 5: Outputs

VAR_OUTPUT
    Busy         : BOOL;
    Error        : BOOL;
    AdsErrId     : UINT;
    CANopenErrId : UINT;
END_VAR

Name

Type

Description

Busy

BOOL

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

Error

BOOL

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

AdsErrId

UINT

In the event of a set error output returns the ADS error code.

CANopenErrId

UINT

In the event of a set error output returns the CANopen error code.

Example for implementation in ST:

PROGRAM MAIN
VAR
    fbCoEWrite     : FB_CoEWrite;
    NetId          : T_AmsNetID := ''; 
    Index          : WORD := 16#1018;
    SubIndex       : BYTE := 1; 
    Execute        : BOOL := TRUE;
    Timeout        : TIME := T#5S;
    CompleteAccess : BOOL := TRUE;
    Axis           : AXIS_REF;
    vendorId       : UDINT := 2;
    Error          : BOOL;
    AdsErrId       : UDINT;
    CANopenErrId   : UDINT;END_VAR

fbCoEWrite(
    NetId          := NetId, 
    Index          := Index, 
    SubIndex       := SubIndex, 
    pSrcBuf        := ADR(vendorId), 
    BufLen         := SIZEOF(vendorId), 
    Execute        := Execute, 
    Timeout        := Timeout, 
    CompleteAccess := CompleteAccess, 
    Axis := Axis
);

IF NOT fbCoEWrite.Busy THEN
    Error        := fbCoEWrite.Error;
    AdsErrId     := fbCoEWrite.AdsErrId;
    CANopenErrId :=fbCoEWrite.CANopenErrId;
    Execute      := FALSE;
    fbCoEWrite(Execute := Execute, Axis := Axis);
END_IF