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;
}

解决方案 »

  1.   


    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;
      

  2.   

    妹子丑,还带游泳圈。你得给出u16, u8的定义,这两个显然是自定义类型或某类型的typedef
      

  3.   

    要修改下,如下: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;
      

  4.   

    多谢,我试下!
    u16 就是双字节 unsigned short;
    u8           unsigned char;
      

  5.   

    还有点问题
    原先的函数定义是
    function CRC16_Modbus(pByteInfo : PByte; iLength : integer);stdcall;external 'CRC16Modbus.dll';
    现在改成
    function CRC16(var Buff_addr; len: Word): Word;
    编译报错,能不能兼容原来的定义?
      

  6.   


    可以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;
      

  7.   

    原来是这样调用的
    var
      Data:array[0..2048] of Byte;
      iCount,iResult:Integer;
    iResult := CRC16_Modbus(@Data,iCount);现在我想直接替换成
    iResult := CRC16(@Data,iCount); 这种方式~ 基本不会delphi!
      

  8.   


    可以的,将CRC16定义为
    function CRC16(pByteInfo: array of Byte;; iLength : integer): integer;
      

  9.   


    如果这样调用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;
      

  10.   

    function CRC16(Buff_addr: PChar; 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]); 就开始了
      

  11.   

    CRC校验是吗?如果是的话,我这里有DELPHI的
      

  12.   

    循环里面少了一个Dec(len);不过也还是有问题, 例如要算{0x04,0x00,0x01} 正确应该是0x4bdb
    我和c语言里面的比较了一下,第一遍循环的时候crc是对的,到第二遍循环就出错了~
      

  13.   

    type
      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);