3

I have an EtherCat Wago 750-354 and I would like to do simple test for the output module (turn the leds on and off). I read the tutorial and I wrote short code for testing but I am not able to write the output module.

The tutorial link is: https://openethercatsociety.github.io/doc/soem/tutorial_8txt.html

and here is my code:

#define SLAVE_NR 1

void set_output_int8 (uint16 slave_no, uint8 module_index, int8 value)
{
  uint8 *data_ptr;

  data_ptr = ec_slave[slave_no].outputs;
  /* Move pointer to correct module index*/
  //data_ptr += module_index * 8;
  /* Read value byte by byte since all targets can't handle misaligned addresses */
  *data_ptr++ = value;
}

void set_output_bit (uint16 slave_no, uint8 module_index, uint8 value)
{
   /* Get the the startbit position in slaves IO byte */
   uint8 startbit = ec_slave[slave_no].Ostartbit;
   /* Set or Clear bit */
   if (value == 0)
      *ec_slave[slave_no].outputs &= ~(1 << (module_index - 1 + startbit));
   else
      *ec_slave[slave_no].outputs |= (1 << (module_index - 1 + startbit));
}


char IOmap[4096];
volatile int wkc;


int main(int argc, char **argv)
{
  int chk;

 //initialise SOEM
 if (ec_init("enp4s0f1"))
 {

   if ( ec_config_init(FALSE) > 0 )
  {
     std::cout<<"slave found and configured"<<ec_slavecount<<std::endl;

     ec_config_map(&IOmap);

     ec_configdc();

     std::cout<<"Slave mapped, state to SAFE_OP"<<std::endl;

     /* wait for all slaves to reach SAFE_OP state */
     ec_statecheck(1, EC_STATE_SAFE_OP,  EC_TIMEOUTSTATE * 4);


     std::cout<<"Request operational state for the slave"<<std::endl;

     ec_slave[1].state = EC_STATE_OPERATIONAL;
     /* send one valid process data to make outputs in slaves happy*/
     ec_send_processdata();
     ec_receive_processdata(EC_TIMEOUTRET);
     /* request OP state for all slaves */
     ec_writestate(1);
     chk = 40;
     /* wait for all slaves to reach OP state */
     do
     {
        ec_send_processdata();
        ec_receive_processdata(EC_TIMEOUTRET);
        ec_statecheck(0, EC_STATE_OPERATIONAL, 50000);
     }
     while (chk-- && (ec_slave[1].state != EC_STATE_OPERATIONAL));
     if (ec_slave[1].state == EC_STATE_OPERATIONAL )
     {
        std::cout<<"Operational state reached for the slave"<<std::endl;
     }

     ec_slave[1].state = EC_STATE_INIT;
     /* request INIT state for all slaves */
     ec_writestate(0);

  set_output_bit (SLAVE_NR, 2, 1);

  ec_send_processdata();
   }

Vaban
  • 33
  • 2

0 Answers0