Join us today!
Read and Write CoE paramaters
If you need to read CoE parameters from an EtherCAT Slave, you can use the FB_EcCoESdoRead function block and to write FB_EcCoESdoWrite function block can be used. The EtherCAT Slave must support 'CANopen over EtherCAT' (CoE) protocol to be able to use these function blocks.
In this tutorial, we will read the 'Analog Input Limit 1' value of channel 2 from EL3002 2 channels analog input terminal and then write a new limit value to CoE parameter.
Add the Tc2_EtherCAT library into the project.
Add the code below in MAIN program and build the solution.
PROGRAM MAIN VAR fbEcCoESdoRead : FB_EcCoESdoRead; bReadAnalogInputLimit1 : BOOL; fbEcCoESdoWrite : FB_EcCoESdoWrite; bWriteAnalogInputLimit1 : BOOL; SlaveNetID AT %I* : T_AmsNetIdArr; (* AMS Net ID EL3002 *) SlavePort AT %I* : WORD; (* Port number EL3002 *) fAnalogInputLimit1 : REAL := 0; (* Analog Input Limit 1 *) bReadError : BOOL; nReadErrId : UDINT; bWriteError : BOOL; nWriteErrId : UDINT; tTimeout : TIME := DEFAULT_ADS_TIMEOUT; END_VAR (* Read the Analog Input Limit 1 CoE parameter *) fbEcCoESdoRead( sNetId:= F_CreateAmsNetId(SlaveNetID), nSlaveAddr:= SlavePort, nSubIndex:= 13, (* Subindex of CoE Object *) nIndex:= 16#8010 , (* CoE Object Index *) pDstBuf:= ADR(fAnalogInputLimit1), cbBufLen:= SIZEOF(fAnalogInputLimit1), bExecute:= bReadAnalogInputLimit1, tTimeout:= tTimeout, bBusy=> , bError=> bReadError, nErrId=> nReadErrId, cbRead=> ); (* Write to Analog Input Limit 1 CoE parameter *) FB_EcCoESdoWrite( sNetId:= F_CreateAmsNetId(SlaveNetID), nSlaveAddr:=SlavePort , nSubIndex:=13, (* Subindex of CoE Object *) nIndex:=16#8010 , (* CoE Object Index *) pSrcBuf:=ADR(fAnalogInputLimit1) , cbBufLen:=SIZEOF(fAnalogInputLimit1) , bExecute:= bWriteAnalogInputLimit1,(* TRUE activate writing *) tTimeout:= tTimeout, bBusy=> , bError=> , nErrId=> ); IF NOT FB_EcCoESdoWrite.bBusy THEN bWriteAnalogInputLimit1 := FALSE; IF NOT FB_EcCoESdoWrite.bError THEN (* write successful *) bWriteError := FALSE; nWriteErrId := 0; ELSE (* write failed *) bWriteError := FB_EcCoESdoWrite.bError; nWriteErrId := FB_EcCoESdoWrite.nErrId; END_IF FB_EcCoESdoWrite(bExecute := FALSE); END_IF
After the build, link the SlavePort and SlaveNetID to terminal's netID and port, which located under the InfoData.AddsAddr of the terminal.
To find the index and the subindex values of CoE object, double click on the terminal and go to 'CoE-Online' tab. Find the CoE object and note the index and subindex values. For this tutorial, 'Analog input limit 1' of channel 2 is used and the index value of this CoE object is '16#8010' and the subindex is '13' as it can be seen below:
Toggle the bReadAnalogInputLimit1 to read the current 'Analog input limit 1'. Set the new desired value by writing the value to fAnalogInputLimit1 variable and set the bWriteAnalogInputLimit1 variable to TRUE. You can toggle again the bReadAnalogInputLimit1 to ensure new limit is written into the CoE object.
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
- 17 Forums
- 265 Topics
- 932 Posts
- 2 Online
- 688 Members