CAN Bit-Unpacking

CAN Bit-Unpacking block


Simulink® Real-Time™ Library for CAN


This block is for message data of type double. This block extracts CAN data frames, and its input port is normally connected to an output port of a CAN Receive driver block. The block has one input port of data type double (a scalar), which represents the data frame entity from which the signals are extracted and leaving the block at its output ports. The number of output ports and the data type of each output port depend on the settings in the block dialog box.

Do not use this block to unpack CAN_MESSAGE data types or to process a remote frame. Use the CAN Unpack block.

Block Parameters

Bit Patterns

Lets you define the bit patterns in a flexible way. The data type entered in the control must be a MATLAB® cell array vector. The number of elements in the cell array define the number of output ports shown by this block instance. The cell array elements must be of type double array and define the position of each bit of the incoming value (data typed output port) in the incoming double value (data frame).

Data Types

From a data type perspective (output ports), the block behaves like a Simulink Source block, and therefore the data types of the output ports must be defined in the second control (edit field). The data type entered in that control must be a MATLAB cell array vector of the same length as the bit pattern cell array. The cell array elements must be of type char and define the data type of the corresponding output port. The following values are supported:

boolean, int8, uint8, int16, uint16, int32, uint32

The sample time of the block is inherited from the driving block. Therefore you do not need to provide an explicit sample time in the block dialog box.

If you unpack the data frame into a signed type (int8, int 16, or int 32), the block performs sign extension. For example, if the bit pattern is [0:4], and the data type is int8, you are extracting 5 bits into an 8 bit wide signed type. In this case, bits 5, 6, and 7 are the same as bit 4, resulting in sign extension. This functionality enables you to pack and unpack negative numbers without losing precision. In the preceding example, you can pack and unpack numbers in the range [-16 : 15] (a fictitious int5 type).


The example in this topic describes the use of the Softing® CAN blocks with standard double data types for message storage. For an alternative and easier way to unpack CAN_MESSAGE data types, use the CAN Unpack block.

The functionality of the block is easiest explained by means of an example. The same example as used above shows the functionality of the CAN Bit-Packing block. But in this case, the data frame is sent by an external CAN node and is received by the real-time application running on a Simulink Real-Time system. Therefore the CAN Bit-Unpacking block is used to extract the various data fields from the entire data frame. Because the bit pattern definitions of the packing and unpacking block are symmetric, the bit pattern definition could look exactly the same. You do not need to extract byte 2 (reserved area), because its content is known. The bit pattern edit field can therefore look as follows:

{ [0:7] , [15:-1:8] , [25] , [-1,-1,26:31] , [32:47] }

and the data type edit field as

{ 'uint8' , 'uint8' , 'boolean' , 'int8' , 'int16' }

This leads to the following Simulink model.

In many cases it makes sense to test the bit-packing and unpacking operations in a Simulink model (simulation) before building the real-time application. Both blocks work the same way either in the Simulink system or the generated code. By combining the two models shown so far, a third model emerges that can be used to simulate the behavior.

Was this topic helpful?