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

📄 unit2.pas

📁 此文章是关于Delphi串口通信类的一些文章和源代码,它是单片机串口通讯较好的上位机设计的参考!
💻 PAS
字号:
unit Unit2;

interface

uses
  Windows,Classes, SysUtils;

type
  TReadThread = class(TThread)
  private
    { Private declarations }
    procedure ReadPort;   //读取串行端口数据
  protected
    procedure Execute; override;
  end;

implementation
uses Unit1; // 声明引用Unit1,必须放在implementation区段

procedure TReadThread.Execute;
begin
  { Place thread code here }
  While not Terminated do
  begin
    Synchronize(ReadPort);//刚才所定义的读取数据函数
  end;
end;

//读取串行端口的程序放在这里
procedure TReadThread.ReadPort;
  var
  Temp : string;
  inbuff: array[0..2047] of Char;
  nBytesRead, dwEvent, dwError:LongWORD ;
  cs:TCOMSTAT;
  begin
   if (hComm=0) then exit;//先判断是否已打开通信端口
   ClearCommError(hComm,dwError,@CS);  //取得状态
   ReadFile(hComm, inbuff,cs.cbInQue,nBytesRead,nil); // 接收COM 的数据
   //串行在读取数据后,会自动将缓冲区中已被读取的数据清除掉
   if cs.cbInQue =0 then exit;
   // 数据是否大于我们所准备的Buffer
   if cs.cbInQue > sizeof(inbuff) then begin
     PurgeComm(hComm, PURGE_RXCLEAR);  // 清除COM 数据
     exit;
   end;
   Temp:=Copy(inbuff,1,cs.cbInQue);//取出数据
   Form1.mReceive.Text :=Form1.mReceive.Text + Temp;   // 将数据显示于Memo1 上
   Form1.mReceive.SetFocus ;
   Form1.mReceive.SelStart  :=Length(Form1.mReceive.Text);
   Form1.mReceive.SelLength :=0;  //移到最下端

end;


end.

⌨️ 快捷键说明

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