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.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

CAN Bit-Packing block

Simulink^{®} Real-Time™ Library for CAN

This block is for message data of type double. This block constructs CAN data frames, and its output port is ordinarily connected to an input port of a CAN Send driver block. The block has one output port of data type double (a scalar). This port represents the data frame entity constructed by the signals entering the block at its input ports. The number of input ports depends on the setting in the block dialog box.

Do not use this block to construct CAN_MESSAGE data types or to prepare a remote frame. Use the CAN Pack block.

**Bit Patterns**Specify bit patterns. 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 input 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 input port) in the outgoing double value (data frame).From a data type perspective (input ports), the block behaves like a Simulink Sink block, and therefore the data types of the input ports are inherited from the driving blocks.

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

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 pack CAN_MESSAGE data types, use the CAN
Pack block.

Assume that a node on the CAN network must receive a CAN message with identifier 156 having the following data frame content. The data frame must be 6 bytes long.

| Function class of type |

| Function subclass of type |

| Reserved, all bits
must be |

| Bit 0 must be |

| Value of type |

The bit pattern cell array, which bit-packs the data frame according to the above specification, can look as follows:

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

And the Simulink model simulating the required behavior would be as shown.

The function class is of type uint8, which has an example value of

`112`

. This value becomes byte 0 (bits 0–7) of the data frame. Therefore the first bit (element 1 of double array [0:7]) gets bit 0 of the data frame, the second bit 1, and so on. It is easiest to define this mapping by the MATLAB colon operator (:).The function subclass is of type uint8, which has an example value of

`23`

. This value becomes byte 1 (bits 8:15) of the data frame but in reversed bit order. Therefore the first bit (element 1 of double array [15:-1:8]) gets bit 15, the second bit 14, and so on. It is easiest to define this mapping by the MATLAB colon operator (:) and an increment of -1.The reserved byte 2 must have all bits set to 1. If a bit pattern array element does not reference a bit position in the outgoing data frame, the bit is 0 by default. However. you cannot set the bits to 1 as the default. Therefore, you must bring in a uint8 constant with value

`255`

externally. The constant 255 must get to bit positions 16–23 (byte 2) of the outgoing data frame.Bit 0 of data frame byte 3 (bit 24) must be 0. Because 0 is the default value for a nonreferenced bit, you do not need to take an explicit action here.

The flag is of type boolean, which has an example value of

`1`

. This value must become bit 1 of byte 3 (bit 25) of the data frame. Therefore the single bit (element 1 of double array [25]) must get bit 25 of the data frame.The control is of type int8, which has an example value of

`121`

. However, only bits 2–7 must be mapped into the outgoing data frame. In other words, bits 0 and 1 must be thrown away. Indexing of incoming values starts with the first bit (bit 0). Therefore, you must use a special indexing value (-1) to skip bits 0 and 1 of the incoming int8 value. Bits 2–7 are directly mapped to bits 2–7 of byte 3 (bits 26–31) of the outgoing data frame. The resulting bit pattern is`[-1,-1,26:31]`

.The value is of type int16, which has an example value of

`-12270`

. This value must become byte 4 and 5 (bits 32–47) of the outgoing data frame. Therefore the first bit (element 1 of double array [32:47]) must get bit 32 of the data frame, the second bit 33, and so on. It is easiest to define this mapping by the MATLAB colon operator (:).

The output of the CAN Bit-Packing block consists of a double value representing the packed data types within the first 6 bytes. The last 2 bytes are zero. Therefore, even when fewer than 8 bytes are significant, a double value (8 bytes) represents the CAN data frame. The value of the constructed floating-point double does not have a particular meaning but you still see it with a numerical display.

The data frame is then propagated to the CAN Send driver
block and is sent as part of a CAN message having identifier `156`

.
In the Send block dialog box, the data frame size is
6 bytes. Therefore, only the first 6 bytes of the incoming double
value are transmitted as part of the CAN message.

Was this topic helpful?