1. CAN通信概述
CAN(Controller Area Network)是一种多主、广播式的串行通信总线,广泛应用于汽车电子、工业控制等领域。CAN总线具有高可靠性、实时性强、抗干扰能力突出等特点,其数据帧结构如下:
- 标识符(ID):11位(标准帧)或29位(扩展帧),决定消息优先级。
- 数据长度码(DLC):0~8字节,表示数据段长度。
在Linux系统中,通常通过SocketCAN(内核子系统)访问CAN总线。SocketCAN提供标准的BSD Socket API,使得CAN设备可以像普通网络设备一样被操作。
2. Linux SocketCAN编程基础
SocketCAN的核心数据结构是struct can_frame(定义在linux/can.h):
structcan_frame {
canid_t can_id; // CAN标识符,低29位有效,最高位标记扩展帧/远程帧
__u8 can_dlc; // 数据长度(0~8)
__u8 data[8]; // 数据载荷
};
基本操作流程:
- 创建socket:
socket(PF_CAN, SOCK_RAW, CAN_RAW) - 指定CAN接口(如
can0)并绑定:bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) - 发送/接收
can_frame:write/sendto 或 read/recvfrom - 可设置过滤器(
struct can_filter)以接收特定ID的帧。
3. 定时器在CAN通信中的典型应用
CAN通信往往需要周期性操作或超时控制:
- 周期性发送:例如以固定间隔发送状态帧、心跳帧或传感器指令。
- 接收超时:等待预期应答帧时,若超过时间未收到则重发或报错。
- 轮询与事件解耦:使用定时器触发非阻塞检查,避免忙等待。
Linux下常用的高精度定时器方案:
- timerfd:将定时器事件转化为文件描述符,可集成到
epoll/select循环中。 - POSIX timer:
timer_create + 信号或回调,但信号处理复杂。 - sleep/usleep/ nanosleep:精度低或阻塞线程,适合简单场景。
本文采用timerfd + epoll的事件驱动模型,实现单线程同时处理CAN接收与周期性发送,具有良好的实时性和可扩展性。
4. 完整C++示例:周期性CAN发送与实时接收
4.1 代码结构
- 初始化CAN套接字(虚拟接口vcan0或物理接口)。
- 使用
epoll同时监听CAN socket和timerfd。 - timerfd到期时,构造并发送一条CAN帧(ID=0x123,数据字节递增)。
- CAN socket可读时,读取并打印接收到的所有帧。
4.2 代码实现
#include<iostream>
#include<cstring>
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/epoll.h>
#include<net/if.h>
#include<sys/ioctl.h>
#include<linux/can.h>
#include<linux/can/raw.h>
#include<sys/timerfd.h>
classCanTimerDemo {
public:
CanTimerDemo(conststd::string& ifname, int period_ms)
: ifname_(ifname), period_ms_(period_ms), can_sock_(-1), timer_fd_(-1), epoll_fd_(-1) {}
~CanTimerDemo() {
if (can_sock_ >= 0) close(can_sock_);
if (timer_fd_ >= 0) close(timer_fd_);
if (epoll_fd_ >= 0) close(epoll_fd_);
}
boolinitialize(){
// 1. 创建CAN socket
can_sock_ = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (can_sock_ < 0) {
perror("socket");
returnfalse;
}
// 2. 绑定到指定CAN接口
structifreqifr;
strcpy(ifr.ifr_name, ifname_.c_str());
if (ioctl(can_sock_, SIOCGIFINDEX, &ifr) < 0) {
perror("ioctl");
returnfalse;
}
structsockaddr_canaddr;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(can_sock_, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind");
returnfalse;
}
// 可选:设置非阻塞,便于epoll处理
int flags = fcntl(can_sock_, F_GETFL, 0);
fcntl(can_sock_, F_SETFL, flags | O_NONBLOCK);
// 3. 创建timerfd
timer_fd_ = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (timer_fd_ < 0) {
perror("timerfd_create");
returnfalse;
}
structitimerspectimer_spec;
timer_spec.it_value.tv_sec = period_ms_ / 1000;
timer_spec.it_value.tv_nsec = (period_ms_ % 1000) * 1000000;
timer_spec.it_interval = timer_spec.it_value; // 周期触发
if (timerfd_settime(timer_fd_, 0, &timer_spec, nullptr) < 0) {
perror("timerfd_settime");
returnfalse;
}
// 4. 创建epoll并注册两个fd
epoll_fd_ = epoll_create1(0);
if (epoll_fd_ < 0) {
perror("epoll_create1");
returnfalse;
}
structepoll_eventev;
ev.events = EPOLLIN;
ev.data.fd = can_sock_;
if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, can_sock_, &ev) < 0) {
perror("epoll_ctl: can_sock");
returnfalse;
}
ev.events = EPOLLIN;
ev.data.fd = timer_fd_;
if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, timer_fd_, &ev) < 0) {
perror("epoll_ctl: timer_fd");
returnfalse;
}
returntrue;
}
voidrun(){
constint MAX_EVENTS = 4;
structepoll_eventevents[MAX_EVENTS];
uint8_t send_counter = 0;
std::cout << "Starting CAN + Timer demo on " << ifname_ << ", period = "
<< period_ms_ << " ms" << std::endl;
while (true) {
int nfds = epoll_wait(epoll_fd_, events, MAX_EVENTS, -1);
for (int i = 0; i < nfds; ++i) {
if (events[i].data.fd == can_sock_) {
// 接收CAN帧(可能多个,循环读取直到EAGAIN)
structcan_frameframe;
ssize_t nbytes = read(can_sock_, &frame, sizeof(frame));
if (nbytes == sizeof(frame)) {
printf("Rx: ID=0x%03X, DLC=%d, Data=", frame.can_id, frame.can_dlc);
for (int j = 0; j < frame.can_dlc; ++j)
printf("%02X ", frame.data[j]);
printf("\n");
} elseif (nbytes < 0 && errno != EAGAIN) {
perror("read CAN");
}
}
elseif (events[i].data.fd == timer_fd_) {
// 定时器到期,清除过期计数(必须读)
uint64_t expirations;
ssize_t s = read(timer_fd_, &expirations, sizeof(expirations));
if (s != sizeof(expirations)) {
perror("read timerfd");
continue;
}
// 构造并发送CAN帧
structcan_frametx_frame;
tx_frame.can_id = 0x123; // 标准帧ID
tx_frame.can_dlc = 8;
for (int i = 0; i < 8; ++i)
tx_frame.data[i] = send_counter + i;
send_counter++;
ssize_t nbytes = write(can_sock_, &tx_frame, sizeof(tx_frame));
if (nbytes != sizeof(tx_frame)) {
perror("write CAN");
} else {
printf("Tx: ID=0x%03X, DLC=%d, Data=", tx_frame.can_id, tx_frame.can_dlc);
for (int i = 0; i < tx_frame.can_dlc; ++i)
printf("%02X ", tx_frame.data[i]);
printf("\n");
}
}
}
}
}
private:
std::string ifname_;
int period_ms_;
int can_sock_;
int timer_fd_;
int epoll_fd_;
};
intmain(int argc, char* argv[]){
std::string can_interface = "vcan0"; // 可改为实际接口,如can0
int period_ms = 500; // 发送周期500ms
if (argc >= 2) can_interface = argv[1];
if (argc >= 3) period_ms = std::stoi(argv[2]);
CanTimerDemo demo(can_interface, period_ms);
if (!demo.initialize()) {
std::cerr << "Initialization failed" << std::endl;
return1;
}
demo.run();
return0;
}
4.3 编译与运行说明
编译命令(Linux):
g++ -std=c++11 -o can_timer_demo can_timer_demo.cpp
准备虚拟CAN接口(用于测试,无需硬件):
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set vcan0 up
运行:
# 使用vcan0接口,周期500ms
./can_timer_demo vcan0 500
测试接收:在另一终端开启candump监听:
candump vcan0
您将看到每500ms发送一帧ID=0x123的数据,且数据字节递增。
5. 关键点解析
- 非阻塞+epoll:
timerfd和can_sock均设为非阻塞,避免了读操作阻塞线程,保证了事件驱动的实时性。 - 定时器精度:
timerfd基于高精度时钟(CLOCK_MONOTONIC),周期可达纳秒级,但受系统调度影响,实际周期误差通常在微秒到毫秒级。 - CAN过滤器:本示例未设置过滤器,接收所有CAN帧。若需只接收特定ID,可使用
setsockopt设置can_filter数组,显著降低处理负载。 - 多线程替代方案:也可以创建独立线程进行周期性发送,但使用
epoll单线程模型避免了锁竞争,代码更简洁。 - 错误处理:示例中简化了持续运行的错误恢复,实际生产环境应区分可恢复错误(如EAGAIN)与致命错误。
6. 高级话题:接收超时实现
基于上述模型,可以轻松实现接收超时:增加一个额外的timerfd作为超时定时器。例如,在发送请求帧后启动一个短周期定时器,若超时未收到应答则触发重发。伪代码思路:
- 当超时定时器到期时,检查是否已收到应答,否则执行重发逻辑。
7. 总结
本文深入讲解了CAN通信在Linux下的SocketCAN编程模型,并重点阐述了timerfd + epoll如何优雅地实现周期性发送与事件驱动接收。提供的完整C++示例可直接在虚拟CAN接口上运行验证,为实际项目中集成CAN通信和定时任务提供了清晰的参考范式。对于更复杂的场景(如多周期任务、动态定时器调整),可基于本模型进一步扩展。
注:运行示例需要Linux内核支持SocketCAN(内核版本≥2.6.25)以及虚拟CAN模块(vcan)。实际使用物理CAN接口时,请确保接线正确并配置比特率等参数。