FB_CoERead

FB_CoERead 1:

The function block FB_CoERead allows data to be read from an object directory of an EtherCAT slave through an SDO (Service Data Object) access. 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 read out. Via CompleteAccess := TRUE the parameter can be read with sub-elements.

FB_CoERead 2: Inputs

VAR_INPUT
   NetId          : T_AmsNetID;(*netID of PC with NC*)
   Index          : WORD;(*CoE object index*)
   SubIndex       : BYTE;(*CoE sub index*)
   pDstBuf        : PVOID;(*Contains the address of the buffer for the received data*)
   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 to be read.

SubIndex

BYTE

Subindex of the object that is to be read.

pDstBuf

PVOID

Address (pointer) to the receive buffer

BufLen

UDINT

Maximum available buffer size (in bytes) for the data to be read

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

Via Complete Access the complete object can be accessed at once.

FB_CoERead 3:

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

FB_CoERead 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_CoERead 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
    fbCoERead         : FB_CoERead;
    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

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

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