CAN 测试偶尔会卡住

问题描述 投票:0回答:1

我正在从 Beaglebone black 向自定义 PCBA 上的 STM32 MCU 发送 CAN 消息。我使用以下 python 脚本:

参考代码

import can

# Specify the CAN interface and interface type
can_interface = "can1"

with can.interface.Bus(channel=can_interface, bustype="socketcan") as bus:
    msg = can.Message(
        arbitration_id=0x5A1,
        data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07],
        is_extended_id=True
    )
    try:
        bus.send(msg)
        print(f"Message sent on {bus.channel_info}")
    except can.CanError:
        print("Message NOT sent")
    
   
    response = bus.recv()
    print ("Response = ", response)
        

        

在MCU上,接收到的CAN报文中的数字加1并发回:

test_can_bus() 在 main 中的 while 循环中运行。 (这是 while 循环中运行的唯一函数)

MCU是用C语言编写的:

void test_can_bus()
{
/* Receive data */
if (HAL_FDCAN_GetRxFifoFillLevel(&hfdcan1, FDCAN_RX_FIFO0))
{
    HAL_UART_Transmit(&hlpuart1, "Received:", 9, HAL_MAX_DELAY);
    HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_FIFO0, &RxHeader, RxData);
    HAL_Delay(500);
    sprintf((char*) debug_str, "%02x %02x %02x %02x %02x %02x %02x %02x\r\n", RxData[0], RxData[1], RxData[2], RxData[3], RxData[4], RxData[5], RxData[6], RxData[7]);

    /* Prepare received data to be sent back */
    for (int i = 0; i<8; i++)
    {
        /* AUX 1 increase by 1 */
        TxData[i]= RxData[i]+1;
        /* AUX 2 increase by 2 */
        //TxData[i]= RxData[i] + 2;
    }
        //HAL_Delay(1000);
        HAL_UART_Transmit(&hlpuart1, (uint8_t*)debug_str, strlen((const char*)(debug_str)), HAL_MAX_DELAY);
        HAL_UART_Transmit(&hlpuart1, "Sent:    ", 9, HAL_MAX_DELAY);
        if (HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData) != HAL_OK)
        {
            // Transmission request Error
              Error_Handler();
        }
        HAL_Delay(500);
        sprintf((char*) debug_str, "%02x %02x %02x %02x %02x %02x %02x %02x\r\n", TxData[0],
                      TxData[1], TxData[2], TxData[3], TxData[4], TxData[5], TxData[6], TxData[7]);
        HAL_UART_Transmit(&hlpuart1, (uint8_t*)debug_str, strlen((const char*)debug_str), HAL_MAX_DELAY);
        HAL_UART_Transmit(&hlpuart1, NL, sizeof(NL), HAL_MAX_DELAY);
    }
}

我通过 SSH 连接到 Beaglebone black,运行脚本并查看结果。当它起作用时,结果如下所示:

Message sent on socketcan channel 'can1'
Response =  Timestamp: 1699543213.502878    ID: 00000023    X Rx                DL:  8    01 02 03 04 05 06 07 08     Channel: can1

有时候,输出会卡住

Message sent on socketcan channel 'can1'

我还通过 UART 连接到 MCU 以确认它接收并发送了消息。在这两种情况下(输出卡住或不卡住),MCU 的 UART 读数都显示它收到了 CAN 消息,并发送回了消息,并且值增加了 1。

任何帮助表示赞赏。

我还发现并尝试了一些使用运行 candump 和 cansend 脚本的子进程例程的代码,具有相同的结果 - 它们可以工作,但偶尔会卡住。

我还尝试使用线程,它在单独的线程中运行 candump 路由,结果与上面相同。

stm32 beagleboneblack can-bus
1个回答
0
投票

问题已解决。

这是由电缆连接松动引起的硬件问题。

© www.soinside.com 2019 - 2024. All rights reserved.