How to pack message for FCAN_Send / unpack message for FCAN_Receive

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to pack message for FCAN_Send / unpack message for FCAN_Receive

Jump to solution
1,866 Views
johngreenjets
Contributor III

CAN packing

At the moment I am having to manually pack CAN messages using bit shifts, masks, mux etc in order to construct 8 bytes of data to transmit a CAN message.  An example of one such message is shown below...

 

johngreenjets_0-1684499366144.png

However, this is very tedious especially when there are lots of CAN messages to pack.

I came across the embedded coder block 'CAN pack' which imports the CAN database, but unfortunately I cannot get this to work.

There is a data type mismatch because the 'CAN Pack' output is not a structure...

johngreenjets_1-1684499782245.png

 

The 'CAN pack' block does not seem to output structures...

johngreenjets_2-1684499952227.png

Should this 'CAN pack' block work with the 'FCAN_Send' block and if so, how ? 

If not, is there another way of packing the CAN messages (preferably by importing a CAN database) ?

 

CAN unpacking

I haven't tried unpacking signals from the 'FCAN_Receive' block yet, so far I have only unpacked the 8 raw bytes of data as shown below... 

 

johngreenjets_3-1684500311364.png

For testing purposes I have made byte 1 control a digital output (LED).

How can I extract the signals from the 8 bytes of raw data, without having to manually implement the unpacking using bit shifts, masks, mux etc

I tried the embedded coder block 'CAN unpack' which imports the CAN database, but unfortunately I cannot get this to work.

There is a data type mismatch because the 'CAN unpack' input does not allow structures...

johngreenjets_4-1684501055288.png

Should this 'CAN unpack' block work with the 'FCAN_Receive' block and if so, how ? 

If not, is there another way of unpacking the CAN messages (preferably by importing a CAN database) ?

 

1 Solution
1,817 Views
johngreenjets
Contributor III

UPDATE: 

I have managed to get 'CAN Unpack' to work as follows:

1. Create the datatype 'CAN_MESSAGE_BUS' by running the command 'canMessageBusType' at the MATLAB prompt.  To see the structure of the datatype, run 'CAN_MESSAGE_BUS.Elements'...

johngreenjets_0-1684832781179.png

2. Create a bus and set the output data type to 'CAN_MESSAGE_BUS'.  The inputs to the bus are as follows:

  • Extended
    • datatype uint8
  • Length 
    • datatype uint8
  • Remote 
    • datatype uint8
  • Error
    • datatype uint8
  • ID
    • datatype uint32
  • Timestamp
    • datatype double
  • Data 
    • datatype uint8(8)

 

johngreenjets_1-1684833145728.png

3. The following outputs from the NXP Rx Complete block are used as the inputs to the 'CAN Unpack' block...

  • ID
  • Data
  • Length 

johngreenjets_2-1684833597344.png

 

 

View solution in original post

3 Replies
868 Views
Vasappanavara
Contributor II

Simply awesome John to document your work for the benifit of the community . I am also stuck on this issue , well check on your suggested solution.

1,818 Views
johngreenjets
Contributor III

UPDATE: 

I have managed to get 'CAN Unpack' to work as follows:

1. Create the datatype 'CAN_MESSAGE_BUS' by running the command 'canMessageBusType' at the MATLAB prompt.  To see the structure of the datatype, run 'CAN_MESSAGE_BUS.Elements'...

johngreenjets_0-1684832781179.png

2. Create a bus and set the output data type to 'CAN_MESSAGE_BUS'.  The inputs to the bus are as follows:

  • Extended
    • datatype uint8
  • Length 
    • datatype uint8
  • Remote 
    • datatype uint8
  • Error
    • datatype uint8
  • ID
    • datatype uint32
  • Timestamp
    • datatype double
  • Data 
    • datatype uint8(8)

 

johngreenjets_1-1684833145728.png

3. The following outputs from the NXP Rx Complete block are used as the inputs to the 'CAN Unpack' block...

  • ID
  • Data
  • Length 

johngreenjets_2-1684833597344.png

 

 

1,856 Views
johngreenjets
Contributor III

UPDATE: 

I have managed to get 'CAN Pack' to work, but I'm not having any luck with 'CAN Unpack'.

To get 'CAN Pack' to work, I set the output as a bus...

johngreenjets_0-1684503766194.png

 

The bus elements are shown below...

johngreenjets_1-1684503941760.png

 

In this case I selected the bus elements Data, Length and ID to use with the FCAN_Send block.

I tried creating the same bus for 'CAN Unpack', but this did not work...

 

johngreenjets_3-1684504322692.png