treadunit.pas

来自「用来模拟PC机的串口」· PAS 代码 · 共 58 行

PAS
58
字号
unit TreadUnit;

interface

uses Windows,Classes;


type
TSerialThread = class (TThread)
private
  AppHandle:HWND;
  SerialHandle : integer;
  sermes : integer;
protected
  procedure Execute; override;
public
  constructor Create(SerHandle:integer;mes:integer;ApHndl:HWND);
end;

implementation


constructor TSerialThread.Create(SerHandle:integer;mes : integer;ApHndl:HWND);
begin
  AppHandle:=ApHndl;      { Store the handle of the main application            }
  SerialHandle:=SerHandle;{ Store the handle of the serial port                 }
  sermes:=mes;            { Store the user-defined message value. This will be  }
                          { used by the application to check if the message     }
                          { is send by this thread, and thus a serial event has }
                          { ocurred.                                            }

  FreeOnTerminate:= True; { When the thread is done, it will close down and     }
                          { free all of its resources.                          }

  inherited Create (False);{ Call the constructor of the used base-class        }
end;

procedure TSerialThread.Execute;
var
  SerialEvent : Cardinal;
begin
  SetCommMask (SerialHandle,
  EV_CTS or EV_BREAK or EV_DSR or
  EV_ERR or EV_RING or EV_RLSD or
  EV_RXCHAR or EV_RXFLAG or EV_TXEMPTY);
  WaitCommEvent (SerialHandle,SerialEvent,nil);
{ Now, execution is halted, until some event occurs on the      }
{ serial port.                                                  }

  PostMessage (AppHandle,sermes,SerialEvent,0);
{ Send a message to the application, to notify the change in    }
{ state of the serial port.                                     }
end;

begin
end.

⌨️ 快捷键说明

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