CAN

CAN长时间收发测试,开发板与CAN分析仪通信异常

发布于 2020-12-22 14:19:33

使用STM32L432开发板测试CAN收发功能,分为发送、接收两个线程,用CAN分析仪进行数据收发测试,长时间运行后,CAN分析仪与开发板收发数据均异常,调试串口功能正常,代码如下:

void CanTx_thread_entry(void* parameter)
{
struct rt_can_msg *msg;

if (can_open("can1") != RT_EOK)

{
    rt_kprintf("can1 open error.\n");
    while (1)
    {
        rt_thread_delay(10);
    }
}
rt_kprintf("CanTx_thread_entry...\n");

for(;;)
{
    processTxRxOnce();
    rt_kprintf("processTxRxOnce\n");

    rt_thread_mdelay(1000);
}

}

void CanRx_thread_entry(void* parameter)
{

struct rt_can_msg *msg;
rt_kprintf("CanRx_thread_entry...\n");

for(;;)
    {
        can_recvmsg(&msg);
        rt_thread_mdelay(10);
    }

}

void processTxRxOnce(void)
{

struct rt_can_msg msg = {0};

msg.id      = 0x78;
msg.ide     = RT_CAN_STDID;
msg.rtr     = RT_CAN_DTR;
msg.len     = 8;
msg.data[0] = 0x00;
msg.data[1] = 0x11;
msg.data[2] = 0x22;
msg.data[3] = 0x00;
msg.data[4] = 0x11;
msg.data[5] = 0x00;
msg.data[6] = 0x11;
msg.data[7] = 0x22;

const rt_size_t tx_res = can_sendmsg(&msg);
if(tx_res == 0)           // Failure - drop the frame and report
{
    //canardPopTxQueue(&canard_);
    rt_kprintf("Transmit error %d, frame dropped\n", tx_res);
}

return;

}

rt_err_t can_recvmsg(struct rt_can_msg *msg)
{

rt_size_t size = 0;
struct rt_can_msg can_msg = {0};
rt_err_t result = RT_EOK;
rt_uint32_t rx_length;

/* 从消息队列中读取消息*/
result = rt_mq_recv(rx_mq, &size, sizeof(size), 0);
if(result == -RT_ETIMEOUT)
{
    /* 接收超时*/
    //rt_kprintf("timeout count:%d\n", ++count);
    return -RT_ETIMEOUT;
}

/* 成功收到消息*/
if(result == RT_EOK)
{
    count = 0;

    /* 读取消息*/
    rx_length = rt_device_read(can_device, 0, &can_msg,
            sizeof(can_msg));

    if(rx_length > 0)
    {
        rt_kprintf("CanRcvMsg Id-%x IDE-%d Len-%d\n", can_msg.id, can_msg.ide, can_msg.len);

        int i = 0;
        for(i = 0; i < can_msg.len; i++)
        {
            rt_kprintf("%02X ", can_msg.data[i]);
        }
        rt_kprintf("\n");

        rt_memcpy(msg, &can_msg, sizeof(can_msg));
        return rx_length;
    }
}

return -RT_ERROR;

}

rt_size_t can_sendmsg(const struct rt_can_msg *data)
{

struct rt_can_msg msg = {0};

rt_memcpy(&msg, data, sizeof(msg));
msg.ide = RT_CAN_EXTID;
msg.rtr = 0;

return rt_device_write(can_device, 0, &msg, sizeof(msg));

}

查询线程、CAN状态如下:
msh >list_thread
thread pri status sp stack size max used left tick error
------- --- ------- ---------- ---------- ------ ---------- ---
CanRx 10 suspend 0x000000b8 0x00000400 17% 0x0000000f 000
CanTx 10 suspend 0x00000148 0x00000400 32% 0x0000000d 000
serial2 20 suspend 0x000000c0 0x00000400 18% 0x0000000a 000
tshell 20 ready 0x000001c8 0x00001000 12% 0x00000008 000
tidle 31 ready 0x0000005c 0x00000100 51% 0x00000002 000
timer 4 suspend 0x00000080 0x00000200 25% 0x00000009 000
main 10 suspend 0x000000bc 0x00000800 15% 0x00000012 000
msh >canstat can1
Finded can device: can1...
Receive...error..count: 0000000000. Send.....error....count: 0000000136.
Bit..pad..error..count: 0000000000. Format...error....count: 0000000000
Ack.......error..count: 0000000000. Bit......error....count: 0000000004.
CRC.......error..count: 0000000000. Error.code.[0000000003]: Passive !.
Total.receive.packages: 0000000000. Droped.receive.packages: 0000000000.
Total..send...packages: 0000000343. Droped...send..packages: 0000000000.

查看更多

关注者
0
被浏览
230
0 个回答
暂无答案,快来添加答案吧

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览