?? unit1.~pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, MSCommLib_TLB, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
MSComm1: TMSComm;
Memo1: TMemo;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Label3: TLabel;
Edit2: TEdit;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Mscomm1.InBufferCount :=0; // 清空接收緩沖區
Mscomm1.InputLen :=0; // Input讀取整個緩沖區內容
Mscomm1.RThreshold :=1; // 每次接收1個字符即產生OnComm事件
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Mscomm1.Settings :=Edit1.Text; //通信參數('波特率,奇偶校驗,數據位停止位')
Mscomm1.CommPort :=StrToInt(Edit2.Text); //串口號 com幾
Mscomm1.PortOpen :=true; // 打開串口
Mscomm1.DTREnable :=true; // 數據終端準備好
Mscomm1.RTSEnable :=true; // 請求發送
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Mscomm1.PortOpen :=false; // 關閉串口
Mscomm1.DTREnable :=false;
Mscomm1.RTSEnable :=false;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
recstr:Olevariant; //接收的數據
wt,ht:string; //wt體重,ht身高
begin
recstr:='';
if Mscomm1.CommEvent = 2 then
begin
recstr := Mscomm1.Input ; //接收數據
if (pos(Char($02),recstr)=1) and (pos(Char($03),recstr)=24) then //確認數據格式
begin //從數據中COPY體重和身高的子串
wt:=copy(recstr,6,5);
ht:=copy(recstr,17,5);
Memo1.text := Memo1.Text + '體重:'+wt+'公斤 身高:'+ht+'厘米'+char(13)+char(10);
end
else
Mscomm1.InBufferCount :=0;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Memo1.text := '';
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -