尝试igh函数的功能,加深对不同函数的认识,总结相关函数的用法。
1.ecrt_master_get_slave
/** Obtains slave information.
*
* Tries to find the slave with the given ring position. The obtained
* information is stored in a structure. No memory is allocated on the heap in
* this function.
*
* \attention The pointer to this structure must point to a valid variable.
*
* \return 0 in case of success, else < 0
*/
int ecrt_master_get_slave(
ec_master_t *master, /**< EtherCAT master */
uint16_t slave_position, /**< Slave position. */
ec_slave_info_t *slave_info /**< Structure that will output the
information */
);
//example----------------------------------------------------------------------
typedef struct {
unsigned short position;
unsigned int vendor_id;
unsigned int product_code;
}EL_Slave_t;
int GetSlavesInfo(EL_Slave_t* slaves, int num)
{
ec_master_t* master = ecrt_request_master(0);
ec_slave_info_t tmps;
int i;
for(i = 0;i< num;i++)
{
if(ecrt_master_get_slave(master, i, &tmps) == 0)
{
(slaves + i)->position = tmps.position;
(slaves + i)->vendor_id = tmps.vendor_id;
(slaves + i)->product_code = tmps.product_code;
printf("slave position:%d,vendor_id:%d,product_code:%d,name:%s\n",
tmps.position, tmps.vendor_id, tmps.product_code, tmps.name);
}
else
break;
}
ecrt_release_master(master);
return i;
}
int main(int argc, char *argv[])
{
...
EL_Slave_t slaves[16];
int num = GetSlavesInfo(slaves, 16);
printf("num = %d\n",num);
...
}
//测试结果、终端输出
slave position:0,vendor_id:9,product_code:37458,name:LAN9252-EVB-HBI
slave position:1,vendor_id:2,product_code:72100946,name:EK1100 EtherCAT-Koppler (2A E-Bus)
slave position:2,vendor_id:2,product_code:131608658,name:EL2008 8K. Dig. Ausgang 24V, 0.5A
slave position:3,vendor_id:2,product_code:131608658,name:EL2008 8K. Dig. Ausgang 24V, 0.5A
slave position:4,vendor_id:2,product_code:131608658,name:EL2008 8K. Dig. Ausgang 24V, 0.5A
num = 5