Implementing a Checksum algorithm as Embedded Matlab Function

20 visualizzazioni (ultimi 30 giorni)
Hello,
I have been trying implement a checksum algorithm. I have implmeneted a matlab function that converts the inputs to binary and then performs some bit-wise operations to compute the checksum. Unfortunately, this does not work as an embedded matlab function because of several reasons: 1) some functions including dec2bin, num2str, int2bin etc.. cannot be compiled 2) MxArray is not supported
The function I have coded is as follows. Is there anyway I can get these functions to compile or another way to perform functions such as the int2bin function. Thank you in advance for your help and suggestions.
function [chec , intcheck] = checksum (torque, extc, messageCntr)
% create dataArray for checksum from inputs
if (torque < 0)
pos_torque = abs(torque);
p = fliplr (bitget(bitcmp(uint16(pos_torque)), 1:16))
p = num2str(p)
char1 = dec2bin(bin2dec(p)+bin2dec('0000000000000001'))
torque_fin = char1-'0';
else
torque_fin = fliplr(bitget(torque, 1:16));
end
byte_0=torque_fin(1:8);
byte_1=torque_fin(9:16);
byte_2=zeros(1,8);
byte_3=zeros(1,8);
byte_4=zeros(1,8);
byte_5=zeros(1,8);
bits_extc = fliplr(bitget(extc, 1:4));
bits_messageCntr = fliplr(bitget(messageCntr, 1:4));
byte_6 = [bits_extc, bits_messageCntr];
dataArray = [byte_0, byte_1, byte_2, byte_3, byte_4, byte_5, byte_6];
% checksum
%initialization
dataLength= 6;
startValue = [0,0,0,1,1,0,1,1]; % 1B
genPol = [1,0,0,1,1,0,1,1]; %9B
finalXor = [1,1,1,1,1,1,1,1]; %FF
byteCount =0;
bitCount = 0;
chec = 0;
chec = startValue;
for byteCount = 0:dataLength
chec = bitxor(dataArray (byteCount*8+1: (byteCount*8 +8)), chec);
%C = dataArray (byteCount*8+1: (byteCount*8 +8))
D = chec
for bitCount=1:8
if (chec(1) ~= 0)
chec = bitxor([chec(2:8),0], genPol);
else
chec = [chec(2:8),0];
end
end
end
chec = bitxor(chec, finalXor);
intcheck = bin2dec(num2str(chec));
end

Risposte (1)

Sriram Narayanan
Sriram Narayanan il 6 Mag 2015
I simplified your code to not include the condition for negative torque (which could be implemented using Simulink blocks) and I was able to calculate the correct checksum. Please note that functions like "num2str" cannot be compiled and therefore, need to be declared as extrinsic functions using the coder.extrinsic keyword. Only thing that you have to ensure is that the outputs to your function have their sizes pre-defined using a pre-initialization.
More information about this function is below:
Please find the working model which consists of the MATLAB function block attached.
  2 Commenti
NGR MNFD
NGR MNFD il 2 Ott 2021
hello dear ...
How can I check the checksum of the force signal binary file? This force signal is measured by a 12-bit adc. I was able to display it through the following method.
fileID1=fopen('control1.let','r');
A= fread(fileID1, [3, 45000], 'uint8')'; % matrix with 3 rows, each 8 bits long, = 2*12bit
fclose(fileID1);
M2H= bitshift(A(:,2), -4);
M1H= bitand(A(:,2), 15);
PRL=bitshift(bitand(A(:,2),8),9); % sign-bit
PRR=bitshift(bitand(A(:,2),128),5); % sign-bit
M( : , 1)= bitshift(M1H,8)+ A(:,1)-PRL;
M( : , 2)= bitshift(M2H,8)+ A(:,3)-PRR;
N1 = reshape(M',[90000,1]); plot(N1);
Can I also check to see if it is correct or not? I do not know what code to use in MATLAB to calculate checksum? Please show me. Thank you.
Jan
Jan il 2 Ott 2021
@NGR MNFD: Please do not attach a new question as a comment to an answer of another question. Create a new question instead. Thanks.

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by