Help converting CAN Payload in String format to engineering unit

5 次查看(过去 30 天)
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:

采纳的回答

Arjun
Arjun 2024-7-15
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 个评论
Harpreet Singh
Harpreet Singh 2024-7-15
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 2024-7-16
Hi Harpreet,
Yes you are correct ! That was good value addition for me.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by