Auto commit at 2025-06-12 15:29:22

This commit is contained in:
cxh 2025-06-12 15:29:22 +08:00
parent f019bef882
commit 50f316a5f2
2 changed files with 10 additions and 1 deletions

View File

@ -167,6 +167,13 @@ void CANDriver::receiveThreadFunc()
frame.ext = rec[i].ExternFlag != 0;
frame.rtr = rec[i].RemoteFlag != 0;
frame.dlc = rec[i].DataLen;
if (frame.dlc > 8)
{
std::cerr << "Invalid CAN data length: " << (int)frame.dlc << std::endl;
frame.dlc = 8; // 或跳过此帧
}
std::memcpy(frame.data, rec[i].Data, frame.dlc);
callback(frame, userData);
}

View File

@ -95,7 +95,9 @@ void receiveHandler(const CANFrame &frame, void *userData)
auto msg = sweeperMsg::CanFrame();
msg.id = frame.id;
msg.dlc = frame.dlc;
msg.data.assign(frame.data, frame.data + frame.dlc);
size_t len = frame.dlc <= 8 ? frame.dlc : 8;
msg.data.assign(frame.data, frame.data + len);
publisher->publish(msg);
// 根据开关决定是否打印 CAN 消息