unit1.pas

来自「Delphi下利用串口进行数据的读写」· PAS 代码 · 共 82 行

PAS
82
字号
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,pcomm;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Memo1: TMemo;
    Label10: TLabel;
    procedure Label1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Label1Click(Sender: TObject);
var
   myResult,i: integer; //定义一个整型变量用来保存结果
   mybuffer: pchar;  //定义一个指针串型变量用作输出缓冲
begin
      Memo1.Lines.Clear; //清除Memo1显示的原内容
      myResult:=SIO_open(1); //打开串口com1 :=表示“赋值”
      Memo1.Lines.Add('打开串口1,结果:'+Errorcode(myResult));
      myResult:=sio_SetWriteTimeouts(1,5000);
      Memo1.Lines.Add('设定串口1写超时时间为2秒,结果:'+Errorcode(myResult));
      MyResult:=SIO_baud(1,300);//设定串口1的波特率为300波特
      Memo1.Lines.Add('设定波特率为300波特,结果:'+Errorcode(myResult));
      for i:= 1 to 527  do begin ;
      Mybuffer:=chr($ff);
      MyResult:=SIO_write(1,mybuffer,64);//从缓冲区写64个字节到串口1
      Memo1.Lines.Add('写出,结果:'+Errorcode(myResult));
      end;
      for i:= 1 to 225 do begin ;
      Mybuffer:=chr($00);
      MyResult:=SIO_write(1,mybuffer,50);//从缓冲区写50个字节到串口1
      Memo1.Lines.Add('写出:'+Errorcode(myResult));
      end;
      for i:= 1 to 105  do begin ;
      Mybuffer:=chr($ff);
      MyResult:=SIO_write(1,mybuffer,64); //从缓冲区写64个字节到串口1
      Memo1.Lines.Add('写出,结果:'+Errorcode(myResult));
      end;
      for i:= 1 to 70 do begin ;
      Mybuffer:=chr($00);
      MyResult:=SIO_write(1,mybuffer,64); //从缓冲区写64个字节到串口1
      Memo1.Lines.Add('写出:'+Errorcode(myResult));
      end;
      for i:= 1 to 105  do begin ;
      Mybuffer:=chr($ff);
      MyResult:=SIO_write(1,mybuffer,64); //从缓冲区写64个字节到串口1
      Memo1.Lines.Add('写出,结果:'+Errorcode(myResult));
      end;
      for i:= 1 to 70 do begin ;
      Mybuffer:=chr($00);
      MyResult:=SIO_write(1,mybuffer,64); //从缓冲区写64个字节到串口1
      Memo1.Lines.Add('写出:'+Errorcode(myResult));
      end;
      MyResult:=SIO_close(1);
      Memo1.Lines.Add('写出:'+Errorcode(myResult));
end;
end.

⌨️ 快捷键说明

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