嵌入式


socket CAN

<pre><code>ifconfig –a # ip命令设置baudrate ip link set can0 type cantq 125 prop-seg 6phase-seg1 7 phase-seg2 2 sjw 1 #或直接设置 ip link set can0 type can bitrate 125000 #查询设定值 ip -details link show can0 #使能can ifconfig can0 up #关闭can ifconfig can0 down #查询can工作状态 ip -details -statistics link show can0</code></pre> <pre><code>struct sockaddr_can addr; struct ifreq ifr; s = socket(PF_CAN, SOCK_RAW, CAN_RAW);//创建 SocketCAN 套接字 strcpy(ifr.ifr_name, "can0" ); ioctl(s, SIOCGIFINDEX, &amp;ifr);//指定 can0 设备 addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; bind(s, (struct sockaddr *)&amp;addr, sizeof(addr)); //将套接字与 can0 绑定 struct can_frame frame; int nbytes = write(s, &amp;frame, sizeof(frame)); //发送数据 if(nbytes != sizeof(frame)) //如果 nbytes 不等于帧长度,就说明发送失败 write(s, &amp;frame, sizeof(frame)); int nbytes = read(s, &amp;frame, sizeof(frame)); # 过滤 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0); //禁用过滤规则 # 过滤错误帧 can_err_mask_t err_mask = ( CAN_ERR_TX_TIMEOUT | CAN_ERR_BUSOFF ); setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, err_mask, sizeof(err_mask)); #回环 int loopback = 0; // 0 表示关闭, 1 表示开启( 默认) setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &amp;loopback, sizeof(loopback));</code></pre>

页面列表

ITEM_HTML