sudo apt-get update
sudo apt-get install can-utils libsocketcan-dev
can数据发送这个采用来自网上的一段代码进行测试:
can_send.c代码内容如下:
/* 1. 报文发送程序 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>int main()
{int s, nbytes;struct sockaddr_can addr;struct ifreq ifr;struct can_frame frame[2] = {{0}};s = socket(PF_CAN, SOCK_RAW, CAN_RAW);//创建套接字strcpy(ifr.ifr_name, "can0" );ioctl(s, SIOCGIFINDEX, &ifr); //指定 can0 设备addr.can_family = AF_CAN;addr.can_ifindex = ifr.ifr_ifindex;bind(s, (struct sockaddr *)&addr, sizeof(addr));//将套接字与 can0 绑定//禁用过滤规则,本进程不接收报文,只负责发送setsockopt(s, SOL_CAN_RAW, CAN_RA