crc16的校验问题,我试过以前的论坛的结果,好像都不对???急!(50分)

  • 主题发起人 主题发起人 onewater
  • 开始时间 开始时间
O

onewater

Unregistered / Unconfirmed
GUEST, unregistred user!
大家看下面这段程序:mplementation

{$R *.dfm}
function CRC16(const AData: array of Byte): String;
var
I, Flag: Integer;
CRC16High, CRC16Low, SaveHigh, SaveLow: Byte;
begin
CRC16High:= $FF;
CRC16Low:= $FF;

for I:=0 to 3 do
begin
CRC16Low:= CRC16Low xor AData;
for Flag:= 0 to 7 do
begin
SaveHigh:= CRC16High;
SaveLow:= CRC16Low;
CRC16High:= CRC16High shr 1;
CRC16Low:= CRC16Low shr 1;
//SaveHigh:= CRC16High;
//SaveLow:= CRC16Low;
if (SaveHigh and $1)=$1 then
CRC16Low:= CRC16Low or $80;
if (SaveLow and $1)=$1 then
begin
CRC16High:= CRC16High xor $A0;
CRC16Low:= CRC16Low xor $01;
end;
end;
end;
Result:= IntToHex(CRC16High,2) + IntToHex(CRC16Low,2);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
shuju:array[0..3] of byte;
begin
shuju[0]:=$00;
shuju[1]:=$01;
shuju[2]:=$00;
shuju[3]:=$03;
label1.caption:=crc16(shuju);
end;
end.
多项式码为A001,现校验00010003,怎么结果为E511啊,正确结果好像是55E9,谁能解答啊?或者有其它的正确的程序
 
是不是这个问题太难了啊?[8D]
 
算了,这个问题不解答了。。那位高手编写过modbus的程序啊?

另加200分,请高手指点啊,特急
 
看我的论坛,上面有解答,www.lanboy.net
 
而且你最好不要临时计算,最好还是使用表的方式,因为如果临时算的话,大量数据的计算速度太慢了。
 
天使兄:
你说的你的论坛上的内容就是你上传的那个压缩文件是吧?
我看看里面有没有modbus的编程或者crc算法先。谢过
 
对不起,忘记贴上来了,对不起,请再次访问。
 

我这里有c写的crc例程,你看看对你有没有用
#define uint unsigned int
#define uchar unsigned char
uchar rxd[6]={0x01,0x03,0x20,0x00,0x01,0x00};
void main()
{
uint reg_crc=0xffff;
uchar length=6;
uchar x,j;
while(length--){
reg_crc^=rxd[x];
x++;
for(j=0;j<8;j++){
if(reg_crc&amp;0x01){
reg_crc=(reg_crc>>1)^0xa001;
}else{
reg_crc=reg_crc>>1;
}
}
};
}
 
unit Main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
GenPoly: Word=$1021; // 生成多项式的值
// $1021, CRC-CCITT, X16+X12+X5+1
// $F005, CRC-16, X16+X15+X2+1
CRCTab: array [0..255] of Word;

function CalCRC16(data, crc, genpoly: Word): Word;
function CalCRC16_2(data, crc, genpoly: Word): Word;
function QuickCalCRC16(data, crc: Word): Word;
function HalfQuickCalCRC16(data, crc: Word): Word;

implementation

{$R *.DFM}

// 按位计算CRC,速度最慢,占用空间最少
function CalCRC16(data, crc, genpoly: Word): Word;
var i: Word;
begin
data := data shl 8; // 移到高字节
for i:=7 downto 0 do
begin
if ((data xor crc) and $8000) <> 0 then // 只测试最高位
crc := (crc shl 1) xor genpoly // 最高位为1,移位和异或处理
else crc := crc shl 1; // 否则只移位(乘2)
data := data shl 1; // 处理下一位
end;
Result := crc;
end;

// 按位计算CRC,结果与CalCRC16相同
function CalCRC16_2(data, crc, genpoly: Word): Word;
var i: Word;
begin
for i:=7 downto 0 do
begin
if (crc and $8000) <> 0 then
crc := (crc*2) xor genpoly // 余式CRC乘以2再求CRC
else crc := crc*2;
if (data and (1 shl i)) <> 0 then
crc := crc xor genpoly; // 再加上本位的CRC
end;
Result := crc;
end;

// 按字节计算CRC,通过查表(256项)快速计算
// 速度最快,占用空间最多(需要表格空间)
function QuickCalCRC16(data, crc: Word): Word;
begin
Result := CRCTab[(crc shr 8) xor data] xor (crc shl 8);
end;

// 按半字节计算CRC,通过查表(16项)快速计算
// 速度比较快,占用空间也比较少(表格空间只有16个字)
function HalfQuickCalCRC16(data, crc: Word): Word;
begin
crc := CRCTab[(crc shr 12) xor (data shr 4)] xor (crc shl 4);
Result := CRCTab[(crc shr 12) xor (data and $0F)] xor (crc shl 4);
end;

procedure TForm1.Button1Click(Sender: TObject);
var data, crc: Word;
begin
Memo1.Clear;
Memo1.Lines.Add('16bit CRC');
for data:=0 to 255 do
begin
crc := CalCRC16(data,0,GenPoly); // 按位计算CRC
Memo1.Text := Memo1.Text + IntToHex(crc, 4) + ' ';
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var data, crc: Word;
begin
Memo1.Clear;
Memo1.Lines.Add('16bit CRC_2');
for data:=0 to 255 do
begin
crc := CalCRC16_2(data,0,GenPoly); // 按位计算CRC
Memo1.Text := Memo1.Text + IntToHex(crc, 4) + ' ';
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var data, crc: Word;
begin
// 先生成CRC表(256项),用于快速查表计算
for data:=0 to 255 do CRCTab[data] := CalCRC16(data,0,GenPoly);
Memo1.Clear;
Memo1.Lines.Add('16bit Quick CRC');
for data:=0 to 255 do
begin
crc := QuickCalCRC16(data,0); // 按字节计算CRC
Memo1.Text := Memo1.Text + IntToHex(crc, 4) + ' ';
end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var data, crc: Word;
begin
// 先生成CRC表(16项),用于快速查表计算
for data:=0 to 15 do CRCTab[data] := CalCRC16(data,0,GenPoly);
Memo1.Clear;
Memo1.Lines.Add('16bit Half Quick CRC');
for data:=0 to 255 do
begin
crc := HalfQuickCalCRC16(data,0); // 按半字节计算CRC
Memo1.Text := Memo1.Text + IntToHex(crc, 4) + ' ';
end;
end;

end.
 
天使兄:辛苦啊
但好像还是有点问题啊,比如我多字节00H03H00H01H进行CRC校验,怎么办啊
而且生成的校验码还必须是8个字节的。。。。
你的程序中只能对8个字节进行校验啊
 
//CRC32校验函数,本单元经过了我的测试,你可以把它使用在你的程序中,加入到USES段中

UNIT CRC32;

INTERFACE

USES
Windows; // DWORD for D3/D4/D5/D6/D7 compatibility

TYPE
{$IFDEF VER130} // This is a bit awkward
// 8-byte integer
TInteger8 = Int64; // Delphi 5
{$ELSE}
{$IFDEF VER120}
TInteger8 = Int64; // Delphi 4
{$ELSE}
TInteger8 = COMP; // Delphi 2 or 3
{$ENDIF}
{$ENDIF}

PROCEDURE CalcCRC32 (p: pointer; ByteCount: DWORD; VAR CRCvalue: DWORD);
//标准CRC32函数
PROCEDURE CalcFileCRC32 (FromName: STRING; VAR CRCvalue: DWORD;
VAR TotalBytes: TInteger8;
VAR error: WORD);
//对于文件的CRC校验
IMPLEMENTATION

USES
{$IFDEF StreamIO}
SysUtils, // SysErrorMessage
{$ENDIF}
Dialogs, // ShowMessage
Classes; // TMemoryStream

CONST
table: ARRAY[0..255] OF DWORD =
($00000000, $77073096, $EE0E612C, $990951BA,
$076DC419, $706AF48F, $E963A535, $9E6495A3,
$0EDB8832, $79DCB8A4, $E0D5E91E, $97D2D988,
$09B64C2B, $7EB17CBD, $E7B82D07, $90BF1D91,
$1DB71064, $6AB020F2, $F3B97148, $84BE41DE,
$1ADAD47D, $6DDDE4EB, $F4D4B551, $83D385C7,
$136C9856, $646BA8C0, $FD62F97A, $8A65C9EC,
$14015C4F, $63066CD9, $FA0F3D63, $8D080DF5,
$3B6E20C8, $4C69105E, $D56041E4, $A2677172,
$3C03E4D1, $4B04D447, $D20D85FD, $A50AB56B,
$35B5A8FA, $42B2986C, $DBBBC9D6, $ACBCF940,
$32D86CE3, $45DF5C75, $DCD60DCF, $ABD13D59,
$26D930AC, $51DE003A, $C8D75180, $BFD06116,
$21B4F4B5, $56B3C423, $CFBA9599, $B8BDA50F,
$2802B89E, $5F058808, $C60CD9B2, $B10BE924,
$2F6F7C87, $58684C11, $C1611DAB, $B6662D3D,

$76DC4190, $01DB7106, $98D220BC, $EFD5102A,
$71B18589, $06B6B51F, $9FBFE4A5, $E8B8D433,
$7807C9A2, $0F00F934, $9609A88E, $E10E9818,
$7F6A0DBB, $086D3D2D, $91646C97, $E6635C01,
$6B6B51F4, $1C6C6162, $856530D8, $F262004E,
$6C0695ED, $1B01A57B, $8208F4C1, $F50FC457,
$65B0D9C6, $12B7E950, $8BBEB8EA, $FCB9887C,
$62DD1DDF, $15DA2D49, $8CD37CF3, $FBD44C65,
$4DB26158, $3AB551CE, $A3BC0074, $D4BB30E2,
$4ADFA541, $3DD895D7, $A4D1C46D, $D3D6F4FB,
$4369E96A, $346ED9FC, $AD678846, $DA60B8D0,
$44042D73, $33031DE5, $AA0A4C5F, $DD0D7CC9,
$5005713C, $270241AA, $BE0B1010, $C90C2086,
$5768B525, $206F85B3, $B966D409, $CE61E49F,
$5EDEF90E, $29D9C998, $B0D09822, $C7D7A8B4,
$59B33D17, $2EB40D81, $B7BD5C3B, $C0BA6CAD,

$EDB88320, $9ABFB3B6, $03B6E20C, $74B1D29A,
$EAD54739, $9DD277AF, $04DB2615, $73DC1683,
$E3630B12, $94643B84, $0D6D6A3E, $7A6A5AA8,
$E40ECF0B, $9309FF9D, $0A00AE27, $7D079EB1,
$F00F9344, $8708A3D2, $1E01F268, $6906C2FE,
$F762575D, $806567CB, $196C3671, $6E6B06E7,
$FED41B76, $89D32BE0, $10DA7A5A, $67DD4ACC,
$F9B9DF6F, $8EBEEFF9, $17B7BE43, $60B08ED5,
$D6D6A3E8, $A1D1937E, $38D8C2C4, $4FDFF252,
$D1BB67F1, $A6BC5767, $3FB506DD, $48B2364B,
$D80D2BDA, $AF0A1B4C, $36034AF6, $41047A60,
$DF60EFC3, $A867DF55, $316E8EEF, $4669BE79,
$CB61B38C, $BC66831A, $256FD2A0, $5268E236,
$CC0C7795, $BB0B4703, $220216B9, $5505262F,
$C5BA3BBE, $B2BD0B28, $2BB45A92, $5CB36A04,
$C2D7FFA7, $B5D0CF31, $2CD99E8B, $5BDEAE1D,

$9B64C2B0, $EC63F226, $756AA39C, $026D930A,
$9C0906A9, $EB0E363F, $72076785, $05005713,
$95BF4A82, $E2B87A14, $7BB12BAE, $0CB61B38,
$92D28E9B, $E5D5BE0D, $7CDCEFB7, $0BDBDF21,
$86D3D2D4, $F1D4E242, $68DDB3F8, $1FDA836E,
$81BE16CD, $F6B9265B, $6FB077E1, $18B74777,
$88085AE6, $FF0F6A70, $66063BCA, $11010B5C,
$8F659EFF, $F862AE69, $616BFFD3, $166CCF45,
$A00AE278, $D70DD2EE, $4E048354, $3903B3C2,
$A7672661, $D06016F7, $4969474D, $3E6E77DB,
$AED16A4A, $D9D65ADC, $40DF0B66, $37D83BF0,
$A9BCAE53, $DEBB9EC5, $47B2CF7F, $30B5FFE9,
$BDBDF21C, $CABAC28A, $53B39330, $24B4A3A6,
$BAD03605, $CDD70693, $54DE5729, $23D967BF,
$B3667A2E, $C4614AB8, $5D681B02, $2A6F2B94,
$B40BBE37, $C30C8EA1, $5A05DF1B, $2D02EF8D);

PROCEDURE CalcCRC32 (p: pointer; ByteCount: DWORD; VAR CRCValue: DWORD);
VAR
i: DWORD;
q: ^BYTE;
BEGIN
q := p;
FOR i := 0 TO ByteCount-1 DO BEGIN
CRCvalue := (CRCvalue SHR 8) XOR
Table[ q^ XOR (CRCvalue AND $000000FF) ];
INC(q)
END
END {CalcCRC32};


{$IFDEF StreamIO} // Contemporary method using TMemoryStream

//打开文件流时使用的是二进制读模式
PROCEDURE CalcFileCRC32 (FromName: STRING; VAR CRCvalue: DWORD;
VAR TotalBytes: TInteger8;
VAR error: WORD);
VAR
Stream: TMemoryStream;
BEGIN
error := 0;
CRCValue := $FFFFFFFF;
Stream := TMemoryStream.Create;
TRY
TRY
Stream.LoadFromFile(FromName);
IF Stream.Size > 0
THEN CalcCRC32 (Stream.Memory, Stream.Size, CRCvalue)
EXCEPT
ON E: EReadError DO
error := 1
END;

CRCvalue := NOT CRCvalue;
TotalBytes := Stream.Size
FINALLY
Stream.Free
END;
END {CalcFileCRC32};

{$ELSE}

PROCEDURE CalcFileCRC32 (FromName: STRING; VAR CRCvalue: DWORD;
VAR TotalBytes: TInteger8;
VAR error: WORD);
CONST
BufferSize = 32768;

TYPE
BufferIndex = 0..BufferSize-1;
TBuffer = ARRAY[BufferIndex] OF BYTE;
pBuffer = ^TBuffer;

VAR
BytesRead: INTEGER;
FromFile : FILE;
IOBuffer : pBuffer;
BEGIN
New(IOBuffer);
TRY
FileMode := 0; {Turbo default is 2 for R/W; 0 is for R/O}
CRCValue := $FFFFFFFF;
ASSIGN (FromFile,FromName);
{$I-} RESET (FromFile,1); {$I+}
error := IOResult;
IF error = 0
THEN BEGIN
TotalBytes := 0;

REPEAT
{$I-}
BlockRead (FromFile, IOBuffer^, BufferSize, BytesRead);
{$I+}
error := IOResult;
IF (error = 0) AND (BytesRead > 0)
THEN BEGIN
CalcCRC32 (IOBuffer, BytesRead, CRCvalue);
TotalBytes := TotalBytes + BytesRead;
END
UNTIL (BytesRead = 0) OR (error > 0);

CLOSE (FromFile)
END;
CRCvalue := NOT CRCvalue
FINALLY
Dispose(IOBuffer)
END
END {CalcFileCRC32};

{$ENDIF}

END {CRC32}.
 
天使兄这么细致耐心的回答,真是感动,散分了啊
天使兄:举个例子,比如modbus协议格式如下:
地址码(8字节)+功能码(8字节)+数据码(N X 8字节)+crc校验码(8字节)
crc校验的时候就是对数据码(N x 8字节进行校验的),不论n是多少,最后生成的crc校验码始终是8位的,比如,数据码也可能是03H,也可能是E5H78H09H02H,对数据码进行全部校验,最后得到的校验码都应该是8位的。。。

我想你的例子还没有完全解答我的问题,但是很谢谢你啊,我研究后大有启发
 
那么说明你其实并没有真正的懂的什么叫做CRC32校验,其实我给你程序,不管是一片文章也好,一个字符也好,校验出来生成的都是8位校验代码,例如要这样的程序,可以去我的论坛下载这样的例子程序,我已经写了一个简单的,你看看吧,试着输入一个字符或者一片文章,看看是不是都是生成8位校验码。
 
好好的
我桄然大捂
哈哈
多谢天使
 
后退
顶部