Help converting CAN Payload in String format to engineering unit
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I have the following valiable in Matlab which holds the CAN data from a recorder in the following format:
Hexadecimal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1728571/image.jpeg)
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 DBC is as follows:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1728576/image.jpeg)
2 件のコメント
Stephen23
2024 年 7 月 16 日
T = "61,F8,FF,FF";
N = double(typecast(uint8(sscanf(T,'%x,')),'int32'))*0.001
採用された回答
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:
- https://www.mathworks.com/help/matlab/ref/split.html#d126e1564444
- https://www.mathworks.com/help/matlab/ref/uint8.html
- https://www.mathworks.com/help/matlab/ref/hex2dec.html
- https://www.mathworks.com/help/matlab/ref/typecast.html
I hope this helps!
2 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!