?? unit1.~pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, MSCommLib_TLB, StdCtrls;
type
TForm1 = class(TForm)
MSComm1: TMSComm;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Label1: TLabel;
Label2: TLabel;
procedure MSComm1Comm(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.MSComm1Comm(Sender: TObject);
var
recstr : OleVariant;
begin
if Mscomm1.CommEvent = 2 then
begin
recstr := Mscomm1.Input ;
Memo1.text := Memo1.Text+recstr;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Mscomm1.Settings :=ComboBox1.Text;
if ComboBox2.Text ='com1' then // 串口只有com1和com2兩種情況
Mscomm1.CommPort :=1
else
Mscomm1.CommPort :=2;
Mscomm1.PortOpen :=true; // 打開串口
Mscomm1.DTREnable :=true; // 數據終端準備好
Mscomm1.RTSEnable :=true; // 請求發送
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Mscomm1.InBufferCount :=0; // 清空接收緩沖區
Mscomm1.InputLen :=0; // Input讀取整個緩沖區內容
Mscomm1.RThreshold :=1; // 每次接收到字符即產生OnComm事件
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Mscomm1.PortOpen :=false; // 關閉串口
Mscomm1.DTREnable :=false;
Mscomm1.RTSEnable :=false;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -