Unit_ModBus_CRC.pas
上传用户:yongjie826
上传日期:2022-07-22
资源大小:164k
文件大小:1k
- unit Unit_ModBus_CRC;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, strUtils;
- type
- TForm_ModBus_CRC = class(TForm)
- Button1: TButton;
- Edit1: TEdit;
- Edit2: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- procedure Edit2Change(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form_ModBus_CRC: TForm_ModBus_CRC;
- dataArray : Array[0..6] of byte = ($01,$03,$04,$00,$00,$00,$00);//(01,03,00,00,00,02);//
- implementation
- {$R *.dfm}
- function crc(str:String):dword;
- var
- i,j,
- crcInt : dword;
- tmp : byte;
- begin
- crcInt := $FFFF;
- str := strUtils.AnsiReplaceStr(str,' ','');
- for i:=0 to length(str) div 2 - 1 do
- begin
- tmp := strtoint('$'+copy(str,i*2+1,2));
- crcInt := (crcInt and $FF00) + ((crcInt and $FF) xor tmp) and $FF;
- for j:=1 to 8 do
- begin
- if (crcInt and 1) = 0 then
- crcInt := (crcInt shr 1)
- else
- crcInt := (crcInt shr 1) xor $A001;
- end;
- end;
- result := crcInt;
- end;
- procedure TForm_ModBus_CRC.Edit2Change(Sender: TObject);
- var
- crcR : dword;
- begin
- crcR := crc(Edit2.text);
- Edit1.Text := rightstr('0'+inttohex(crcR and $00FF,0),2)+' '+ rightstr('0'+inttohex(crcR shr 8,0),2);
- end;
- procedure TForm_ModBus_CRC.FormCreate(Sender: TObject);
- begin
- Edit2Change(nil);
- end;
- end.