필터 지우기
필터 지우기

how can I pack the data in simulink without Vehicle Toolbox?

조회 수: 4 (최근 30일)
Gabriela
Gabriela 2014년 4월 2일
편집: Adel Fernandez 2019년 3월 26일
how can I pack the data in simulink without Vehicle Toolbox?

답변 (1개)

Adel Fernandez
Adel Fernandez 2019년 3월 25일
편집: Adel Fernandez 2019년 3월 26일
data = zeros(1,8);
data = can_pack(data, hex2dec('FA0') ,8,12);
data = can_pack(data, hex2dec('C0BC4') ,22,20);
data = can_pack(data, hex2dec('2') ,45,2);
dec2hex(data)
function data = can_pack(data,value,start,len)
masks = [1,3,7,15,31,63,127,255];
while len > 0
%% compute local variables in current iteration
byte_index = floor(start/8)+1; % get byte inside payload
bits_to_shift = mod(start,8); % get start bit inside byte
% get bits quantity to put inside a byte
bits_packed = (8-bits_to_shift);
if bits_packed > len
bits_packed = len;
end
mask = masks(bits_packed); % get the mask to use, alternatives bitshift(1,bits_packed)-1 or 2^bits_packed-1
%% update message payload
% bit operation data[i] = data[i] | ( (value & mask) << bits)
data(byte_index) = bitor(data(byte_index),bitshift( bitand(mask,value),bits_to_shift));
%% update input parameters to next iteration
start = start + bits_packed; % update start bit to continue processing
value = bitshift(value,-bits_packed); % update value to continue processing
len = len - bits_packed; % update the length to continue processing
end
end

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by