Example program R/W CoE

Program description/ function:

After starting this example program, by setting TRUE of the variable startRead or startWrite, read or write access from/to the CoE directory of a particular EtherCAT participant can be made.

Notes:

This example writes into the CoE object 0x8000 with sub index 0x16 the value 22 and activates thereby filter 1 to „IIR Butterw. LP 5th Ord. 1000 Hz“, or reads out the internal temperature value of the EL3751 terminal from CoE object 0x9000, Sub‑Index 0x01. The program variable for writing has already been assigned the value in the variable declaration. For test of writing the CoE directory can be reviewed and the variable (int16Buffer) can be watched to check reading.

Download: Programmlink

Preparations for starting the sample programs (tnzip file / TwinCAT 3)

  • Click on the download button to save the Zip archive locally on your hard disk, then unzip the *.tnzip archive file in a temporary folder.
  • Example program R/W CoE 1:
    Opening the *. tnzip archive
  • Select the .tnzip file (sample program).
  • A further selection window opens. Select the destination directory for storing the project.
  • For a description of the general PLC commissioning procedure and starting the program please refer to the terminal documentation or the EtherCAT system documentation.

Declaration (ST)

PROGRAM MAIN
VAR
   fb_coe_write          : FB_EcCoESdoWrite; // Function Block for writing in CoE
   fb_coe_read           : FB_EcCoESdoRead;  // Function Block for reading from CoE
   userNetId             : T_AmsNetId := 'a.b.c.d.4.1'; // Have to be entered
   userSlaveAddr         : UINT := 1002;     // Have to be entered
   startWrite            : BOOL := FALSE;    // Sign for start writing
   startRead             : BOOL := FALSE;    // Sign for start reading
   nState                : BYTE := 0;        // RW-status
   // Example: Read EL3751 PAI Internal Data: Temperature Value
   int16Buffer           : INT;              // Buffer for reading
   // Example: Select EL3751 Filter1: 22 = IIR Butterw. LP 5th Ord. 1000 Hz:
   uint16Buffer          : UINT:=22;         // Buffer for writing
   bTxPDOState AT%I*     : BOOL;             // (PDO for synchronization)
END_VAR

Implementation (ST):

CASE nState OF
0:
   IF startWrite THEN
      // Prepare CoE-Access: Write value of CoE object/ sub index:
      fb_coe_write(bExecute := FALSE);
      nState := 1;// Next state for writing
      startWrite := FALSE;
   END_IF
   IF startRead THEN
      // Prepare CoE-Access: Read value of CoE object/ sub index:
      fb_coe_read(bExecute := FALSE);
      nState := 11;// Next state for reading
      startRead := FALSE;
   END_IF
// ============== COE WRITE ================
1:
   // Write entry
   fb_coe_write(
   sNetId:= userNetId,
   nSlaveAddr:= userSlaveAddr,
   nSubIndex:= 16#16,
   nIndex:= 16#8000,
   pSrcBuf:= ADR(uint16Buffer),
   cbBufLen:= 2,
   bExecute:= TRUE,
   tTimeout:= T#1S
   );
   nState := nState + 1; // Next state
2:
   fb_coe_write();       // Execute CoE write until done
   IF fb_coe_write.bError THEN
      nState := 100;     // Error case
   ELSE
      IF NOT fb_coe_write.bBusy THEN
         nState := 0;    // Done
      END_IF
   END_IF
// ============== COE READ =================
11:
   // Read entry
   fb_coe_read(
   sNetId:= userNetId, 
   nSlaveAddr:= userSlaveAddr, 
   nSubIndex:= 1, 
   nIndex:= 16#9000,
   pDstBuf:= ADR(int16Buffer), 
   cbBufLen:= 2, 
   bExecute:= TRUE, 
   tTimeout:= T#1S 
   );

   nState := nState + 1; // Next state
12:
   fb_coe_read();        // Execute CoE read until done
   IF fb_coe_read.bError THEN
      nState := 100;     // Error case
   ELSE
      IF NOT fb_coe_read.bBusy THEN
         nState := 0;    // Done
      END_IF
   END_IF
   100:
   ;                     // Error handling..
END_CASE
CoE_Access.zip