u16 CRC16(void * Buff_addr,u16 len)
{
u8 * dataPtr=(u8 *)Buff_addr;
u16 index=0;
u16 crc=0; while(len--)
{
crc=(u8)(crc>>8)|(crc<<8);
crc^=dataPtr[index++];
crc^=(u8)(crc&0xff)>>4;
crc^=(crc<<8)<<4;
crc^=((crc&0xff)<<4)<<1;
} return crc;
}
{
u8 * dataPtr=(u8 *)Buff_addr;
u16 index=0;
u16 crc=0; while(len--)
{
crc=(u8)(crc>>8)|(crc<<8);
crc^=dataPtr[index++];
crc^=(u8)(crc&0xff)>>4;
crc^=(crc<<8)<<4;
crc^=((crc&0xff)<<4)<<1;
} return crc;
}
function CRC16(var Buff_addr; len: Word): Word;
var
index, crc: Word;
dataPtr: PChar;
begin
dataPtr := PChar(@Buff_addr);
index := 0;
crc := 0; while (len > 0) do
begin
crc := Byte((crc shr 8) or (crc shl 8));
crc := crc xor Byte(dataPtr[index]);
Inc(index);
crc := crc xor Byte((crc and $ff) shr 4);
crc := crc xor (crc shl 8) shl 4;
crc := crc xor ((crc and $ff) shl 4) shl 1;
end; Result := crc;
end;
var
index, crc: Word;
dataPtr: PChar;
begin
dataPtr := PChar(@Buff_addr);
index := 0;
crc := 0; while (len > 0) do
begin
crc := Byte(crc shr 8) or (crc shl 8);
crc := crc xor Byte(dataPtr[index]);
Inc(index);
crc := crc xor Byte(crc and $ff) shr 4;
crc := crc xor (crc shl 8) shl 4;
crc := crc xor ((crc and $ff) shl 4) shl 1;
end; Result := crc;
end;
u16 就是双字节 unsigned short;
u8 unsigned char;
原先的函数定义是
function CRC16_Modbus(pByteInfo : PByte; iLength : integer);stdcall;external 'CRC16Modbus.dll';
现在改成
function CRC16(var Buff_addr; len: Word): Word;
编译报错,能不能兼容原来的定义?
可以function CRC16_Modbus(pByteInfo: PByte; iLength : integer);stdcall;external 'CRC16Modbus.dll';
或者
function CRC16_Modbus(pByteInfo: PChar; iLength : integer);stdcall;external 'CRC16Modbus.dll';function CRC16(pByteInfo: PChar; iLength : integer): Word;
var
Data:array[0..2048] of Byte;
iCount,iResult:Integer;
iResult := CRC16_Modbus(@Data,iCount);现在我想直接替换成
iResult := CRC16(@Data,iCount); 这种方式~ 基本不会delphi!
可以的,将CRC16定义为
function CRC16(pByteInfo: array of Byte;; iLength : integer): integer;
如果这样调用iResult := CRC16_Modbus(@Data,iCount);则定义为如下:
function CRC16(Buff_addr: PChar; len: integer): integer;如果这样调用iResult := CRC16_Modbus(Data,iCount);则定义为如下:
function CRC16(Buff_addr: array of Byte; len: integer): integer;
var
index, crc: integer;
dataPtr: PChar;
begin
dataPtr := Buff_addr;
index := 0;
crc := 0; while (len > 0) do
begin
crc := Byte(crc shr 8) or (crc shl 8);
crc := crc xor Byte(dataPtr[index]);
Inc(index);
crc := crc xor Byte(crc and $ff) shr 4;
crc := crc xor (crc shl 8) shl 4;
crc := crc xor ((crc and $ff) shl 4) shl 1;
end; Result := crc;
end; 编译没问题,一运行就报错~ 从这句crc := crc xor Byte(dataPtr[index]); 就开始了
我和c语言里面的比较了一下,第一遍循环的时候crc是对的,到第二遍循环就出错了~
u16 = WORD;
u8 = Byte;
pu16 = PWORD;
pu8 = PByte;function CRC16(Buff_addr : Pointer; Len : u16):u16;
var
dataPtr : PansiChar;
index, crc : u16;
begin
dataPtr := PAnsiChar(Buff_addr);
index := 0;
crc := 0;
while Len > 0 do
begin
crc := (crc shr 8)or(crc shl 8);
crc := crc xor u8(dataPtr[index]);
Inc(index);
crc := crc xor ((crc and $ff) shr 4);
crc := crc xor ((crc shl 8) shl 4);
crc := crc xor(((crc and $ff)shl 4)shl 1); Dec(Len);
end;
Result := crc;
end;和你的方式兼容
var
Data:array[0..2048] of Byte;
iCount,iResult:Integer;
iResult := CRC16_Modbus(@Data,iCount);iResult := CRC16(@Data,iCount);