CANopen_over_Ethercat ¶ Function blocks for CoE (CANopen over EtherCAT) Example: This example shows how to read and write SDO’s with CAN over EtherCAT. It is also demonstrated how to get information about the master and slaves, like the number of active slaves or the state of the slave. As hardware two analog terminals with CAN over EtherCAT are used. For device configuration several startup parameters can be set. For devices which use CAN over EtherCAT an index and sub index is used for each parameter. For each parameter it is possible to read or write the value as number or byte array with a maximum of four bytes (* CAN over EtherCAT from the IODrvEtherCAT lib is used to read and write SDO's. *) PROGRAM CoE VAR Read : ETC_CO_SdoRead ; (* Function block to Read a SDO *) Read4 : ETC_CO_SdoRead4 ; (* Function block to Read a SDO into a byte array.*) ReadDWord : ETC_CO_SdoReadDWord ; (* Function block to Read a SDO as DWORD *) ReadAccess : ETC_CO_SdoRead_Access ; (* Function block to Read a the complete SDO entry into a byte array.*) ReadChannel : ETC_CO_SdoRead_Channel ; (* Function block to Read a the complete SDO entry into a byte array. A channel and priority can be specified additionally.*) Write : ETC_CO_sdoWrite ; (* Function block to write Slave parameters *) Write4 : ETC_CO_sdoWrite4 ; (* Function block to write Slave parameters in a byte array.*) abyRead : ARRAY [1..4] OF BYTE ; (* Read EtherCAT Slave parameter. *) abyReadWritten : ARRAY [1..4] OF BYTE ; (* Read a parameter, which is written with the Write function block. *) abyWrite : ARRAY [1..4] OF BYTE := [12, 34, 124, 80] ; (* write a value with maximum 4 bytes *) dwRead : DWORD ; (* Read a parameter, which was set at the startup. *) uiWrite : UINT := 100 ; (* A parameter to be written with 2 bytes. *) udiWrite : UDINT := 2334567 ; (* A parameter to be written with 4 bytes. *) udiRead : UDINT ; (* Read the parameter with 4 bytes.*) abyReadComplete : ARRAY [1..20] OF BYTE ; (* All slave parameters are read *) abyReadChannel : ARRAY [1..20] OF BYTE ; (* All slave parameters are read *) iState : INT ; (* current state of the state machine *) END_VAR (* Global instance of the IoDrvEtherCAT function block.*) Ethercat_Master (); CASE iState OF 0 : (* wait for the EtherCAT to be ready.*) IF Ethercat_Master.xConfigFinished THEN iState := iState + 1 ; END_IF 1 : (* Read a parameter into a byte array, which was set in the device before startup. The device address is 1002, the usiCom = 1, because the first master is used. The index and subindex can be found under the device at the startup tab.*) Read4 ( xExecute := TRUE , usiCom := 1 , uiDevice := 1002 , wIndex := 16#1C12 , bySubindex := 16#01 , udiTimeOut := 500 ); IF Read4.xDone THEN abyRead := Read4.abyData ; iState := iState + 1 ; Read4 ( xExecute := FALSE ); ELSIF Read4.xError THEN iState := 32767 ; END_IF 2 : (* Read a vthe same parameter as in the last step, but as dword.*) ReadDWord ( xExecute := TRUE , uiDevice := 1002 , wIndex := 16#1C12 , bySubindex := 16#01 , udiTimeOut := 500 ); IF ReadDWord.xDone THEN dwRead := ReadDWord.dwData ; iState := iState + 1 ; ReadDWord ( xExecute := FALSE ); ELSIF Read4.xError THEN iState := 32767 ; END_IF 3 : (* Write a parameter with max. 4 Bytes*) Write4 ( xExecute := TRUE , uiDevice := 1002 , wIndex := 16#4062 , bySubindex := 16#02 , udiTimeOut := 500 , abyData := abyWrite , usiDataLength := 4 ); IF Write4.xDone THEN iState := iState + 1 ; Write4 ( xExecute := FALSE ); ELSIF Write4.xError THEN iState := 32767 ; END_IF 4 : (* Read the parameter, which was written in the last step *) Read4 ( xExecute := TRUE , uiDevice := 1002 , wIndex := 16#4062 , bySubindex := 16#02 , udiTimeOut := 500 ); IF Read4.xDone THEN (* abyReadWritten should have the same value like abyWrite *) abyReadWritten := Read4.abyData ; iState := iState + 1 ; Read4 ( xExecute := FALSE ); ELSIF Read4.xError THEN iState := 32767 ; END_IF 5 : (* Write a parameter, larger than 4 bytes *) Write ( xExecute := TRUE , uiDevice := 1003 , wIndex := 16#F008 , bySubindex := 16#00 , udiTimeOut := 500 , pBuffer := ADR ( udiWrite ), szSize := SIZEOF ( udiWrite ), eMode := ETC_CO_MODE.ETC_CO_AUTO ); IF Write.xDone THEN iState := iState + 1 ; Write ( xExecute := FALSE ); ELSIF Write.xError THEN iState := 32767 ; END_IF 6 : (* Read the parameter, which was written in the last step *) Read ( xExecute := TRUE , uiDevice := 1003 , wIndex := 16#F008 , bySubindex := 16#00 , udiTimeOut := 500 , pBuffer := ADR ( udiRead ), szSize := SIZEOF ( udiRead ) ); IF Read.xDone THEN (* uiReadWritten should have the same value like uiWrite *) iState := iState + 1 ; Read ( xExecute := FALSE ); ELSIF Read.xError THEN iState := 32767 ; END_IF 7 : (* Read all slave parameters into a byte array. Additionally the channel and priority can be set. The output is device specific.*) ReadChannel ( xExecute := TRUE , uiDevice := 1003 , usiCom := 1 , wIndex := 16#801F , bySubindex := 16#00 , udiTimeOut := 500 , pBuffer := ADR ( abyReadChannel ), szSize := SIZEOF ( abyReadChannel ), xCompleteAccess := TRUE , byChannelPriority := 255 ); IF ReadChannel.xDone THEN iState := iState + 1 ; ReadChannel ( xExecute := FALSE ); ELSIF ReadChannel.xError THEN iState := 32767 ; END_IF 8 : (* Read an complete index entry, into a byte array. The output is device specific. *) ReadAccess ( xExecute := TRUE , uiDevice := 1003 , usiCom := 1 , wIndex := 16#801F , bySubindex := 16#00 , udiTimeOut := 500 , pBuffer := ADR ( abyReadComplete ), szSize := SIZEOF ( abyReadComplete ), xCompleteAccess := TRUE ); IF ReadAccess.xDone THEN (* *) iState := iState + 1 ; ReadAccess ( xExecute := FALSE ); ELSIF ReadAccess.xError THEN iState := 32767 ; END_IF 32767 : END_CASE ENUMS ETC_CO_ERROR (Enum) ETC_CO_MODE (Enum) ETC_SDO_INFO_LIST_TYPE (Enum) ETC_SDO_INFO_OBJECT_CODE (Enum) ETC_CO_SdoRead (FunctionBlock) ETC_CO_SdoRead4 (FunctionBlock) ETC_CO_SdoReadDWord (FunctionBlock) ETC_CO_SdoRead_Access (FunctionBlock) ETC_CO_SdoRead_Channel (FunctionBlock) ETC_CO_SdoWrite (FunctionBlock) ETC_CO_SdoWrite4 (FunctionBlock) ETC_CO_SdoWriteDWord (FunctionBlock) ETC_CO_SdoWrite_Access (FunctionBlock) STRUCTS ETC_CO_Emergency (Struct) SdoInfo ETC_CO_SdoInfoGeEntryDescription (FunctionBlock) ETC_CO_SdoInfoGetODList (FunctionBlock) ETC_CO_SdoInfoGetObjectDescription (FunctionBlock)