📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, TabNotBk, ExtCtrls, SPComm;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
SPREALCOM: TComboBox;
Button1: TButton;
Button2: TButton;
Timer1: TTimer;
Timer2: TTimer;
TabControl1: TTabControl;
Edit5: TEdit;
Label10: TLabel;
Label8: TLabel;
Edit4: TEdit;
Label9: TLabel;
Edit3: TEdit;
Edit2: TEdit;
Edit1: TEdit;
Button4: TButton;
Label6: TLabel;
Label11: TLabel;
Label12: TLabel;
Comm1: TComm;
Label5: TLabel;
Edit6: TEdit;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
sendfalse: integer;
snedneixing:integer;
receivefalse:integer;
redatafalse:boolean;
receivedata:array[0..130] of Byte;
senddatabuff:array[0..150] of Byte;
daomacont:Byte; //前导码
zichaoiid:Byte; //制造ID号
sebeiid:array[0..5]of Byte; //是设备的ID号
sebeinx:Byte; //设备类型
bingmu:boolean;
implementation
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
var
i, j,sdlength: integer;
kkm:Byte;
sendstring: string; //sdlength: integer;
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$80;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
sendfalse:=1;
timer1.Enabled:=true;
redatafalse:=false;
snedneixing:=1;
//i:= 0+kkm;
//.Caption:=inttostr(i);
// f:=@b[0];
//foatstrzm:= floattostr(f^);
//showmessage(floattostr(f^)) ;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if bingmu then
begin
bingmu:=false;
Button1.Caption:='打开串';
self.Comm1.StopComm;
Button4.Enabled:=false;
end
else
begin
self.Comm1.StopComm;
self.Comm1.CommName := self.SPREALCOM.Text;
self.Comm1.Outx_XonXoffFlow:=false;
self.Comm1.Inx_XonXoffFlow:=false ;
self.Comm1.StartComm;
bingmu:=true;
Button1.Caption:='关闭串口';
Button4.Enabled:=true;
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
type
IArr = ^integer;
var
revp : array[0..1024]of byte;
i,kkm:integer;
p:PByte;
commRevstr: shortstring;
begin
//sleep(300);
commRevstr:='';
kkm:= BufferLength;
Move(IArr(buffer)^,Revp,kkm);
p:= pByte(@ receivedata[0]);
For i:=0 to kkm-1 do
begin
CommrevStr:=CommrevStr +Char(Revp[i]);
p^:= Revp[i];
Inc(p);
end;
Timer2.Enabled:=true;
// Memo1.Lines.Text:= CommrevStr;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
//如果4秒钟已后还没有数据上来发后面指令
var
i, j,sdlength: integer;
kkm:Byte;
sendstring: string;
begin
if sendfalse=1 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$81;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=2;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
timer1.Enabled:=true;
snedneixing:=1;
end
else if sendfalse =2 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$82;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=3;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
timer1.Enabled:=true;
snedneixing:=1;
end
else if sendfalse =3 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$83;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=4;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
timer1.Enabled:=true;
snedneixing:=1;
end
else if sendfalse =4 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$84;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=5;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
timer1.Enabled:=true;
snedneixing:=1;
end
else if sendfalse =5 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$85;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=6;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
timer1.Enabled:=true;
snedneixing:=1;
end
else if sendfalse =6 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$86;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=7;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
timer1.Enabled:=true;
snedneixing:=1;
end
else if sendfalse =7 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$87;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=8;
kkm:=senddatabuff[1];
snedneixing:=1;
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
senddatabuff[9]:=kkm;
senddatabuff[10]:=$40;
sendstring:='';
for i:=0 to 10 do
begin
sendstring:= sendstring + chr(senddatabuff[i])
end;
sdlength:= length(sendstring);
comm1.WriteCommData(pchar(sendstring),sdlength );
timer1.Enabled:=true;
end
else if sendfalse =8 then
begin
senddatabuff[0]:=$23;
senddatabuff[1]:=$FF;
senddatabuff[2]:=$FF;
senddatabuff[3]:=$FF;
senddatabuff[4]:=$FF;
senddatabuff[5]:=$02;
senddatabuff[6]:=$88;
senddatabuff[7]:=$00;
senddatabuff[8]:=$00;
sendfalse:=9;
snedneixing:=1;
kkm:=senddatabuff[1];
for j:=2 to 8 do
begin
kkm:= kkm xor senddatabuff[j];
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -