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

📄 unit1.pas

📁 RS232串口通讯随书源码
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, TComm1;

type
  TForm1 = class(TForm)
    rdCOM: TRadioGroup;
    Label1: TLabel;
    Label2: TLabel;
    btnOpenPort: TButton;
    btnEnd: TButton;
    spCD: TShape;
    spDSR: TShape;
    spCTS: TShape;
    Label3: TLabel;
    Label4: TLabel;
    spRI: TShape;
    Timer1: TTimer;
    spDTR: TShape;
    Label5: TLabel;
    Label6: TLabel;
    spRTS: TShape;
    btnDTR: TButton;
    btnRTS: TButton;
    Comm1: TComm;
    procedure btnOpenPortClick(Sender: TObject);
    procedure btnEndClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure btnDTRClick(Sender: TObject);
    procedure btnRTSClick(Sender: TObject);
  private

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  hComm: THandle;

implementation

{$R *.DFM}

//以下是打开通信端口的程序
procedure TForm1.btnOpenPortClick(Sender: TObject);
begin
 //确定通信端口号码
 case rdcom.ItemIndex of
  0: Comm1.CommPort:=pnCOM1;
  1: Comm1.CommPort:=pnCOM2;
 end;
 //打开通信端口
 Comm1.PortOpen :=True;
 //先将DTR、RTS降成低电压
 Comm1.DTREnabled:=False;
 Comm1.RTSEnabled:=False;
end;


//以下是结束按钮的动作
procedure TForm1.btnEndClick(Sender: TObject);
begin
   //关闭通信端口
   Comm1.PortOpen:=False;
   //结束程序
   close;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
     //检查CTS状态
     if Comm1.CTSHolding  then
       spCTS.Brush.Color :=clRed
     else spCTS.Brush.Color :=clWhite;
     //检查DSR状态
     if Comm1.DSRHolding  then
       spDSR.Brush.Color :=clRed
     else spDSR.Brush.Color :=clWhite;
     //检查RI状态
     if Comm1.RIHolding  then
       spRI.Brush.Color :=clRed
     else spRI.Brush.Color :=clWhite;
     //检查CD状态
     if Comm1.CDHolding  then
       spCD.Brush.Color :=clRed
     else spCD.Brush.Color :=clWhite;

end;



procedure TForm1.btnDTRClick(Sender: TObject);
begin
  //改变DTR脚位的状态
  Comm1.DTREnabled :=not Comm1.DTREnabled;
  if Comm1.DTREnabled then
    spDTR.Brush.Color:=clRed  //改变灯号
  else
    spDTR.Brush.Color:=clWhite;
end;

procedure TForm1.btnRTSClick(Sender: TObject);
begin
  //改变RTS脚位的状态
  Comm1.RTSEnabled :=not Comm1.RTSEnabled;
  if Comm1.RTSEnabled then
    spRTS.Brush.Color:=clRed //改变灯号为红色
  else
    spRTS.Brush.Color:=clWhite;

end;
end.

⌨️ 快捷键说明

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