필터 지우기
필터 지우기

Receiving CAN Massages with Vehicle Network Toolbox

조회 수: 5 (최근 30일)
Lukas
Lukas 2024년 4월 8일
답변: Michael VanMeter 2024년 6월 6일
canChannel1 = canChannel("Vector", "VN7610 1", 2);
candb = canDatabase("Namedbc.dbc");
Message1 = canMessage(candb,"Message1");
Message2 = canMessage(candb,"Message2");
% Start CAN Communication "receive"
start(canChannel1);
Message1 = receive(canChannel1,inf,"OutputFormat","object");
Signal1 = unpack(Message1 , 16, 16, 'LittleEndian', 'int16')
stop(canChannel1);
start(canChannel1); % this stopping and starting the canchannel is only for debug purposes
Message2 = receive(canChannel1,inf,"OutputFormat","object");
Signal2 = unpack(Message2, 0, 16, 'LittleEndian', 'int16')
stop(canChannel1);
Hello,
the matlab code above shows my testscript to get signals out of a CAN bus. I simulate the data on the CAN BUS with a tool called "BUSMASTER". With this tool i can send data in a CAN Message, which belongs to a dbc file. My hardware setup is a PEAK-System device to send CAN messages and Vector device to receive the messages from the bus.
So my problem is that, when i send more than one message at the a time, i not always get the rigth value of the signal. I dont understand how the messages can disturb each other. So here is the "MessageCount" put to "inf" to see how the value changes if i add another can message. If i add another can massage there follows to the existing one, the existing gets another wrong value in the vector.
Example: 3 Can Messages are activ and 4000 is the value i want to send -> [4000 0 0 4000 0 0 4000 0 0]
At the end i only want one value when the script is used.
Whould be very nice if someone could help me with my problem.
Thanks.

채택된 답변

Michael VanMeter
Michael VanMeter 2024년 6월 6일
The receive function reads in all messages on the CAN bus. So if you have more than one message, the messages in the output array will be a mix of all the messages in the order they were recieved. So when you try to unpack that message array, it's doing so on each message regardless of ID. You need to extract the messages of like ID and unpack them appropriately. Below is one method for extracting the signal info from teo different messages. I opted to use the signal info from the database in the unpack function rather than hardcoding. I've attached the DBC file (as a Zip archive) I used for debugging this code.
candb = canDatabase("MyDatabase.dbc");
canCase = canChannel("Vector", "CANcaseXL 1", 1);
canCase.SilentMode = false;
canCase.Database = candb;
% Start CAN Communication
disp('Starting CAN Communications')
start(canCase);
messages = receive(canCase,20,"OutputFormat","object");
message1 = extractAll(messages,'Message_1');
msgInfo = messageInfo(candb,'Message_1');
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_1');
signal_1 = unpack(message1,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_2');
signal_2 = unpack(message1,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_5');
signal_5 = unpack(message1,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
message2 = extractAll(messages,'Message_2');
msgInfo = messageInfo(candb,'Message_2');
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_3');
signal_3 = unpack(message2,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_4');
signal_4 = unpack(message2,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_6');
signal_6 = unpack(message2,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_7');
signal_7 = unpack(message2,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
sigInfo = signalInfo(candb,msgInfo.ID,msgInfo.Extended,'Signal_8');
signal_8 = unpack(message2,sigInfo.StartBit,sigInfo.SignalSize,sigInfo.ByteOrder,sigInfo.Class);
% Stop CAN Communication
disp('Stopping CAN Communications')
stop(canCase);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Vehicle Network Toolbox에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by