Receive CAN messages from specified CAN device
The CAN Receive block receives messages from the CAN network and delivers them to the Simulink model. It outputs one message or all messages at each timestep, depending on the block parameters.
Note: You need a license for both Vehicle Network Toolbox™ and Simulink® software to use this block.
The CAN Receive block has two output ports:
The f() output port is a trigger to a Function-Call subsystem. If the block receives a new message, it triggers a Function-Call from this port. You can then connect to a Function-Call Subsystem to unpack and process a message.
The CAN Msg output port contains a CAN message received at that particular timestep.
The CAN Receive block stores CAN messages in a first-in, first-out (FIFO) buffer. The FIFO buffer delivers the messages to your model in the queued order at every timestep.
Note: You cannot have more than one Receive block in a model using the same NI-CAN, NI-XNET, or PEAK-System device channel.
The CAN Receive block supports the use of Simulink Accelerator™ mode. Using this feature, you can speed up the execution of Simulink models.
For more information on this feature, see the Simulink documentation.
The CAN Receive block supports the use of code generation along with the packNGo function to group required source code and dependent shared libraries. For more information, see Code Generation.
Vehicle Network Toolbox Simulink blocks allow you to generate code, enabling models containing these blocks to run successfully in Accelerator, Rapid Accelerator, External, and Deployed modes.
You can use Vehicle Network Toolbox, Simulink Coder™, and Embedded Coder®products together to generate code (on the host end) that you can use to implement your model for a practical application. For more information on code generation, see Program Builds.
The block generates code with limited portability. The block uses precompiled shared libraries, such as DLLs, to support I/O for specific types of devices. With this block, you can use the packNGo function supported by the Simulink Coder, to set up and manage the build information for your models. The packNGo function allows you to package model code and dependent shared libraries into a zip file for deployment. You do not need MATLAB® installed on the target system, but the target system needs to be supported by MATLAB.
To set up packNGo:
set_param(gcs, 'PostCodeGenCommand', 'packNGo(buildInfo)');
In this example, gcs is the current model that you wish to build. Building the model creates a zip file with the same name as model name. You can move this zip file to another machine and the source code in the zip file can be built to create an executable which can be run independent of MATLAB and Simulink. The generated code compiles with both C and C++ compilers. For more information on packNGo, see packNGo.
Use the Source Block Parameters dialog box to select your CAN Receive block parameters.
Select the CAN device and a channel on the device you want to receive CAN messages from. This field lists all the devices installed on the system. It displays the vendor name, the device name, and the channel ID. The default is the first available device on your system.
Select the filter on this block for standard IDs. Valid choices are:
Allow all: allows all standard IDs to pass the filter. This is the default filter state of the CAN Receive block
Allow only: Allows only ID or range of IDs specified in the text field. You can specify a single ID or an array of IDs. You can also specify disjointed IDs or arrays separated by a comma. For example, to accept IDs from 400 through 500 and 600 through 650, enter [[400:500] [600:650]]. Standard IDs must be a positive integer from 0 through 2047. You can also specify a hexadecimal value using the hex2dec function.
Block all: Blocks all standard IDs from passing the filter.
Select the filter on this block for extended IDs. Valid choices are:
Allow all: allows all extended IDs to pass the filter. This is the default filter state of the CAN Receive block
Allow only: Allows only ID or range of IDs specified in the text field. You can specify a single ID or an array of IDs. You can also specify disjointed IDs or arrays separated by a comma. For example, to accept IDs from 3000 through 3500 and 3600 through 3620, enter [[3000:3500] [3600:3620]]. Extended IDs must be a positive integer from 0 through 536870911. You can also specify a hexadecimal value using the hex2dec function.
Block all: Blocks all extended IDs from passing the filter.
Specify the sampling time of the block during simulation, which is the simulation time as described by the Simulink documentation. This value defines the frequency at which the CAN Receive block runs during simulation. If the block is inside a triggered subsystem or to inherit sample time, you can specify –1 as your sample time. You can also specify a MATLAB variable for sample time. The default value is 0.01 (in seconds).
Select how many messages the block receives at each specified timestep. The choices are 1 and all. By default, the block receives one message at each timestep. Then, the FIFO buffer delivers one new message to the Simulink model. If the block does not receive any message before the next timestep it outputs the last received message.
If you select all, the CAN Receive block delivers all available messages in the FIFO buffer to the model during a specific timestep. The block generates one function call for every message delivered to the model for that particular timestep. The output port always contains one CAN message at a time.