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

📄 unit1.pas

📁 利用MSComm32.ocx控件,Delphi实现体检医院测身高体重仪器与计算机的串口通讯。随附测试工具
💻 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 :=24; // 每次接收24个字符即产生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 + -