Need to do a 8-Bit Fletcher Algorithm checksum.

5 次查看(过去 30 天)
I have to sent serial command to a gps receiver but it is necessary create 2 checksum bytes after all commands.
The receiver protocol is the 8-bit Fletcher.
The number of bytes in the message can be variable. The algorithme in an other language is:
%%%%%%%%%
CK_A = 0, CK_B = 0
For(I=0;I<N;I++)
{
CK_A = CK_A + Buffer[I]
CK_B = CK_B + CK_A
}
%%%%%%%%%%
I'would not prefare use the famous 'dec2bin' if possible
thank you

回答(1 个)

Rushil
Rushil 2025-1-24
Hi Patrice
From the provided implementation, I assume that the buffer is a sequence of 8-bit integers for which we need to compute the checksums. We can use MATLAB to easily accomplish this task using mod” function (to retain the 8-bit nature of the integers). Below you will find implementation of the same:
function [CK_A, CK_B] = fletcher(Buffer)
CK_A = uint8(0);
CK_B = uint8(0);
N = length(Buffer);
for I = 1:N
% use mod with 2^8 (which is 256)
CK_A = mod(CK_A + uint8(Buffer(I)), 256);
CK_B = mod(CK_B + CK_A, 256);
end
end
% an example of using it
Buffer = [1, 2, 3, 4, 5, 6];
[CK_A, CK_B] = fletcher(Buffer)
To help with understanding the function, you may find the documentation of “mod” below:
Hope it helps you out
larush
  3 个评论
Rushil
Rushil 2025-1-24
Hi Walter
Thanks for the response. I may have overlooked this fact, since I am used to using macros in C++.
#define int long long
Usually most operations (multiplication too) performed modulo some 32-bit number work, since the 64-bit operations allow the operations on large integers, which would otherwise overflow. I guess this is a good opportunity for me to learn more about this in MATLAB.
Your comment helps not only 1, but 2 people, thank you for the clarification.
larush

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by