CAN Test Occasionally gets stuck

I am sending a CAN message from Beaglebone black to the STM32 MCU on a custom PCBA. I use the following python script:

Reference code

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(
        data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07],
        print(f"Message sent on {bus.channel_info}")
    except can.CanError:
        print("Message NOT sent")
    response = bus.recv()
    print ("Response = ", response)


On the MCU, numbers in the received CAN message are incremented by 1 and sent back:

test_can_bus() is run in the while loop in main. (This is the only function that is being run in the while loop)

MCU is written in 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);
    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_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
        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);

I SSH into Beaglebone black, run the script and look at results. When it works, results look like this:

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

Occasionally, the output will get stuck after Message sent on socketcan channel 'can1'

I also connect to MCU via UART to confirm it received and sent out the message. In both cases (output stuck or unstuck), the UART readings of MCU show that it received the CAN message, and sent back the message with values incremented by 1.

Any help appreciated.

I also found and tried some code using subprocess routines which run candump and cansend scripts, with same results - they work and occasionally get stuck.

I also tried using threading, which runs candump routing in a separate thread, with same results as above.


  • This was resolved.

    It was a hardware issue caused by a loose cable connection.