Home > Software engineering >  Parity check
Parity check

Time:02-25

Check digit calculation method: first the length byte header + function code + + data accumulative sum, accumulate the value of the 16 bit high value added to the low 16 bits, that is the accumulation of highest carry to its lowest level, and finally to invert, the resulting value from high to low in the two number 0 x00,
 

//this is a routine:
Under-16 checksum (u8 * buffer, under-16 size)
{
U32 cksum=0;
U8 I=0;
for(i=0; i{
Cksum +=* buffer++;
}
Cksum=(cksum & gt; & gt; 16) + (cksum & amp; 0 XFFFF);
Cksum +=(cksum & gt; & gt; 16);//why do you want to add this
Return ~ cksum;
}


 
ULONG uSumL=0;//and the cumulative of all data before
For (int n=0; N & lt; IOfst; N++) {uSumL +=pTxData [n]. }//
//low (high 16 + 16) after the
WORD wSum=(WORD) (~ (HIWORD (uSumL) + LOWORD (uSumL)));


Excuse me everybody, the second piece of code that I write right

CodePudding user response:

This is the second piece of code of two calibration results
PTxData [iOfst]=HIBYTE (wSum); IOfst++;
PTxData [iOfst]=LOBYTE (wSum); IOfst++;

And the same effect as the first piece of code is

CodePudding user response:

Contrast windef. H define macro
# define LOWORD (l) ((WORD) (((DWORD_PTR) (l)) & amp; 0 XFFFF))
# define HIWORD (l) ((WORD) ((((DWORD_PTR) (l)) & gt; & gt; 16) & amp; 0 XFFFF))
# define LOBYTE (w) ((BYTE) (((DWORD_PTR) (w)) & amp; 0 XFF))
# define HIBYTE (w) ((BYTE) ((((DWORD_PTR) (w)) & gt; & gt; 8) & amp; 0 XFF))

Bytes can write simple test to compare the

  • Related