如何在Wago 750-350 ethercat中编写和阅读频道?

时间:2019-04-08 07:02:00

标签: c++ ethercat

我有一个EtherCat Wago 750-354,我想对输出模块进行简单测试(打开和关闭LED)。我阅读了该教程,并编写了简短的测试代码,但无法编写输出模块。

教程链接为:https://openethercatsociety.github.io/doc/soem/tutorial_8txt.html

这是我的代码:

#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();
   }

0 个答案:

没有答案