⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~pas

📁 一个串口通信的软件
💻 ~PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SPComm, ExtCtrls, Spin,TypInfo, Buttons;

type
  TForm1 = class(TForm)
    Comm1: TComm;
    Button1: TButton;
    memo1: TMemo;
    Button2: TButton;
    Label1: TLabel;
    Button3: TButton;
    Button4: TButton;
    Timer1: TTimer;
    Memo2: TMemo;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Button5: TButton;
    Button6: TButton;
    Label2: TLabel;
    Label3: TLabel;
    SpinEdit1: TSpinEdit;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    CheckBox1: TCheckBox;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    Label10: TLabel;
    Label11: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    CheckBox2: TCheckBox;
    Label12: TLabel;
    CheckBox3: TCheckBox;
    BitBtn5: TBitBtn;
    Save: TSaveDialog;
    procedure Button1Click(Sender: TObject);
    procedure senddata;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    //procedure receivedata;
    procedure Button2Click(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
    procedure Button5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);

    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
  private


    { Private declarations }
  public

    { Public declarations }
  end;

var
  Form1: TForm1;
  rbuf,sbuf:array[1..16]of byte;
  vstring,oldvstring:string;
  kg:Boolean;
  sbufcont:integer;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  tempstr:tstringlist;
  i:integer;

begin
 {if kg =false then
   begin
     comm1.StartComm ;
     kg:= true;
   end; }
 //Button3Click(Sender);
 vstring:='';

 tempstr:=Tstringlist.Create ;
 try
  tempstr.Delimiter:=#32;//---#32表示为空格;
  tempstr.DelimitedText:=memo2.text;
  sbufcont:=tempstr.count -1;
  for i:= 0 to tempstr.Count -1 do
  sbuf[i]:=strtoint('$'+tempstr.strings[i]);//---将字符转换成BYTE整数;
 // sbuf[2]:=strtoint('$'+tempstr.strings[1]);//---将字符转换成BYTE整数;
  senddata;
 finally
  freeandnil(tempstr);
 end;


end;

procedure Tform1.senddata;

var

i:integer;
commflg : Boolean;

begin

commflg:=true ;

for i:=0 to sbufcont do

begin

if not comm1.writecommdata(@sbuf[i],1) then
    //comm1.writecommdata(@sbuf[i],1);
begin

Commflg:=false;

break;

end;

  sleep(2);
  vstring:=vstring + inttohex(sbuf[i],2)+' ';
end;
 Commflg:=false;
 //showmessage(vstring);
end;



procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  {if button2.Caption ='停止' then
  begin
    showmessage('请先停止');
    button2.SetFocus ;
    exit;
  end; }
  comm1.StopComm ;
  kg:=false;
  
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  form1.Width  :=759;
  form1.Height :=550;
  form1.Left :=193;
  form1.Top :=160;
  memo1.Text :='';
  memo2.Text :='';
  label1.Caption :='';
  combobox3.Text :='';
  combobox4.Text :='';
// comm1.StartComm ;
 kg:=false;
end;

{procedure TForm1.receivedata;
var
  strrec:string;
  buffer:pointer;
  bufferlength:word;
begin
   SetLength(strrec,bufferlength);
   Move(buffer^,Pchar(strrec)^,bufferlength);
   memo1.Lines.Add(strrec);
   memo1.Invalidate ;
end; }

procedure TForm1.Button2Click(Sender: TObject);

begin
  timer1.Interval :=spinedit1.Value ;
  if button2.Caption ='连续' then
    begin
      button2.Caption :='停止';
      memo1.Text :='';
      button1.Enabled :=false;
      memo2.Enabled :=false;
      spinedit1.Enabled :=false;
      timer1.Enabled :=true;
    end
  else
    begin
      button2.Caption :='连续';
      button1.Enabled :=true;
      memo2.Enabled :=true;
      spinedit1.Enabled :=true;
      timer1.Enabled :=false;
    end;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var
i:integer;
data,data1,data2,data3:string;
int1:integer;
int2:real;
begin
vstring:='' ;
//sleep(200);
move(buffer^,pchar(@rbuf)^,bufferlength);
//NEW(BUFFER);
 for i:=1 to bufferlength do
  begin
    vstring:=vstring+ inttohex(rbuf[i],2)+' ' ;
  end;
    {if rbuf[1]*256 > 9999 then
     begin
     timer1.Enabled :=false;
     data1:= inttostr(rbuf[1]);
     showmessage(data1);
     end; }

    int1:=rbuf[1]*256+rbuf[2];
  {  data:=inttostr(int1);
    if length(data)=2 then
    data:='0.'+data ;
    if length(data)=1 then
    data:='0.0'+data ;
    if length(data)>2 then
      begin
        int2:=int1/100;
        data:=floattostr(int2);
      end; }


    if checkbox3.Checked =true then
      begin
       data2:=inttohex(rbuf[3],2)+inttohex(rbuf[4],2);
       if data2='0101' then int2:=int1/100;
       if data2='0001' then int2:=180 - int1/100 ;
       if data2='0000' then int2:=int1/100 + 180 ;
       if data2='0100' then int2:=360 - int1/100 ;
      end
    else
      int2:=int1/100;

     data:=floattostr(int2);
     data1:=inttostr(int1);
     if length(data1)>4 then
     begin
     timer1.Enabled :=false;
     showmessage(data1);
     end;
   // label1.Caption :=data;

   // showmessage(data);
   if  checkbox2.Checked = false then
      if checkbox1.Checked = false then
         begin
          // if memo1.Lines.Count >27 then memo1.Clear;
          memo1.lines[memo1.lines.count-1]:=memo1.lines[memo1.lines.count-1]+vstring;

           //memo1.Text :=memo1.Text +vstring;

         end
      else
        begin
          //if memo1.Lines.Count >27 then memo1.Text :='';
          memo1.lines[memo1.lines.count-1]:=memo1.lines[memo1.lines.count-1]+vstring+' '+data+' ';

        end
   else
       if checkbox1.Checked =true then
         begin
          // if memo1.Lines.Count >26 then memo1.Text :='';
           memo1.lines.add(vstring+' '+data);

         end
       else
         begin
          // if memo1.Lines.Count >26 then memo1.Text :='';
           memo1.lines.add(vstring);

         end;
   // memo1.lines.add(' ');

   // comm1.SendDataEmpty :=true;


 end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if kg =false then
   begin
     comm1.StartComm ;
     kg:= true;
   end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  timer1.Enabled :=false;
  comm1.StopComm ;
  kg:=false;
end;

procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
  showmessage('cuo');
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
      combobox1.Enabled :=false;
      combobox2.Enabled :=false;
      combobox3.Enabled :=false;
      combobox4.Enabled :=false;
      button1.Enabled :=true;
      button2.Enabled :=true;
      button5.Enabled :=false;
      comm1.BaudRate :=strtoint(combobox2.Text);
      comm1.CommName :=combobox1.Text ;
      comm1.ByteSize :=Tbytesize(GetEnumValue(TypeInfo(Tbytesize), combobox3.Text));
      comm1.StopBits :=TStopBits(GetEnumValue(TypeInfo(TStopBits), combobox4.Text));
      comm1.StartComm ;
      kg:=true;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  combobox1.ItemIndex := 0;
  combobox2.ItemIndex := 2;
  combobox3.ItemIndex := 3;
  combobox4.ItemIndex := 0;
  combobox1.Enabled :=true;
  combobox2.Enabled :=true;
  combobox3.Enabled :=true;
  combobox4.Enabled :=true;
  BitBtn3.Enabled :=false;
  BitBtn4.Enabled :=false;
  combobox1.SetFocus ;
  spinedit1.Value :=1000;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
 { comm1.StopComm ;
  kg:=false;
  combobox1.Enabled :=true;
  combobox2.Enabled :=true;
  combobox3.Enabled :=true;
  combobox4.Enabled :=true;
  button1.Enabled :=false;
  button2.Enabled :=false;
  button5.Enabled :=true;}
  memo2.Text :='68 01';
  spinedit1.Value :=200;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
      combobox1.Enabled :=false;
      combobox2.Enabled :=false;
      combobox3.Enabled :=false;
      combobox4.Enabled :=false;
      BitBtn4.Enabled :=true;
      BitBtn3.Enabled :=true;
      BitBtn1.Enabled :=false;
      comm1.BaudRate :=strtoint(combobox2.Text);
      comm1.CommName :=combobox1.Text ;
      comm1.ByteSize :=Tbytesize(GetEnumValue(TypeInfo(Tbytesize), combobox3.Text));
      comm1.StopBits :=TStopBits(GetEnumValue(TypeInfo(TStopBits), combobox4.Text));
      comm1.StartComm ;
      kg:=true;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
  comm1.StopComm ;
  kg:=false;
  combobox1.Enabled :=true;
  combobox2.Enabled :=true;
  combobox3.Enabled :=true;
  combobox4.Enabled :=true;
  BitBtn4.Enabled :=false;
  BitBtn3.Enabled :=false;
  BitBtn1.Enabled :=true;
end;



procedure TForm1.BitBtn4Click(Sender: TObject);
begin
  timer1.Interval :=spinedit1.Value ;
  if BitBtn4.Caption ='连续' then
    begin
      BitBtn4.Caption :='停止';
      memo1.Text :='';
      BitBtn3.Enabled :=false;
      memo2.Enabled :=false;
      spinedit1.Enabled :=false;
      timer1.Enabled :=true;
    end
  else
    begin
      BitBtn4.Caption :='连续';
      BitBtn3.Enabled :=true;
      memo2.Enabled :=true;
      spinedit1.Enabled :=true;
      timer1.Enabled :=false;
    end;
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
var
  tempstr:tstringlist;
  i:integer;

begin
 {if kg =false then
   begin
     comm1.StartComm ;
     kg:= true;
   end; }
 //Button3Click(Sender);
 vstring:='';

 tempstr:=Tstringlist.Create ;
 try
  tempstr.Delimiter:=#32;//---#32表示为空格;
  tempstr.DelimitedText:=memo2.text;
  sbufcont:=tempstr.count -1;
  for i:= 0 to tempstr.Count -1 do
  sbuf[i]:=strtoint('$'+tempstr.strings[i]);//---将字符转换成BYTE整数;
 // sbuf[2]:=strtoint('$'+tempstr.strings[1]);//---将字符转换成BYTE整数;
  senddata;
 finally
  freeandnil(tempstr);


end;

end;


procedure TForm1.CheckBox1Click(Sender: TObject);
begin
//if checkbox1.Checked =true then checkbox2.Checked :=false;
   if checkbox1.Checked =true then
     begin
       checkbox3.Visible :=true ;
       checkbox3.Checked :=false ;
     end
   else
     checkbox3.visible:=false;

end;

procedure TForm1.CheckBox2Click(Sender: TObject);
begin
//if checkbox2.Checked =true then checkbox1.Checked :=false;
end;

procedure TForm1.BitBtn5Click(Sender: TObject);
var
   filename:Tfilename;
begin

   if save.Execute then
   begin
   filename:=save.FileName;
   memo1.Lines.savetofile(filename+'.txt');
   end;
end;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -