Auto commit at 2025-06-06 15:33:11

This commit is contained in:
cxh 2025-06-06 15:33:11 +08:00
parent 02353b0c3a
commit 1df852f59e
2 changed files with 39 additions and 41 deletions

View File

@ -4,11 +4,14 @@
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <string> #include <string>
#include <cstring>
#include <iostream> #include <iostream>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <termios.h> #include <termios.h>
#include <vector>
#include <algorithm>
class UartHandler class UartHandler
{ {
@ -45,7 +48,7 @@ private:
std::thread read_thread; // 读取数据的线程 std::thread read_thread; // 读取数据的线程
void read_loop(); // 持续读取串口数据 void read_loop(); // 持续读取串口数据
void parse_data(uint8_t *buf, int len); // 解析数据 void parse_data(std::vector<uint8_t> &buffer); // 解析数据
int sbus_parse(); // SBUS 数据解析 int sbus_parse(); // SBUS 数据解析
void print_hex(uint8_t *buf, int len); // 打印数据(调试用) void print_hex(uint8_t *buf, int len); // 打印数据(调试用)
}; };

View File

@ -107,21 +107,18 @@ void UartHandler::stop_reading()
void UartHandler::read_loop() void UartHandler::read_loop()
{ {
uint8_t buf[512]; std::vector<uint8_t> buffer;
uint8_t temp[512];
while (reading) while (reading)
{ {
int n = read(fd, buf, sizeof(buf)); int n = read(fd, temp, sizeof(temp));
if (n > 0) if (n > 0)
{ {
parse_data(buf, n); buffer.insert(buffer.end(), temp, temp + n);
parse_data(buffer);
} }
else if (n == 0) else if (n == 0 || errno == EAGAIN)
{
usleep(1000);
}
else
{
if (errno == EAGAIN)
{ {
usleep(1000); usleep(1000);
} }
@ -132,7 +129,6 @@ void UartHandler::read_loop()
} }
} }
} }
}
void UartHandler::print_hex(uint8_t *buf, int len) void UartHandler::print_hex(uint8_t *buf, int len)
{ {
@ -143,36 +139,35 @@ void UartHandler::print_hex(uint8_t *buf, int len)
printf("\n"); printf("\n");
} }
void UartHandler::parse_data(uint8_t *buf, int len) void UartHandler::parse_data(std::vector<uint8_t> &buffer)
{ {
// 收到一帧完整的25字节数据 while (buffer.size() >= kSbusFrameLength)
if (len >= kSbusFrameLength)
{ {
memcpy(sbus_data, buf, kSbusFrameLength); // 寻找帧头SBUS 通常帧头是 0x0F
int status = sbus_parse(); auto it = std::find(buffer.begin(), buffer.end(), 0x0F);
if (it == buffer.end())
// 打印通道值
// std::cout << "Parsed Channels:\n";
// for (int i = 0; i < 8; ++i)
// {
// std::cout << "CH" << i + 1 << ": " << sbus_channels[i] << " ";
// if ((i + 1) % 8 == 0)
// std::cout << "\n"; // 每8个换行
// }
if (status == SBUS_SIGNAL_LOST)
{ {
// std::cout << "[Warning] SBUS Signal Lost!\n"; buffer.clear(); // 无帧头,清空
}
else if (status == SBUS_SIGNAL_FAILSAFE)
{
// std::cout << "[Warning] SBUS Failsafe!\n";
}
}
else
{
std::cerr << "Received data too short to parse.\n";
failsafe_status = SBUS_SIGNAL_LOST; failsafe_status = SBUS_SIGNAL_LOST;
return;
}
// 如果帧头之后数据不够 25 字节,等待下次接收
size_t index = std::distance(buffer.begin(), it);
if (buffer.size() - index < kSbusFrameLength)
{
// 保留还没凑够的部分
if (index > 0)
buffer.erase(buffer.begin(), buffer.begin() + index); // 移除前面无用部分
return;
}
// 拷贝并解析数据
std::memcpy(sbus_data, &buffer[index], kSbusFrameLength);
sbus_parse();
// 移除已处理数据
buffer.erase(buffer.begin(), buffer.begin() + index + kSbusFrameLength);
} }
} }