How can I get the raw CAN message using canMessage

21 views (last 30 days)
I want to have the raw CAN message as hex using the CAN.message:
db = canDatabase('example.dbc'); %load dbc file
message = canMessage(db,'exampleMessage');% create can message considering dbc file
message.Signals.exampleData = 34; %change data
canRaw = join([dec2hex(message.ID) dec2hex(message.Length) arrayfun(@(x) dec2hex(x,2),[message.Data],'UniformOutput',0)],''); %build frame according to dbc
I'm searching for a build in function that creates the content for "canRaw":
canRaw = CanMessage2hex(message); %function for converting message to can frame (raw CAN message) in hex
The aim is to compare the data on the bus using a bus monitor with the data created by MATLAB.

Answers (1)

Aditya
Aditya on 24 Jan 2024
Edited: Aditya on 24 Jan 2024
Hi Henning,
MATLAB does not have a built-in function that directly converts a canMessage object to a raw CAN frame string in hexadecimal format. However, you can create a custom function that performs this conversion for you. The function you've outlined in your code snippet is a good starting point.
Here's a simple custom function that takes a canMessage object and converts it to a hexadecimal string representing the raw CAN message:
function canRaw = CanMessage2hex(message)
% Convert the message ID, Length, and Data to hexadecimal format
idHex = dec2hex(message.ID, 3); % Assuming standard 11-bit ID
lengthHex = dec2hex(message.Length, 1);
dataHex = dec2hex(message.Data)';
dataHex = dataHex(:)';
% Concatenate the hex strings to form the raw CAN message
canRaw = strcat(idHex, lengthHex, dataHex);
end
To use this function with your canMessage object, simply call it like this:
db = canDatabase('example.dbc'); % Load dbc file
message = canMessage(db, 'exampleMessage', false); % Create CAN message considering dbc file
message.Signals.exampleData = 34; % Change data
canRaw = CanMessage2hex(message);
Please note the following considerations when using this function:
  • The dec2hex function is used to convert decimal numbers to their hexadecimal string representation. The second argument specifies the minimum number of digits in the output string. For the message ID, you might need to adjust the number of digits based on whether you're using standard (11-bit) or extended (29-bit) CAN IDs.
  • The canMessage object's Data property is an array of bytes. The function converts these bytes to a single hexadecimal string.
  • This function assumes that the Data property of the canMessage object contains all the bytes you want to send in the CAN frame. If your message has less than 8 bytes of data, ensure that the Length property of the canMessage object is set correctly to reflect the actual number of data bytes.
  • If the Data array contains fewer elements than the Length property indicates, MATLAB might throw an error because it expects the Data array to have a number of elements equal to the Length property.
Make sure to test the function with your specific use case to ensure it meets your requirements.

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!