How do I convert from CAN_MESSAGE_BUS to CAN_MESSAGE?

21 views (last 30 days)
I'm trying to create a simple program for embedded coder TI C2000 that receives a CAN message via interrupt, places the message in a FIFO queue, and re-transmits the message from a program loop. I'm able to accomplish this until I need to try and transmit the message.
I'm able to make this work on the receive side, but it's a mess using the CAN Unpack block to output all of the data types of the CAN message and rebuilding the bus object to send to the queue.
The transmit side doesn't work because I can't figure out how to construct a CAN_MESSAGE from a CAN_MESSAGE_BUS. Sending only the data doesn't work since I need to dynamically assign the message ID.
The fundamental problem is that I don't understand what a CAN_MESSAGE type is or how to work with it. It seems to behave as if it were a primitive type because bus operations do not work, but I'm not able to work with it unless I use CAN Pack or CAN Unpack blocks. I'd much prefer to just use the CAN_MESSAGE_BUS type which contains all of the needed information, but the eCAN Transmit/Receive blocks don't allow it.
There is a similar unanswered question here.

Answers (1)

prabhat kumar sharma
prabhat kumar sharma on 24 Jan 2024
Hi Jonathan ,
In Simulink, the CAN Pack and CAN Unpack blocks are used to convert between the CAN_MESSAGE data type and a Simulink bus object (CAN_MESSAGE_BUS). However, the "CAN" blocks for transmit and receive directly work with the CAN_MESSAGE data type.
Transmitting ‘CAN’ Messages: If you have the ‘CAN_MESSAGE_BUS’, use the CAN Pack block to convert it back to a CAN_MESSAGE object. Use the CAN Transmit block to send the ‘CAN_MESSAGE’ object.
To construct a CAN_MESSAGE from a CAN_MESSAGE_BUS for transmission, you would typically do the following:
  1. Use a "CAN Pack block" and connect the "CAN_MESSAGE_BUS" to the input.
  2. Configure the "CAN Pack" block with the correct message ID and data length.
  3. The output of the "CAN Pack" block will be a CAN_MESSAGE that you can then send using the "CAN Transmit" block.
For more details on vehicle network toolbox functions you can refer this link:
Standard Flow Chart for Setting Up the Simulink Mode:
Receive Path: CAN Receive block ---> CAN Unpack block ---> FIFO Queue (implemented in State flow or MATLAB Function block)
Transmit Path: FIFO Queue ---> CAN Pack block ---> CAN Transmit block
I hope it helps!

Categories

Find more on Simulink Supported Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!