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

📄 sendthread.pas

📁 使用Delphi 6.0开发用于控制空调的程序
💻 PAS
字号:
unit SendThread;

interface

uses
  Classes,SysUtils;

type
  TSendThread = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
    procedure CycSend();
  end;

implementation

{ Important: Methods and properties of objects in VCL or CLX can only be used
  in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TSendThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ TSendThread }

uses
  Main;

procedure TSendThread.Execute;
begin
  { Place thread code here }
  FreeOnTerminate := True;
  Synchronize(CycSend);
end;

procedure TSendThread.CycSend();
var
  strLines:string;
begin
   if iSendCount >= 1 then //命令发送优先
   begin
         // Main.frmain.mmo2.Lines.Add('执行线程');
          if not main.frmain.Spcomm1.PortOpen  then
          begin
              try
                 main.frmain.Spcomm1.StartComm;
              except
                frmain.statMain.Panels[1].Text := '打开串口失败,'+ '当前串口名:'+Main.frmain.Spcomm1.CommName+',波特率为:'+IntToStr(Main.frmain.Spcomm1.BaudRate);
              end;
          end   //打开串口
          else
          begin
                 strLines := strSend[iSendCount];
                 if strLines <> '' then
                 begin
                   try
                     main.frmain.Spcomm1.WriteCommData( PChar(strLines),Length(strLines));
                     Dec(iSendCount);
                     main.frmain.statMain.Panels[1].Text := '用户命令优先,发送成功'+strLines ;
                   except
                     main.frmain.statMain.Panels[1].Text := '用户命令优先,发送失败'+strLines ;
                     Exit;
                   end;
                 end
                 else
                    iSendCount := 0;
          end;  
   end
   else  //循环发送
   begin
          if iSendCyc = 0 then
            iSendCyc := 1;
          if iSendCyc >= frmain.iNum * 2 + 1then
             iSendCyc := 1;
          //Main.frmain.mmo2.Lines.Add('执行线程,循环发送'+ strSendCyc[iSendCyc]);
          if not main.frmain.Spcomm1.PortOpen  then
          begin
              try
                 main.frmain.Spcomm1.StartComm;
              except
                main.frmain.statMain.Panels[1].Text := '打开串口失败,'+ '当前串口名:'+Main.frmain.Spcomm1.CommName+',波特率为:'+IntToStr(Main.frmain.Spcomm1.BaudRate);
              end;
          end   //打开串口
          else
          begin
                 strLines := strSendCyc[iSendCyc];
                 if strLines <> '' then
                 begin
                   try
                     main.frmain.Spcomm1.WriteCommData( PChar(strLines),Length(strLines));
                     Inc(iSendCyc);
                     main.frmain.statMain.Panels[1].Text := '循环发送,发送成功'+strLines ;
                   except
                     main.frmain.statMain.Panels[1].Text := '循环发送,发送失败'+strLines ;
                     Exit;
                   end;
                 end
                 else
                    iSendCyc := 1;
          end;
   end;
 end;                   
end.

⌨️ 快捷键说明

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