Clear Filters
Clear Filters

Help converting CAN Payload in String format to engineering unit

5 views (last 30 days)
Hello,
I have the following valiable in Matlab which holds the CAN data from a recorder in the following format:
Hexadecimal
The output of Row 1047 Should be -1951 normally and -1.951 after factoring it by 0.001. This conversion is done using this website.This is basically a force value of a Dynamometer. How can I do this conversion in Matlab? Any known function?
The closest example I found is here, but does not actually solves my problem.
The DBC is as follows:

Accepted Answer

Arjun
Arjun on 15 Jul 2024
As per my understanding you have a table of values and you are specifically interested in converting the payload from a comma separated hexadecimal string to a number in engineering unit and then scale it using the scaling factor of 0.001. One way to do so is as follows:
  • First parse the payload string and remove the comma using the split function to convert into 4x1 string array.
  • Convert the string so obtained to decimal from hexadecimal using hex2dec function.
  • Convert the data to bytes using the uint8 function.
  • Use the typecast function to convert the bytes data to signed 32-bit Integer.
  • Finally apply the scaling factor to get the desired result.
You can refer to the following code that corresponds to the above approach:
%String to be converted
payload_str = "61,F8,FF,FF";
%Conversion to bytes after removing comma and converting to decimal
payload_bytes = uint8(hex2dec(split(payload_str, ',')));
%Interpret as a 32-bit signed integer
raw_value = typecast(payload_bytes, 'int32');
scaling_factor = 0.001;
%apply the scaling factor
engineering_value = double(raw_value) * scaling_factor;
disp(engineering_value);
The code produced an output of –1.951 for the given inputs.
Please refer to the following documentation links for more information on the split, uint8, hex2dec and typecast functions respectively:
I hope this helps!
  2 Comments
Harpreet Singh
Harpreet Singh on 15 Jul 2024
Hello Arjun, thank you for your response. I also resolved this the same way, but I have two approaches as sometime you will have signals spread "partially" across 2 bytes as seen below.
For CAN signals spread partially the code section below will work well.
%% Exaple for partial signals
payload_str = "DF,5A,3F,19,00,B0,1F,05";
%Splitting payload_str
payload_split = split(payload_str,',');
%Reversing (Intel/Little Endian) and appending relevant bytes
payload_required = append(payload_split(7), payload_split(6));
%hexstring to binary
payload_binary = hexToBinaryVector(payload_required);
%Trimming to data bits per the photo above
payload_binary_trimmed = payload_binary(:,1:end-3);
%Trimmed binary to decimal
decimal = binaryVectorToDecimal(payload_binary_trimmed);
%Decimal to engineering values through scaling and offset
scale = 0.08;
offset= -40;
Eng_Value = (decimal*scale)+offset;
All the hard coded values to index the "payload_required" and trim "payload_binary_trimmed" can managed by refering to DBC directly using the "canDatabase" function.
Your method is approcal one when the signal counumes complete bytes.
Thank you once again.
Arjun
Arjun on 16 Jul 2024
Hi Harpreet,
Yes you are correct ! That was good value addition for me.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!