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

📄 mainform.~pas

📁 串口接收
💻 ~PAS
字号:
unit mainform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CPort, StdCtrls, CPortCtl, bsSkinCtrls, bsSkinBoxCtrls, Mask;
type
userdata=array[1..6] of byte;

type
  TForm1 = class(TForm)
    ComPort1: TComPort;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    ComComboBox1: TComComboBox;
    ComComboBox2: TComComboBox;
    Button1: TButton;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    GroupBox3: TGroupBox;
    Button2: TButton;
    Button3: TButton;
    GroupBox4: TGroupBox;
    Memo1: TMemo;
    GroupBox5: TGroupBox;
    CheckBox1: TCheckBox;
    Button4: TButton;
    bsSkinEdit1: TbsSkinEdit;
    bsSkinPasswordEdit1: TbsSkinPasswordEdit;
    Label3: TLabel;
    Label6: TLabel;
    ComLed1: TComLed;
    procedure Button1Click(Sender: TObject);
    procedure ComPort1RxChar(Sender: TObject; Count: Integer);
    procedure Button4Click(Sender: TObject);
    procedure ComComboBox1Change(Sender: TObject);
    procedure ComComboBox2Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure bsSkinEdit1Exit(Sender: TObject);
    procedure bsSkinPasswordEdit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure ComLed1Change(Sender: TObject; AState: TLedState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  userid:byte;
  iderror:bool;
  strhex:string;
  memo1_flag:bool;
  ch_count:integer;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
if comport1.Connected =true  then
begin
  comport1.Connected:=false;
  button1.Caption:='打开串口';
end
else
begin
  comport1.Connected:=true;
  button1.Caption:='关闭串口';
end;
end;

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
str,strtmp:string;
ch:byte;
i:integer;

begin
  comport1.ReadStr(str,count);
  if  checkbox1.Checked=false then
    begin
    memo1.Text:=memo1.Text+str;
    end // 16 show check
  else
    begin
    for i:=0 to count-1 do
      begin
      ch:=byte((pchar(str)+i)^);
      strtmp:=strtmp+inttohex(ch,2)+' ';
      end; //for i
      memo1.Text:=memo1.Text+strtmp;
      strtmp:='';
    end;
end;//procedure end;

procedure TForm1.Button4Click(Sender: TObject);
begin
memo1.Clear;
end;

procedure TForm1.ComComboBox1Change(Sender: TObject);
begin
comport1.BaudRate:=comcombobox1.ComPort.BaudRate;
comcombobox1.ApplySettings;
end;

procedure TForm1.ComComboBox2Change(Sender: TObject);
begin
comport1.Port:=comcombobox2.ComPort.Port;
comcombobox2.ApplySettings;
end;

procedure TForm1.Button2Click(Sender: TObject);
var

td:userdata;
begin

  td[1]:=$a5;
  td[2]:=$5a;
  td[3]:=$aa;
  td[4]:=$55;
  td[5]:=$a0;
  td[6]:=userid;
  comport1.Write(td,6);
end;


procedure TForm1.Button3Click(Sender: TObject);
var

td:userdata;
begin

  td[1]:=$a5;
  td[2]:=$5a;
  td[3]:=$aa;
  td[4]:=$55;
  td[5]:=$50;
  td[6]:=userid;
  comport1.Write(td,6);
end;


procedure TForm1.bsSkinEdit1Exit(Sender: TObject);
var

pa:pchar;
i,j,k:byte;
m1:integer;

begin

  getmem(pa,4);
  bsskinedit1.GetTextBuf(pa,4);
  if bsskinedit1.GetTextLen=3 then
  begin
  i:=byte(pa^);
  j:=byte((pa+1)^);
  k:=byte((pa+2)^);
  m1:=(i-48)*100+(j-48)*10+(k-48);
  end;
  if bsskinedit1.GetTextLen=2 then
  begin
  i:=byte(pa^);
  j:=byte((pa+1)^);
  k:=byte((pa+2)^);
  m1:=(i-48)*10+(j-48)
  end;
    if bsskinedit1.GetTextLen=1 then
  begin
  i:=byte(pa^);
  j:=byte((pa+1)^);
  k:=byte((pa+2)^);
  m1:=(i-48);
  end;
  if m1>255 then
  begin
   MessageDlg('亮度值应为:0~255之间的3位数',mtInformation,[mbOK],0);
   iderror:=true;
  end
  else
  begin
  userid:=m1;
  iderror:=false;
  end;
//tmpnum:=bsskinedit1.GetTextLen;
//form1.Caption:=inttostr(tmpnum);

end;


procedure TForm1.bsSkinPasswordEdit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

  if char(key)=char(13) then
  begin
    if iderror=true then
     MessageDlg('亮度值应为:0~255之间的3位数',mtInformation,[mbOK],0)
    else
    begin //iderror is false,that say the user's id is below 256
      if bsskinpasswordedit1.Text='sanshi'then
      begin
      button2.Enabled:=true;
       button3.Enabled:=true;
        //showmessage('     '+bsskinedit1.text+' 欢迎使用'+'         ');
        MessageDlg(' 欢迎使用'+'         ',mtInformation,[mbOK],0);

      end //password true
      else
      begin // password untrue
        //showmessage('            密码错误               ');
         MessageDlg(' 密码错误 ',mtInformation,[mbOK],0);

        bsskinpasswordedit1.Clear;
      end; //password false
    end;  //iderror=false
    bsskinpasswordedit1.Clear;
  end;    //CR press
end; //procedure end

procedure TForm1.FormCreate(Sender: TObject);
begin
iderror:=true;
end;

procedure TForm1.ComLed1Change(Sender: TObject; AState: TLedState);
begin
if comled1.State=TLedState(0) then
begin
label6.Caption:='已关闭  ';
end
else
label6.Caption:='已打开  ';
end;

end.

⌨️ 快捷键说明

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