📄 mainrs232.pas
字号:
procedure TfrmMain.RTClick(Sender: TObject);
begin
MSComm1.RTSEnable:=not MSComm1.RTSEnable;
if MSComm1.RTSEnable then
lblrts.Caption:='False'
else
lblrts.Caption:='True';
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
// Button2Click(SELF);
{ if MSComm1.PortOpen then
MSComm1.PortOpen:=False;
MSComm1.CommPort:=StrToInt(edtCommport.text);
MSComm1.Settings:=edtCommsetting.text;
MSComm1.InputLen:=0;
MSComm1.InBufferCount:=0;
MSComm1.RThreshold:=1;
MSComm1.RTSEnable:=True;
MSComm1.PortOpen:=true;
if MSComm1.RTSEnable then
lblrts.Caption:='False'
else
lblrts.Caption:='True';}
sAddress:='$00';
end;
procedure TfrmMain.Button2Click(Sender: TObject);
begin
Comth:=COMTHread.Create(False);
comth.Data:=strtoint(edt.text);
Comth.Priority:=tpIdle;
Comth.Resume;
//Timer2.Enabled:=true;
if Timer2.Enabled then
lblTimer2.Caption:='Timer2开'
else
lblTimer2.caption:='Timer2关';
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
if Assigned(Comth)then
begin
Comth.Suspend;
Comth.Free;
end;
end;
procedure TfrmMain.Timer2Timer(Sender: TObject);
begin
btnSendClick(self);
end;
procedure TfrmMain.Button3Click(Sender: TObject);
begin
Timer2.Enabled:=not Timer2.Enabled;
if Timer2.Enabled then
lblTimer2.Caption:='Timer2开'
else
lblTimer2.caption:='Timer2关';
end;
procedure TfrmMain.Button4Click(Sender: TObject);
begin
memoSend.Clear;
end;
procedure TfrmMain.Button5Click(Sender: TObject);
var
i:integer;
begin
with table1 do
begin
Close;
Open;
for i:=1 to RecordCount do
begin
delete;
next;
end;
Close;
end;
ShowMessage('数据清空!!!');
end;
procedure TfrmMain.Button6Click(Sender: TObject);
begin
if MSComm1.PortOpen then
MSComm1.PortOpen:=False;
if Trim(cboCommport.text)='COM1' then
MSComm1.CommPort:=1
else if Trim(cboCommport.text)='COM2' then
MSComm1.CommPort:=2
else if Trim(cboCommport.text)='COM3' then
MSComm1.CommPort:=3
else if Trim(cboCommport.text)='COM4' then
MSComm1.CommPort:=4
else
MSComm1.CommPort:=1;
MSComm1.Settings:=cboCommsetting.text;
MSComm1.InputLen:=0;
MSComm1.InBufferCount:=0;
MSComm1.RThreshold:=1;
MSComm1.RTSEnable:=False;
MSComm1.PortOpen:=true;
//if strtoint(edit7.text)<256 then edit7.text:=inttostr((strtoint(edit7.text)+1))
//else edit7.text:='0';
MSComm1.Output:=edit7.text;
//msComm1.
// edit2.text:= MSComm1.Input;
end;
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if MSComm1.PortOpen then
MSComm1.PortOpen:=False;
end;
procedure TfrmMain.mnuStartClick(Sender: TObject);
begin
if (MSComm1.PortOpen=False) then
begin
try
begin
if Trim(cboCommport.text)='COM1' then
MSComm1.CommPort:=1
else if Trim(cboCommport.text)='COM2' then
MSComm1.CommPort:=2
else if Trim(cboCommport.text)='COM3' then
MSComm1.CommPort:=3
else if Trim(cboCommport.text)='COM4' then
MSComm1.CommPort:=4
else
MSComm1.CommPort:=1;
//MSComm1.CommPort:=StrToInt(cboCommport.text);
MSComm1.Settings:=cboCommsetting.text;
MSComm1.InputLen:=0;
MSComm1.InBufferSize:=1024;
MSComm1.OutBufferSize:=1024;
MSComm1.InputMode:=comInputModeText;
//MSComm1.InputMode:=comInputModeBinary;
MSComm1.InBufferCount:=0;
MSComm1.RThreshold:=1;
MSComm1.RTSEnable:=True;
MSComm1.PortOpen:=true;
if MSComm1.RTSEnable then
lblrts.Caption:='False'
else
lblrts.Caption:='True';
//MSComm1.PortOpen:=true;
end
except
Showmessage('无法打开!!!');
end
end;
mnuStart.Enabled:=not MSComm1.PortOpen;
mnuStop.Enabled:=MSComm1.PortOpen;
mnuSend.Enabled:=MSComm1.PortOpen;
mnuGoon.Enabled:=MSComm1.PortOpen;
mnuReset.Enabled:=MSComm1.PortOpen;
mnuLink.Enabled:=MSComm1.PortOpen;
end;
procedure TfrmMain.mnuStopClick(Sender: TObject);
begin
Timer3.Enabled:=false;
mnuStart.Enabled:=MSComm1.PortOpen;
mnuStop.Enabled:=not MSComm1.PortOpen;
if MSComm1.PortOpen then
MSComm1.PortOpen:=False;
mnuSend.Enabled:=not MSComm1.PortOpen;
mnuGoon.Enabled:=not MSComm1.PortOpen;
mnuReset.Enabled:=not MSComm1.PortOpen;
mnuLink.Enabled:=not MSComm1.PortOpen;
end;
procedure TfrmMain.N1Click(Sender: TObject);
begin
close;
end;
procedure TfrmMain.mnuResetClick(Sender: TObject);
var
sSUM:Byte;
begin
iSendCount:=0;
sSendbuf[0]:=Trim(cboAddress.text);//'0x01';
sAddress:=sSendbuf[0];
sSendbuf[1]:='$A3';
sSendbuf[2]:='$02';
sSUM:=StrToInt(sSendbuf[0])+StrToInt(sSendbuf[1])+StrToInt(sSendbuf[2]);
sSendbuf[3]:='$'+IntToHex(sSUM,2);
sSendBuf[4]:='$5A';
if memoSend.Font.Color=clRed then
memoSend.Font.Color:=clBlue
else
memoSend.Font.Color:=clRed;
sSendbuf[0]:=Trim(cboAddress.Text);
for i:=5 to 12 do
sSendbuf[i]:='$00';
memoSend.Lines.Add(sSendBuf[0]+' ,'+sSendBuf[1]+' ,'+sSendBuf[2]+' ,'+sSendBuf[3]+' ,'+sSendBuf[4]);//
// +' ,'+sSendBuf[5]+' ,'+sSendBuf[6]+' ,'+sSendBuf[7]+' ,'+sSendBuf[8]+' ,'+sSendBuf[9]
// +' ,'+sSendBuf[10]+' ,'+sSendBuf[11]+' , '+sSendBuf[12]);
if memoSend.Font.Color=clRed then
memoSend.Font.Color:=clBlue
else
memoSend.Font.Color:=clRed;
try
MSComm1.Output:=sSendbuf[0];//+sSendbuf[2]+sSendbuf[3]+sSendbuf[4];
except
;
end;
Timer3.Enabled:=true;
end;
function TfrmMain.DecToHex(var data: byte): string;
var
da:integer;
sHex:string;
begin
sHex:=Chr(data);
Result:=sHex;
end;
procedure TfrmMain.mnuLinkClick(Sender: TObject);
var
sSUM:byte;
begin
iSendCount:=0;
sSendbuf[0]:=Trim(cboAddress.text);//'0x01';
sAddress:=sSendbuf[0];
sSendbuf[1]:='$A1';
sSendbuf[2]:='$02';
sSUM:=StrToInt(sSendbuf[0])+StrToInt(sSendbuf[1])+StrToInt(sSendbuf[2]);
sSendbuf[3]:='$'+IntToHex(sSUM,2);
sSendBuf[4]:='$5A';
for i:=5 to 12 do
sSendbuf[i]:='$00';
sSendbuf[0]:=Trim(cboAddress.Text);
memoSend.Lines.Add(sSendBuf[0]+' ,'+sSendBuf[1]+' ,'+sSendBuf[2]+' ,'+sSendBuf[3]+' ,'+sSendBuf[4]);//
// +' ,'+sSendBuf[5]+' ,'+sSendBuf[6]+' ,'+sSendBuf[7]+' ,'+sSendBuf[8]+' ,'+sSendBuf[9]
// +' ,'+sSendBuf[10]+' ,'+sSendBuf[11]+' , '+sSendBuf[12]);
if memoSend.Font.Color=clRed then
memoSend.Font.Color:=clBlue
else
memoSend.Font.Color:=clRed;
try
MSComm1.Output:=sSendbuf[0];
except
;
end;
Timer3.Enabled:=true;
end;
procedure TfrmMain.mnuSendClick(Sender: TObject);
var
ss:string;
i:integer;
crc:byte;
begin
iSendCount:=0;
SS:=cboSend.Text;
for i:=0 to 12 do
sSendBuf[i]:=Copy(SS,i*4+1,3);
if (tmrSend.Enabled) then
begin
if ((StrToInt(sAddress)+1)=$10) then sAddress:='$00';
sAddress:='$'+IntToHex(StrToInt(sAddress)+1,2)
end
else
sAddress:=Trim(cboAddress.Text);
sSendbuf[0]:=Trim(sAddress);//cboAddress.Text);
crc:=StrToInt(sSendbuf[0])+StrToInt(sSendbuf[1])+StrToInt(sSendbuf[2])
+StrToInt(sSendbuf[3])+StrToInt(sSendbuf[4])+StrToInt(sSendbuf[5])
+StrToInt(sSendbuf[6])+StrToInt(sSendbuf[7])+StrToInt(sSendbuf[8])
+StrToInt(sSendbuf[9])+StrToInt(sSendbuf[10]);
sSendBuf[11]:='$'+IntToHex(crc,2);
if (memoSend.Lines.Count>100)then
begin
memoSend.Lines.Clear;
memoSend.Lines.Add('地址 命令 Cunt Dat1 Dat2 Dat3 Dat4 Dat5 Dat6 Dat7 Dat8 校验和 结束符');
end;
memoSend.Lines.Add(sSendBuf[0]+' ,'+sSendBuf[1]+' ,'+sSendBuf[2]+' ,'+sSendBuf[3]+' ,'+sSendBuf[4]
+' ,'+sSendBuf[5]+' ,'+sSendBuf[6]+' ,'+sSendBuf[7]+' ,'+sSendBuf[8]+' ,'+sSendBuf[9]
+' ,'+sSendBuf[10]+' ,'+sSendBuf[11]+' ,'+sSendBuf[12]);
if memoSend.Font.Color=clRed then
memoSend.Font.Color:=clBlue
else
memoSend.Font.Color:=clRed;
try
MSComm1.Output:=sSendbuf[0];//+sSendbuf[1]+sSendbuf[2]+sSendbuf[3]+sSendbuf[4]
//+sSendbuf[5]+sSendbuf[6]+sSendbuf[7]+sSendbuf[8]+sSendbuf[9]+sSendbuf[10]+sSendbuf[11]+sSendbuf[12];
except
;
end;
Timer3.Enabled:=true;
end;
procedure TfrmMain.btnSendCmdClick(Sender: TObject);
var
crc:byte;
begin
crc:=StrToInt(edtAddr.Text)+StrToInt(edtCmd.Text)+StrToInt(edtCount.Text)
+StrToInt(edtData1.Text)+StrToInt(edtData2.Text)+StrToInt(edtData3.Text)
+StrToInt(edtData4.Text)+StrToInt(edtData5.Text)+StrToInt(edtData6.Text)
+StrToInt(edtData7.Text)+StrToInt(edtData8.Text);
//crc:='$'+IntToHex(crc,2);
sSendBuf[0]:=Trim(edtAddr.Text);
sSendBuf[1]:=Trim(edtCmd.Text);
sSendBuf[2]:=Trim(edtCount.Text);
sSendBuf[3]:=Trim(edtData1.Text);
sSendBuf[4]:=Trim(edtData2.Text);
sSendBuf[5]:=Trim(edtData3.Text);
sSendBuf[6]:=Trim(edtData4.Text);
sSendBuf[7]:=Trim(edtData5.Text);
sSendBuf[8]:=Trim(edtData6.Text);
sSendBuf[9]:=Trim(edtData7.Text);
sSendBuf[10]:=Trim(edtData8.Text);
sSendBuf[11]:='$'+IntToHex(crc,2);
sSendBuf[12]:=Trim(edtEnd.Text);
edtSUM.text:= sSendBuf[11];
if memoSend.Font.Color=clRed then
memoSend.Font.Color:=clBlue
else
memoSend.Font.Color:=clRed;
memoSend.Lines.Add(sSendBuf[0]+' ,'+sSendBuf[1]+' ,'+sSendBuf[2]+' ,'+sSendBuf[3]+' ,'+sSendBuf[4]
+' ,'+sSendBuf[5]+' ,'+sSendBuf[6]+' ,'+sSendBuf[7]+' ,'+sSendBuf[8]+' ,'+sSendBuf[9]
+' ,'+sSendBuf[10]+' ,'+sSendBuf[11]+' , '+sSendBuf[12]);
iSendCount:=0;
try
MSComm1.Output:=sSendbuf[0];
except
;
end;
end;
procedure TfrmMain.mnuGoOnClick(Sender: TObject);
begin
tmrSend.Enabled:=not tmrSend.Enabled;
if (mnuGoOn.Bitmap.Canvas.Pen.Color=clred) then
mnuGoOn.Bitmap.Canvas.Pen.Color:=clred
else
mnuGoOn.Bitmap.Canvas.Pen.Color:=clgray;
end;
procedure TfrmMain.tmrSendTimer(Sender: TObject);
begin
mnuSendClick(self);
end;
procedure TfrmMain.cboAddressChange(Sender: TObject);
begin
sAddress:=cboAddress.text;
end;
procedure TfrmMain.Timer3Timer(Sender: TObject);
var
b:boolean;
begin
b:=timer3.Enabled;
iSendCount:=iSendCount+1;
if (iSendCount<=(StrToInt(sSendBuf[2])+2)) then
begin
MSComm1.Output:=sSendbuf[iSendCount];
timer3.Enabled:=b;
end
else
Timer3.Enabled:=false;
//memosend.Lines.Add(sSendbuf[iSendCount]);
end;
procedure TfrmMain.Button7Click(Sender: TObject);
begin
try
timer3.Interval:=StrToInt(edit8.text);
except
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -