Main Content

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 Signal 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.

  1. 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 or CAN_FD_MESSAGE_BUS, respectively. You use this variable later for building your S-functions.

  2. Open a new blank model in Simulink, and add to your model an S-Function Builder (Simulink) block from the block library.

  3. Double-click the S-Function Builder block to open its dialog box. The first function you build is for transmitting.

  4. 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.

      Port and parameter properties in the S-function builder block

      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.

  5. Click Build. The code files are placed in the current working folder of MATLAB.

  6. 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.

  7. 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.

  8. 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.

  9. Add other necessary blocks to your model, including:

  10. 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.

    Custom transmit and receive blocks in a model

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:

  1. Define the IMPORT_SCANUTIL identifier that imports the required symbols when you compile the S-function:

    #define IMPORT_SCANUTIL
  2. Include the can_datatype.h header located inmatlabroot\toolbox\vnt\vntblks\include\candatatype at the top of the S-function:

    #include "can_datatype.h"

    Note

    The header can_message.h included by can_datatype.h is located in matlabroot\toolbox\shared\can\src\scanutil\. See the can_message.h file for information on the CAN_MESSAGE and CAN_DATATYPE structures.

  3. Link your S-function during build to the scanutil.lib located in the matlabroot\toolbox\vnt\vntblks\lib\ARCH folder. The shared library scanutil.dll, is located in the matlabroot\bin\ARCH

  4. Call this function in mdlInitializeSizes (Simulink) to initialize the custom CAN data type:

    mdlInitialize_CAN_datatype(S);
  5. Get custom data type ID using ssGetDataTypeId (Simulink):

    dataTypeID = ssGetDataTypeId(S,SL_CAN_MESSAGE_DTYPE_NAME);
  6. 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 to CAN_MESSAGE:

      ssSetInputPortDataType(S,portID,dataTypeID);

See Also

Functions

Related Topics