This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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). This port 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 example in the CAN Bit-Packing shows the transmission of a data frame to an external CAN node. In this case, an external CAN node sends the data frame and the real-time application running on a target computer receives it. Therefore, you use the CAN Bit-Unpacking block 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 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' }

These requirements result in the following Simulink model.

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

See Also

Introduced before R2006a

Was this topic helpful?