Create Custom CAN Blocks
You can create custom Receive
and Transmit
blocks to
use with hardware currently not supported by Vehicle Network Toolbox™. Choose one of the following work flows.
Blocks Using Simulink Buses (recommended) — Use Simulink® bus signals to connect blocks. Create functions and blocks with S-Function Builder and S-Function blocks.
Blocks Using CAN Message Data Types — Use CAN message data types to share information. Write and compile your own C++ code to define functions, and MATLAB® code to create blocks.
Blocks Using Simulink Buses
To create custom blocks for Vehicle Network Toolbox that use Simulink CAN buses, you can use the S-function builder. For full instructions on building S-functions and blocks this way, see Use a Bus with S-Function Builder to Create an S-Function (Simulink). The following example uses the steps outlined in that topic.
This example shows you how to build two custom blocks for transmitting and receiving CAN messages. These blocks use a Simulink message bus to interact with CAN Pack and CAN Unpack blocks.
Create a Simulink message bus in the MATLAB workspace for CAN or CAN FD.
canMessageBusType
or
canFDMessageBusType
Each of these functions creates a variable in the workspace named
CAN_MESSAGE_BUS
orCAN_FD_MESSAGE_BUS
, respectively. You use this variable later for building your S-functions.Open a new blank model in Simulink, and add to your model an S-Function Builder (Simulink) block from the block library.
Double-click the S-Function Builder block to open its dialog box. The first function you build is for transmitting.
Among the settings in the dialog box, define a function name and specify usage of a Simulink bus.
S-function name:
CustomCANTransmit
Data Properties: Input Ports: Bus:
On
, Bus Name:CAN_MESSAGE_BUS
, as shown in the following figure.For CAN FD, set the bus name to
CAN_FD_MESSAGE_BUS
.
In your function and block building, use the other tabs in the dialog box to define the code for interaction with your device driver, and remove unnecessary ports.
Click Build. The code files are placed in the current working folder of MATLAB.
Place a new S-Function Builder block in your model, and repeat the steps to build an S-function named CustomCANReceive. Use the same settings, except for input and output ports. The receive block output port uses the same bus name as the transmit function input.
Build the receive function, and remove both S-Function Builder blocks from your model. At this point, you can use the files generated by the S-Function Builder as a set of templates, which you can further edit and compile with your own tools. Alternatively, you can use S-Function (Simulink) blocks to run your functions.
Add two S-Function blocks to your model. Open each block, and set its Model Parameters S-function name field, so you have one each of CustomCANTransmit and CustomCANReceive.
At this point you could create a mask for each block to allow access to parameters for your hardware. This example does not need masks for these blocks.
Add other necessary blocks to your model, including:
Set the block parameters and connections.
A typical model might look like this. Here a Constant (Simulink) block and a Display (Simulink) block allow verification of connections and model behavior.
Blocks Using CAN Message Data Types
Note
For ease of design and to take advantage or more Simulink features, it is recommended that you use Simulink buses instead of CAN message data types when possible. See Blocks Using Simulink Buses.
To create your own blocks for use with other Vehicle Network Toolbox blocks, can use a custom CAN data type. Register this custom CAN data type in a C++ S-function.
Note
You must use a C++ file type S-function (.cpp
) to create custom
blocks that use CAN message data types. Using a C-file type S-function
(.c
) might cause linker errors.
To register and use the custom CAN data type, in your S-function:
Define the
IMPORT_SCANUTIL
identifier that imports the required symbols when you compile the S-function:#define IMPORT_SCANUTIL
Include the
can_datatype.h
header located in
at the top of the S-function:matlabroot
\toolbox\vnt\vntblks\include\candatatype#include "can_datatype.h"
Note
The header
can_message.h
included bycan_datatype.h
is located in
. See thematlabroot
\toolbox\shared\can\src\scanutil\can_message.h
file for information on theCAN_MESSAGE
andCAN_DATATYPE
structures.Link your S-function during build to the
scanutil.lib
located in the
folder. The shared librarymatlabroot
\toolbox\vnt\vntblks\lib\ARCH
scanutil.dll
is located in thematlabroot
\bin\ARCH
Call this function in
mdlInitializeSizes
(Simulink) to initialize the custom CAN data type:mdlInitialize_CAN_datatype(S);
Get custom data type ID using
ssGetDataTypeId
(Simulink):dataTypeID = ssGetDataTypeId(S,SL_CAN_MESSAGE_DTYPE_NAME);
Do one of the following:
To create a receive block, set output port data type to
CAN_MESSAGE
:ssSetOutputPortDataType(S,portID,dataTypeID);
To create a transmit block, set the input port type to
CAN_MESSAGE
:ssSetInputPortDataType(S,portID,dataTypeID);
See Also
Functions
Related Topics
- C/C++ S-Function Basics (Simulink)
- Use a Bus with S-Function Builder to Create an S-Function (Simulink)