Clear Filters
Clear Filters

How to queue CAN messages for transmission?

3 views (last 30 days)
Chris Mounce
Chris Mounce on 11 Mar 2020
Commented: Chris Mounce on 9 Jun 2022
Hello,
I have a number of CAN messages that I'm looking to add to a queue for transmission. When trying to do this with either of the two queue blocks, I get errors about either the output of the queue not being compatible with the transmit block, or (with the other queue block option) the CAN message being the wrong data type, since it's a CAN message and not a basic datatype. It seems like setting up a CAN message FIFO in Simulink should be fairly straightforward, what am I missing? Thanks!
  2 Comments
Chris Mounce
Chris Mounce on 9 Jun 2022
I eventually used an S function with a third party CAN solution that was able to queue up the messages.
However, there is a solution with newer versions of MATLAB that allow for data store indexing. As you can use an indexed data store as a rotating buffer.
It involves 5 steps:
  1. Create a ReadIndex datastore starting at value 1
  2. Create a WriteIndex datastore starting at value 1
  3. Create a MessageBuffer datastore of whatever size you want
  4. Write a message to MessageBuffer at index WriteIndex and then increment WriteIndex. If greater than the size of your MessageBuffer, wrap to 1
  5. Whenever WriteIndex != ReadIndex, read the data in MessageBuffer at index ReadIndex and then increment ReadIndex. If greater than the size of your MessageBuffer, wrap to 1.
Keep in mind that if these writes or reads are called via an interrupt, you could have problems with data integrity.

Sign in to comment.

Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!