创建虚拟CAN接口
在Linux上能使用虚拟CAN接口之前,需要在终端执行以下三个步骤:
加载vcan内核模块: sudo modprobe vcan
创建虚拟CAN接口: sudo ip link add dev vcan0 type vcan
将虚拟CAN接口处于在线状态: sudo ip link set up vcan0
然后,通过命令ip addr | grep "can"
来验证是否可用并处于在线状态
也可以通过shell脚本来自动化实现以上步骤:
创建一个vcan.sh
文件。然后将其标记为可执行文件: chmod +x ~/vcan.sh
,之后执行这个文件 ./vcan.sh
#!/bin/bash
# Make sure the script runs with super user priviliges.
[ "$UID" -eq 0 ] || exec sudo bash "$0" "$@"
# Load the kernel module.
modprobe vcan
# Create the virtual CAN interface.
ip link add dev vcan0 type vcan
# Bring the virutal CAN interface online.
ip link set up vcan0
使用 can-utils 测试CAN通信
接下来我们要基于上面创建的虚拟CAN接口,来测试一下CAN通信情况。工具包 can-utils 是一个命令行工具,可以完美的满足我们的需求。我们只需要在电脑上安装一下这个工具包即可:
Ubuntu/Debian: sudo apt install can-utils
接下来,我们打开两个终端窗口,一个是用来查看所有的CAN消息,另一个是用来发送CAN消息。
在用来查看CAN消息的终端中执行以下命令:
candump -tz vcan0
在用来发送CAN消息的终端中,模拟发送CAN请求:
cansend vcan0 123#00FFAA5501020304
在发送完CAN请求后,就会在第一个查看CAN消息的终端中看到发送的CAN消息:
发送CAN信息
melvyn@melvyn:~$ cansend vcan0 123#00FFAA5501020304
melvyn@melvyn:~$ cansend vcan0 123#00FFAA5501020304
查看CAN信息
(000.000000) vcan0 123 [8] 00 FF AA 55 01 02 03 04
(001.919922) vcan0 123 [8] 00 FF AA 55 01 02 03 04
从上面的输出可以验证使用新创建的vcan0虚拟CAN接口可以正常进行CAN通信。
CAN数据读取的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
int main(void)
{
struct ifreq ifr = {0};
struct sockaddr_can can_addr = {0};
struct can_frame frame = {0};
int sockfd = -1;
int i;
int ret;
/* 打开套接字 */
sockfd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if(0 > sockfd) {
perror("socket error");
exit(EXIT_FAILURE);
}
/* 指定can0设备 */
strcpy(ifr.ifr_name, "vcan0");
ioctl(sockfd, SIOCGIFINDEX, &ifr);
can_addr.can_family = AF_CAN;
can_addr.can_ifindex = ifr.ifr_ifindex;
/* 将can0与套接字进行绑定 */
ret = bind(sockfd, (struct sockaddr *)&can_addr, sizeof(can_addr));
if (0 > ret) {
perror("bind error");
close(sockfd);
exit(EXIT_FAILURE);
}
/* 设置过滤规则 */
//setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
/* 接收数据 */
for ( ; ; ) {
if (0 > read(sockfd, &frame, sizeof(struct can_frame))) {
perror("read error");
break;
}
/* 校验是否接收到错误帧 */
if (frame.can_id & CAN_ERR_FLAG) {
printf("Error frame!\n");
break;
}
/* 校验帧格式 */
if (frame.can_id & CAN_EFF_FLAG) //扩展帧
printf("扩展帧 <0x%08x> ", frame.can_id & CAN_EFF_MASK);
else //标准帧
printf("标准帧 <0x%03x> ", frame.can_id & CAN_SFF_MASK);
/* 校验帧类型:数据帧还是远程帧 */
if (frame.can_id & CAN_RTR_FLAG) {
printf("remote request\n");
continue;
}
/* 打印数据长度 */
printf("[%d] ", frame.can_dlc);
/* 打印数据 */
for (i = 0; i < frame.can_dlc; i++)
printf("%02x ", frame.data[i]);
printf("\n");
}
/* 关闭套接字 */
close(sockfd);
exit(EXIT_SUCCESS);
}
CAN数据写入的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
int main(void)
{
struct ifreq ifr = {0};
struct sockaddr_can can_addr = {0};
struct can_frame frame = {0};
int sockfd = -1;
int ret;
/* 打开套接字 */
sockfd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if(0 > sockfd) {
perror("socket error");
exit(EXIT_FAILURE);
}
/* 指定can0设备 */
strcpy(ifr.ifr_name, "vcan0");
ioctl(sockfd, SIOCGIFINDEX, &ifr);
can_addr.can_family = AF_CAN;
can_addr.can_ifindex = ifr.ifr_ifindex;
/* 将can0与套接字进行绑定 */
ret = bind(sockfd, (struct sockaddr *)&can_addr, sizeof(can_addr));
if (0 > ret) {
perror("bind error");
close(sockfd);
exit(EXIT_FAILURE);
}
/* 设置过滤规则:不接受任何报文、仅发送数据 */
setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
/* 发送数据 */
frame.data[0] = 0xA0;
frame.data[1] = 0xB0;
frame.data[2] = 0xC0;
frame.data[3] = 0xD0;
frame.data[4] = 0xE0;
frame.data[5] = 0xF0;
frame.can_dlc = 6; //一次发送6个字节数据
frame.can_id = 0x123;//帧ID为0x123,标准帧
for ( ; ; ) {
ret = write(sockfd, &frame, sizeof(frame)); //发送数据
if(sizeof(frame) != ret) { //如果ret不等于帧长度,就说明发送失败
perror("write error");
goto out;
}
sleep(1); //一秒钟发送一次
}
out:
/* 关闭套接字 */
close(sockfd);
exit(EXIT_SUCCESS);
}
补充 CAN 的常用操作命令:
可以使用ip命令来查看或设置CAN,使用ifconfig
或ip
命令来开启/关闭CAN,canconfig工具来配置和调试CAN,cansend
和 candump
用于收发CAN报文。
#ifconfig -a //查到当前can网络 can0 can1,包括收发包数量、是否有错误等等
#ip link set vcan0 down //关闭can设备;或使用ifconfig canX down
#ip link set vcan0 up //开启can设备;或使用ifconfig canX up
#ip -details link show vcan0 //显示can设备详细信息;
#ip link set vcan0 up type can bitrate 250000 //设置can波特率
#canconfig vcan0 ctrlmode loopback on //回环测试;
#canconfig vcan0 restart // 重启can设备;
#canconfig vcan0 stop //停止can设备;
#canecho vcan0 //查看can设备总线状态;
#candump vcan0 //接收can总线发来的数据;
#cansend vcan0 --identifier=ID+数据 //发送数据;
#candump vcan0 --filter=ID:mask//使用滤波器接收ID匹配的数据