Auto commit at 2025-07-22 08:39:28
This commit is contained in:
parent
4fb4b07b02
commit
e5ecb2e441
@ -3,8 +3,9 @@
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <fcntl.h> // 用于 fcntl
|
||||
#include <unistd.h> // 用于 close 和 read
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
|
||||
CANDriver::CANDriver() = default;
|
||||
|
||||
@ -178,22 +179,27 @@ bool CANDriver::applyFilters()
|
||||
|
||||
void CANDriver::receiveThreadFunc()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// Windows接收实现
|
||||
#else
|
||||
struct can_frame raw_frame;
|
||||
struct pollfd fds;
|
||||
fds.fd = sockfd;
|
||||
fds.events = POLLIN;
|
||||
|
||||
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));
|
||||
if (nbytes < 0)
|
||||
{
|
||||
if (errno != EAGAIN) // 非阻塞错误,忽略
|
||||
{
|
||||
perror("read");
|
||||
}
|
||||
continue; // 没有数据时继续循环
|
||||
}
|
||||
|
||||
if (nbytes == sizeof(raw_frame) && callback)
|
||||
{
|
||||
@ -208,5 +214,4 @@ void CANDriver::receiveThreadFunc()
|
||||
callback(frame, userData);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user