/* Initialise lib in redundant NIC mode
* 在冗余网卡模式下初始化lib库
* @param[in] context = context struct
* 上下文结构体
* @param[in] redport = pointer to redport, redundant port data
* 指向冗余端口的指针,冗余端口数据
* @param[in] ifname = Primary Dev name, f.e. "eth0"
* 主网卡设备名称(ifname),例如“eth0”
* @param[in] if2name = Secondary Dev name, f.e. "eth1"
* 辅助(备用)网卡设备名称(if2name),例如“eth1”
* @return >0 if OK
* 如果成功,返回值>0
*/
int ecx_init_redundant(ecx_contextt *context, ecx_redportt *redport, const char *ifname, char *if2name)
{
int rval, /* 返回值 */
zbuf;/* 临时缓冲区 */
ec_etherheadert *ehp;/* 以太网报头定义 */
context->port->redport /* 指向冗余端口和缓冲区的指针 */
= redport;/* 指向冗余端口的指针,冗余端口数据 */
ecx_setupnic(// 将 NIC 连接到套接字(socket)的基本设置。
context->port,/* 端口(port) */
ifname, /* 主网卡设备名称(ifname) */
FALSE);/* 使用主堆栈 */
rval = /* 返回值 */
ecx_setupnic(// 将 NIC 连接到套接字(socket)的基本设置。
context->port,/* 端口(port) */
if2name,/* 辅助(备用)网卡设备名称(if2name) */
TRUE);/* 使用辅助堆栈 */
/* prepare "dummy" BRD tx frame for redundant operation */
/* 准备“dummy”广播读(BRD) 发送帧(tx frame)以进行冗余操作 */
ehp /* 以太网报头定义 */
= (ec_etherheadert *)&(context->port->txbuf2);/* 临时发送缓存区 */
ehp->sa1 =/* 源 MAC 地址 */
oshw_htons(// 将机器上的一个无符号短整型数值(uint16)转换为网络字节序[大端模式(big - endian)]
secMAC[0]);/* 用于 EtherCAT 的辅助源 MAC 地址。*/
zbuf = 0;/* 临时缓冲区 */
//在标准以太网帧中生成和设置 EtherCAT 数据报
ecx_setupdatagram(
context->port,/* 端口(port) */
&(context->port->txbuf2/* 临时发送缓存区 */),// 帧缓冲区
EC_CMD_BRD, /* 广播读(BRD) *//* Broadcast Read */
0,/* IDX 索引 */
0x0000,/* ADP 地址区 */
0x0000,/* ADO 地址区 */
2,/* 不包括 EtherCAT 报头的数据报长度 */
&zbuf);/* 临时缓冲区 */
context->port->txbuflength2 /* 临时发送缓存区长度 */
= ETH_HEADERSIZE/* 以太网报头大小=14U */
+ EC_HEADERSIZE/* EtherCAT 报头大小=12U */
+ EC_WKCSIZE/* EtherCAT 数据报中工作计数器的大小=2U */
+ 2;
return rval;/* 返回值 */
}