CAN module DataReceivedEvent does not get triggerd if CAN messages are sent while EMX is booting up

Hi,

When using CAN on the EMX I have a method that hooks into the DataReceivedEvent of the CAN class. I have a peripheral device which sends CAN messages to the EMX.

The method works fine when the peripheral device sends CAN messages AFTER the EXM application has fully started up.

The problem is; IF the peripheral device sends messages to the EMX before the EMX has fully booted up THEN the DataReceivedEvent will not trigger at all for any subsequent messages that the peripheral device sends to the EMX.

I have found a work around that if i call can.GetMessages after i start the application, i find that there were messages in the buffer (which were sent while the EMX was booting up) and any subsequent message sent by the peripheral device DOES trigger the DataReceivedEvent handler.

Hi there Evan

I suspect this might be a common problem when you have a hardware device buffering the CAN messages. The issue would seem to come about when your hardware has started and a message has been accepted but not processed by the app, and the hardware doesn’t trigger the event. One approach to dealing with this is like you have done; another might be to move the CAN initialisation as early in startup as you can; another is to flush anything in the queue (not sure if CAN has the same flushinbuffer like UART does).

It is possible that your device is in bus-off state not emx.

Hi Evan,

I had the same issues, the trick is to place no code between can start and the creation of the event handlers (if you have code in between and allot of traffic then hardware buffers are flooded before you event handler is up)

            // Use channel 1 SET to 1000 kbit/s (BRP=3) and create Eventhandlers
            BRP = 3;
            can1 = new CAN( CAN.Channel.Channel_1, (uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)));
            can1.DataReceivedEvent += new CANDataReceivedEventHandler(can1_DataReceivedEvent);
            can1.ErrorReceivedEvent += new CANErrorReceivedEventHandler(can1_ErrorReceivedEvent);

            // Use channel 2 SET to 125 kbit/s (BRP=24) and create Eventhandlers
            BRP = 24;
            can2 = new CAN(CAN.Channel.Channel_2, (uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)));
            can2.DataReceivedEvent += new CANDataReceivedEventHandler(can2_DataReceivedEvent);
            can2.ErrorReceivedEvent += new CANErrorReceivedEventHandler(can2_ErrorReceivedEvent);
1 Like