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

📄 main.~pas

📁 delphi写的com口通信程序
💻 ~PAS
字号:
unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Buttons, _GClass, AbLED, AbDBCtrls, SPComm;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    ComboBox5: TComboBox;
    Memo1: TMemo;
    SpeedButton1: TSpeedButton;
    AbDBLED1: TAbDBLED;
    Memo2: TMemo;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    Label9: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Label10: TLabel;
    CheckBox3: TCheckBox;
    Label11: TLabel;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    Edit1: TEdit;
    Label12: TLabel;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    CheckBox4: TCheckBox;
    Comm1: TComm;
    Timer1: TTimer;
    SpeedButton9: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure ComboBox3Change(Sender: TObject);
    procedure ComboBox4Change(Sender: TObject);
    procedure ComboBox5Change(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Commswitch: Boolean;

implementation

{$R *.DFM}

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if CommSwitch = False then
  begin
    comm1.StartComm;
    Commswitch := True;
    SpeedButton1.Caption := '关闭串口';
    StaticText1.Caption := '打开';
    AbDBLED1.checked := True;
  end else
  begin
    comm1.StopComm;
    Commswitch := False;
    SpeedButton1.Caption := '打开串口';
    StaticText1.Caption := '关闭';
    AbDBLED1.checked := False;
  end;
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
  Form1.close;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
  StaticText2.Caption := IntToStr(0);
  StaticText3.Caption := IntToStr(0);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  Comm1.CommName := ComboBox1.Items.Strings[ComboBox1.ItemIndex];
  if CommSwitch = True then
  begin
    Comm1.StopComm;
    Comm1.StartComm;
  end;
end;

procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
  Memo1.Clear;
end;

procedure TForm1.SpeedButton7Click(Sender: TObject);
begin
  Memo2.Clear;
end;

procedure TForm1.ComboBox2Change(Sender: TObject);
begin
  Comm1.BaudRate := StrToInt(ComboBox2.Items.Strings[ComboBox2.ItemIndex]);
  if CommSwitch = True then
  begin
    comm1.StopComm;
    Comm1.StartComm;
  end;
end;

procedure TForm1.ComboBox3Change(Sender: TObject);
begin
  case ComboBox3.ItemIndex of
    0: Comm1.ByteSize := _5;
    1: Comm1.ByteSize := _6;
    2: Comm1.ByteSize := _7;
    3: Comm1.ByteSize := _8;
  end;
  if CommSwitch = True then
  begin
    comm1.StopComm;
    Comm1.StartComm;
  end;
end;

procedure TForm1.ComboBox4Change(Sender: TObject);
begin
  case ComboBox4.ItemIndex of
    0: Comm1.Parity := EVEN;
    1: Comm1.Parity := MARK;
    2: Comm1.Parity := NONE;
    3: Comm1.Parity := ODD;
    4: Comm1.Parity := SPACE;
  end;
  if CommSwitch = True then
  begin
    comm1.StopComm;
    Comm1.StartComm;
  end;
end;

procedure TForm1.ComboBox5Change(Sender: TObject);
begin
  case ComboBox5.ItemIndex of
    0: Comm1.StopBits := _1;
    1: Comm1.StopBits := _1_5;
    2: Comm1.StopBits := _2;
    end;
  if CommSwitch = True then
  begin
    comm1.StopComm;
    Comm1.StartComm;
  end;
end;

procedure TForm1.SpeedButton6Click(Sender: TObject);
var SendStr: String;
    mstr:String;
    SendHex: array [0..100] of char;
    i:integer;
begin
  SendStr := '';
  mstr := '';
  for i := 0 to Memo1.Lines.Count-1 do
  begin
    SendStr := SendStr + Trim(Memo1.Lines.Strings[i]);
  end;
  for i := 1 to Length(SendStr) do
  begin
    if SendStr[i] <> ' ' then mstr := mstr + SendStr[i];
  end;
  if CheckBox1.Checked = True then
  begin
    for i := 0 to Length(mstr) div 2 -1 do
    begin
      SendHex[i] := chr(StrToInt64('$' + copy(mstr,i*2+1,2)));
    end;
    Comm1.WriteCommData(@SendHex,Length(mstr) div 2);
  end else
  begin
    Comm1.WriteCommData(@SendStr,Length(SendStr));
  end;

end;

end.

⌨️ 快捷键说明

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