主要内容

CAN Receive

R2026b

Receive message from CAN network

Since R2026b

  • CAN Receive block

Libraries:
Embedded Coder Support Package for Real-Time Linux / SocketCAN

Description

Add-On Required: This feature requires the Embedded Coder Support Package For Real-Time Linux add-on.

The CAN Receive block receives messages from the CAN network. This block uses SocketCAN which is a device-independent driver for implementing the CAN interface supporting all CAN shields.

The CAN Receive block supports Raw data and CAN Msg as output types. To use the bus mode, create a CAN_MESSAGE_BUS type in the base workspace. See canMessageBusType (Vehicle Network Toolbox) function. To use a CANdb (CAN database) file or to specify signals manually, use CAN Msg output type and the CAN Unpack (Simulink Real-Time) block.

You can use CAN Receive block to interact with a real or virtual CAN interface. For more information on how to set up a virtual CAN interface, see Transmit and Receive CAN Raw Data and Messages on Virtual Linux Target Device.

Note

To generate code C/C++ code for this block, you must have an Embedded Coder® license.

Examples

Ports

Output

expand all

Received message data, returned as a vector or scalar.

Data Types: uint8 | CAN Msg

Status of the received output message.

Data Types: uint8

Error codes.

Dependencies

To enable this parameter, select Enable error output signal.

0000000000RXWARRXEPRXOVR
bit7bit6bit5bit4bit3bit2bit1bit0

RXOVR: Receive Buffer Overflow Flag bit

RXEP: Receive Error-Passive Flag bit

RXWAR: Receive Error Warning Flag bit

Remote message flag.

Dependencies

To enable this parameter, select Enable remote frame output signal.

Parameters

expand all

Enter the name of the real or virtual CAN interface. To find the name of the CAN interface, in the target Linux device terminal, execute the ifconfig command. To configure the CAN interface, see the target Linux device documentation.

Programmatic Use

Block Parameter: DeviceID

Select a type to receive message.

  • Raw data – To receive message as a 1-by-N uint8 array, select Data output type as Raw data.

  • CAN Msg – To receive message in CAN message format, select Data output type as CAN Msg and then perform these steps:

    1. Add a CAN Unpack (Simulink Real-Time) block to your Simulink® model.

    2. Connect the output of the CAN Receive block to the input of the CAN Unpack (Simulink Real-Time) block.

    3. Using the options in the Data output type list of the CAN Unpack (Simulink Real-Time) block, specify if you want to create your messages or you want to upload a CAN database file. If you choose to upload a CAN database file, the CAN Unpack (Simulink Real-Time) inherits the message properties from the uploaded file.

Programmatic Use

Block Parameter: OutputDataType

The type of message identifier.

Dependencies

This parameter appears only when you select Data output type as Raw data.

Programmatic Use

Block Parameter: IdentifierType

Message identifier, which is 11 bits long for the standard frame size or 29 bits long for the extended frame size, specified in decimal, binary, or hex. For binary and hex formats, use bin2dec(' ') and hex2dec(' '), respectively, to convert the entry. The message identifier is coded into a message that is sent to the CAN bus.

Dependencies

This parameter appears only when you select Data output type as Raw data. To output the identifier of the CAN Msg, select Output identifier of the CAN Unpack (Simulink Real-Time) block.

Programmatic Use

Block Parameter: MessageId

The length of the message, in bytes.

Dependencies

This parameter appears only when you select Data output type as Raw data. To output the length of the CAN Msg, select Output length of the CAN Unpack (Simulink Real-Time) block.

Programmatic Use

Block Parameter: MsgLength

Specify how often the block receives message, in 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.

Programmatic Use

Block Parameter: SampleTime

When you select the Enable error output signal parameter, the block configures an output port, Error. The port outputs the error code as a uint8 integer. Each bit in code corresponds to the type of error in the received message. An output of 0 indicates that the message is error-free. Any value other than 0 indicates that the message is an error frame.

CAN Receive block output bit definitions

Dependencies

This parameter appears only when you select Data output type as Raw data. To output the error in the CAN Msg, select Output error of the CAN Unpack (Simulink Real-Time) block.

Programmatic Use

Block Parameter: OutputError

Select this parameter to enable the Remote output port. This port outputs the message remote frame status.

Dependencies

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

Programmatic Use

Block Parameter: OutputRemote

Extended Capabilities

expand all

Version History

Introduced in R2026b