Skip to main content
Associate
June 11, 2025
Solved

Performing an action after receiving a CAN message

  • June 11, 2025
  • 2 replies
  • 324 views

Question moved from this thread (a new question)

Thank you very much for the solution. However, I’m still unclear about the core point.

My main concern is that I need CAN files such that, when the code is flashed onto the STM32L4R9I-EVAL board, it should behave in the following way: upon receiving a CAN message, the board should either print something which is predefined or perform an action in response to that message.

Could you please help me with this


Best answer by mƎALLEm

Hello,

I recommend to use the Rx interrupts.

To print messages, you need to do it outside the interrupt handlers and the callback. You can for example set a flag in the interrupt handler and in the main if you check that flag was set, print the message you want.

If for example you need to toggle a LED, you can do it inside the interrupt.

You can inspire from this example: https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_Networking/Src/main.c

Hope that helps.

2 replies

mƎALLEm
mƎALLEmBest answer
Technical Moderator
June 11, 2025

Hello,

I recommend to use the Rx interrupts.

To print messages, you need to do it outside the interrupt handlers and the callback. You can for example set a flag in the interrupt handler and in the main if you check that flag was set, print the message you want.

If for example you need to toggle a LED, you can do it inside the interrupt.

You can inspire from this example: https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_Networking/Src/main.c

Hope that helps.

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
Andrew Neil
Super User
June 11, 2025

@sonic_solutions wrote:

should behave in the following way: upon receiving a CAN message, the board should either print something which is predefined or perform an action in response to that message.


Well, that's pretty much the definition of any embedded system: it receives events from external interfaces, and then does something in response.

Your job, as the embedded system developer, is to write the code to implement that something.

As @mƎALLEm said, that might typically involve saving any data from the interrupt and/or setting a flag (or flags) to tell the rest of your code that data is available, and to do its work on it.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.