EtherCAT主站SOEM源码解析----Raw Socket

转自 https://blog.csdn.net/ethercat_i7/article/details/52847876

SOEM使用Raw Socket收发EtherCAT帧,通过系统调用bind、send和recv实现EtherCAT通信。


1、创建Socket

初始化函数ec_init(ifname)最终会调用/oshw/linux/nicdrv.c下的以下这个函数完成绑定网卡和创建1个Raw Socket。
int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary)

/* we use RAW packet socket, with packet type ETH_P_ECAT */
*psock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ECAT));

其中ETH_P_ECAT定义为:
#define ETH_P_ECAT              0x88A4

2、绑定Socket到网卡

   /* connect socket to NIC by name */
   strcpy(ifr.ifr_name, ifname);
   r = ioctl(*psock, SIOCGIFINDEX, &ifr);
   ...
   r = bind(*psock, (struct sockaddr *)&sll, sizeof(sll));

3、收发EtherCAT帧

收发和处理EtherCAT帧的函数调用关系如下图所示:

  

猜你喜欢

转载自blog.csdn.net/wofreeo/article/details/89377656