Auto commit at 2025-07-22 08:39:28

This commit is contained in:
cxh 2025-07-22 08:39:28 +08:00
parent 4fb4b07b02
commit e5ecb2e441

View File

@ -3,8 +3,9 @@
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <fcntl.h> // 用于 fcntl #include <fcntl.h>
#include <unistd.h> // 用于 close 和 read #include <unistd.h>
#include <poll.h>
CANDriver::CANDriver() = default; CANDriver::CANDriver() = default;
@ -178,22 +179,27 @@ bool CANDriver::applyFilters()
void CANDriver::receiveThreadFunc() void CANDriver::receiveThreadFunc()
{ {
#ifdef _WIN32
// Windows接收实现
#else
struct can_frame raw_frame; struct can_frame raw_frame;
struct pollfd fds;
fds.fd = sockfd;
fds.events = POLLIN;
while (running) while (running)
{ {
// 在非阻塞模式下读取数据 int ret = poll(&fds, 1, 100); // 等待最多100ms
if (ret < 0)
{
perror("poll");
continue;
}
else if (ret == 0)
{
// 超时无数据,可继续
continue;
}
ssize_t nbytes = read(sockfd, &raw_frame, sizeof(raw_frame)); ssize_t nbytes = read(sockfd, &raw_frame, sizeof(raw_frame));
if (nbytes < 0)
{
if (errno != EAGAIN) // 非阻塞错误,忽略
{
perror("read");
}
continue; // 没有数据时继续循环
}
if (nbytes == sizeof(raw_frame) && callback) if (nbytes == sizeof(raw_frame) && callback)
{ {
@ -208,5 +214,4 @@ void CANDriver::receiveThreadFunc()
callback(frame, userData); callback(frame, userData);
} }
} }
#endif
} }