function GetCRCWord(const DataStr: string): Word;
var
Crc16: word;
aByte: Byte;
tmpStr: string;
i, j: integer;
begin
Crc16 := $0000;
if DataStr = '' then Exit;
tmpStr := DataStr + #0#0;
for i := 1 to Length(tmpStr) do
begin
aByte := Ord(tmpStr[i]);
for j := 0 to 7 do
begin
if (crc16 and $8000) <> 0 then // 判斷 crc16 首位是否等於 1
begin
crc16 := (Crc16 shl 1) xor (aByte shr 7);
crc16 := crc16 xor $1021;
end
else crc16 := (Crc16 shl 1) xor (aByte shr 7);
aByte := aByte shl 1;
end; // for
end;
Result := Crc16;
end;
暫時還沒有驗證方法:
將接收的數據,crc碼交換高低位,重新進行CRC校驗,如果得到0 證明數據正確。