CodePudding user response:
__stdcall unsigned short uart_crc (unsigned char * data, long length)
{
Unsigned short reg_crc;
Unsigned short s_crcchk;
S_crcchk=0;
Reg_crc=0 XFFFF;
While (length -)
{
Reg_crc ^=* data++;
For (s_crcchk=0; S_crcchk & lt; 8; S_crcchk + +)
{
If (reg_crc & amp; 0 x01)
{
Reg_crc=(reg_crc & gt;> 1) ^ 0 xa001;
}
The else
{
Reg_crc=reg_crc & gt;> 1;
}
}
}
Return reg_crc;
}
PB:
The function int uart_crc (ref string s, long len) library...
CodePudding user response:
Do you have any pb?CodePudding user response:
Global type xor16 from function_object
End type
Forward as
Global function unsignedinteger xor16 (unsignedinteger unsignedinteger p1, p2)
End as
Global function unsignedinteger xor16 (unsignedinteger unsignedinteger p1, p2); Uint ret, n
For n=0 to 15
If the mod (long (p1/(2 ^ n)), 2) & lt;> The mod (long (p2)/(2 ^ n)), 2) then
Ret +=2 ^ n
End the if
End for
Return ret
End the function
Global type uart_crc16 from function_object
End type
Forward as
Global function unsignedinteger uart_crc16 (character data [], unsignedinteger len)
End as
Global function unsignedinteger uart_crc16 (character data [], unsignedinteger len); Uint ret, n, I
Ret=65535
For I=1 to len
Ret=xor16 (ret, asc (data) [I])
For n=1 to 8
If the mod (ret, 2)=1 then
Ret=xor16 (long (ret/2), 40961)
The else
Ret=long (ret/2)
End the if
End for
End for
Return ret
End the function
Call:
Char data []
Data [1]='~ h23'
Data [2]='~ h3a'
Data [3]='~ h00'
Data [4]='~ h01-2'
Data [5]='~ h02'
Data [6]='~ h00'
Uint ret
Ret=uart_crc16 (data, 6)
Messagebox (' ', ret)
Return
CodePudding user response:
Learning how to learn!!!!!!!!!!!!!!!CodePudding user response:
MarkCodePudding user response:
Mark