主要内容

CAN Receive

Receive messages from controller area network (CAN) bus

Since R2026a

  • ROS 2 CAN Receive Block Mask

Libraries:
ROS Toolbox / ROS 2

Description

This feature also requires the Vehicle Network Toolbox™ product when receiving message as a structured CAN message.

The CAN Receive block receives messages from a CAN network using a real or virtual CAN interface on your target machine. It enables the Simulink® model to process incoming CAN messages in either raw uint8 format or as structured messages containing ID, length, and data fields. Use this block to receive CAN messages in real-time simulation, external mode execution, or code generation workflows. The block supports polling-based execution, where it checks for new messages at each time step, similar to the IsNew semantics in the ROS 2 Subscribe block. If you do not have access to physical hardware, you can use a virtual CAN interface for testing and validation. For more information on how to set up a virtual CAN interface, see Set Up Virtual CAN Interface.

This block outputs received messages in a format compatible with downstream ROS 2 message handling blocks that support SocketCAN integration for CAN message transportation.

This image shows a ROS 2-CAN node that you can deploy on hardware. This node acts as a bridge between the CAN device and the ROS 2 network, thereby enabling the transmission and reception of CAN messages.

For Raw Data input type, you can apply message filters such as Identifier type, Message ID, and Message length from the block mask parameters.

For ROS Msg or CAN Msg input type, you can set message filters such as ID Type, Acceptance Mask, and Acceptance Filter using the steps listed in Configure CAN Interfaces and Apply Message Filtering.

Examples

Ports

Output

expand all

Message data, specified as one of these options:

  • Vector of data type uint8 to receive the message as raw data.

  • Bus signal of type SL_Bus_mw_can_msgs_Frame to receive the message as a ROS 2 message. The SL_Bus_mw_can_msgs_Frame type is a bus generated by Simulink that corresponds to the ROS message type mw_can_msgs/Frame. Simulink creates this bus automatically when you import the mw_can_msgs package using the ROS message importer. This type defines the fields of a CAN message exchanged between Simulink and ROS nodes.

    To receive the message as a ROS 2 message, connect the input port to a Blank Message block from the ROS 2 library.

  • Bus signal of type CAN_MESSAGE_BUS to receive the message as a structured CAN message. The CAN_MESSAGE_BUS type is a Simulink bus object that defines the structure of a CAN message used for communication. It is used as the signal interface between Simulink and the CAN protocol layers.

    To receive the message as a structured CAN message, connect the input port to corresponding blocks from the Vehicle Network Toolbox to convert between sets of signal values and formatted CAN messages for communication in a CAN network.

Data Types: uint8 | ROS Msg | CAN Msg

Status of the received output message, returned as a scalar value.

Data Types: uint8

Error codes, specified as a scalar value when the Data output type parameter is set to Raw Data.

Dependencies

To enable this parameter at the output port, select Enable error output signal under Output Ports.

00000000RXWARRXEPRX0OVRRX1OVR
bit7bit6bit5bit4bit3bit2bit1bit0

RX1OVR: Receive Buffer 1 Overflow Flag bit

RX0OVR: Receive Buffer 0 Overflow Flag bit

RXEP: Receive Error-Passive Flag bit

RXWAR: Receive Error Warning Flag bit

Indicates whether the received CAN message is a remote frame, specified as a scalar value when the Data output type parameter is set to Raw data.

Dependencies

To enable this parameter at the output port, select Enable remote frame output signal under Output Ports.

Parameters

expand all

To edit block parameters interactively, use the Property Inspector. From the Simulink Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.

Interface name of the CAN controller to use, specified as a valid CAN device name. To find the name of the CAN interface, execute the ifconfig command in the host terminal.

Data type used for receiving messages, specified as one of the following types —

  • Raw Data — To receive the message as a uint8 vector array, set Data output type parameter as Raw Data.

  • ROS Msg — To receive the message as a ROS 2 message, set Data output type parameter as ROS Msg. For Port1 to accept a ROS 2 message at the input, you can connect the output of the CAN Receive block to the input port of the Blank Message block.

  • CAN Msg — To receive the message as a structured CAN message, set Data output type parameter as CAN Msg. For Port1 to output a CAN message that was packed using the CAN Pack block, you can connect the output of the CAN Receive block to the input port of the CAN Pack block.

Message identifier type, specified as a Standard (11-bit identifier) or Extended (29-bit identifier) value.

Dependencies

To enable this parameter, set Data output type parameter to Raw Data.

Message identifier, specified as 11 bits for a standard frame or 29 bits for an extended frame. returned in decimal, binary, or hex. It is returned in decimal, binary, or hex. For binary or hex, use bin2dec(' ') or hex2dec(' ') for conversion. This ID is included in the message sent on the CAN bus.

Dependencies

To enable this parameter, set Data output type parameter to Raw Data.

Length of the message received, specified as a positive integer value.

Dependencies

To enable this parameter, set Data output type parameter to Raw Data.

Time frequency at which block receives data, specified as a scalar or vector. You can set it to any value greater than 0 or -1 (for inherited sample time).

This value defaults to a sample time of -1 seconds. When you specify this parameter as -1, Simulink determines the best sample time for the block based on the block context within the model.

Smaller values require the processor to complete the same number of instructions in less time and this can cause task overruns.

Select this parameter to output error codes if the message transfer fails.

Dependencies

To enable this parameter at the output port, select Data output type parameter to Raw Data.

Select this parameter to output remote messages.

To enable this parameter at the output port, select Data output type parameter to Raw Data.

Extended Capabilities

expand all

Version History

Introduced in R2026a