请教,socketcan,用write()发送,连续发送多条数据时,容易返回-1错误。
write加一句qDebug()打印发送的数据后,可以了,但偶尔还是会出,概率极小。感觉是相当于加了个延时等待。
socketcan有没有判断是否可写的功能?
oid NuvotonCan::socketCanSend(uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
1 {
2 memset(&can1SendFrame, 0, sizeof(struct can_frame));
3 can1SendFrame.can_id = (0x1b60f5f4 & CAN_EFF_MASK) | CAN_EFF_FLAG;
4 can1SendFrame.can_dlc = 8;
5 can1SendFrame.data[0] = d0;
6 can1SendFrame.data[1] = d1;
7 can1SendFrame.data[2] = d2;
8 can1SendFrame.data[3] = d3;
9 can1SendFrame.data[4] = d4;
10 can1SendFrame.data[5] = d5;
11 can1SendFrame.data[6] = d6;
12 can1SendFrame.data[7] = d7;
13
14 int ret;
15 ret = write(can1Socket, &can1SendFrame, sizeof(can1SendFrame));
16 if (ret == -1)
17 {
18 qDebug() << "write error!" << ret << endl;
19 }
20 else
21 {
qDebug("socketcan send ok: %x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x", can1SendFrame.can_id, can1SendFrame.data[0]
7 }
6 }
离线
受群里两位朋友指点,改成这样可以了。
int ret; │ ▶ bin/
1 uint8_t count = 0; │ ▶ x86/
2 while (write(can1Socket, &can1SendFrame, sizeof(can1SendFrame)) < 0 && count < 100) │ aescrypto.cpp
3 { │ can.c
4 qDebug() << "socketcan write failed, delay"; │ cantest*
5 count++; │ main.cpp*
6 delayMs(1); │ nuvotoncan.cpp*
7 }
离线