Delphi 10.3 控制台应用程序中的 Tcomport

Tcomport in console application with Delphi 10.3

我在一个简单的 Delphi 10.3 控制台应用程序中使用 TComPort (4.11f),见下文,但我没有获得任何数据。

在 VCL 应用程序中使用相同的代码效果很好。有什么想法吗?

我的猜测是我没有正确连接 ComDataPacket1ComPort1 对象。但是,我找不到其他方法。

program commapp;

{$APPTYPE CONSOLE}

uses
  Vcl.Forms, CPort, CPortCtl, system.sysutils, classes, Windows;

{$R *.res}

var
  ComPort1: TComPort;
  ComDataPacket1: TComDataPacket;
  Sender: TObject;
  Str: string;

type
  TEventHandlers = class
    class procedure ComDataPacket1Packet(Sender: TObject; const Str: string);
  end;

class procedure TEventHandlers.ComDataPacket1Packet(Sender: TObject; const Str: string);
begin
  WriteLn('Inside the proc with ' + str);
end;

begin // Main program                 ///
  ComPort1 := TComPort.Create(nil);  // create the    comm
  ComDataPacket1 := TComDataPacket.Create(nil);
  ComPort1.BaudRate := br115200;                 // set parameters
  ComPort1.Port     := 'COM3';
  ComPort1.Parity.Bits := prNone;

  ComDataPacket1.Size := 33;
  ComDataPacket1.ComPort  := ComPort1;
  ComDataPacket1.OnPacket := TEventHandlers.ComDataPacket1Packet;
  ComDataPacket1.StartString := '';
  ComDataPacket1.StopString := '';

  if ParamCount = 0 then // no paramters passed enter menu
  begin
    ComPort1.ShowSetupDialog; // Open the port settings.
    ComPort1.Open;
    ComPort1.WriteStr('b') ;  // Send the command to start sending data
    WriteLn('After sending an b I get  :  ' + str); 
    ReadLn;
  end;

  // release the components
  ComDataPacket1.Free;
  ComPort1.Free;
end.

在等待 OnPacket 事件触发时尝试 运行 消息循环,例如:

program commapp;

{$APPTYPE CONSOLE}

uses
  Vcl.Forms, CPort, CPortCtl, System.SysUtils,
  System.Classes, Winapi.Windows, System.SyncObjs;

{$R *.res}

var
  ComPort1: TComPort;
  ComDataPacket1: TComDataPacket;
  DataEvent: TEvent;
  hEvent: THandle;
  DataStr: string;

type
  TEventHandlers = class
    class procedure ComDataPacket1Packet(Sender: TObject; const Str: string);
  end;

class procedure TEventHandlers.ComDataPacket1Packet(Sender: TObject; const Str: string);
begin
  WriteLn('Inside the proc with ' + Str);
  DataStr := Str;
  DataEvent.SetEvent;
end;

begin // Main program
  ComPort1 := TComPort.Create(nil); // create the comm
  ComDataPacket1 := TComDataPacket.Create(nil);
  DataEvent := TEvent.Create;

  ComPort1.BaudRate := br115200; // set parameters
  ComPort1.Port := 'COM3';
  ComPort1.Parity.Bits := prNone;

  ComDataPacket1.Size := 33;
  ComDataPacket1.ComPort := ComPort1;
  ComDataPacket1.OnPacket := TEventHandlers.ComDataPacket1Packet;
  ComDataPacket1.StartString := '';
  ComDataPacket1.StopString := '';

  if ParamCount = 0 then // no paramters passed enter menu
  begin
    ComPort1.ShowSetupDialog; // Open the port settings.
  end;

  ComPort1.Open;
  ComPort1.WriteStr('b') ; // Send the command to start sending data

  hEvent := DataEvent.Handle;
  repeat
    case MsgWaitForMultipleObjects(1, hEvent, False, INFINITE, QS_ALLINPUT) of
      WAIT_OBJECT_0: Break;
      WAIT_OBJECT_0 + 1: Application.HandleMessage; // or Application.ProcessMessages()
      WAIT_FAILED: RaiseLastOSError;
    end;
  until False;

  WriteLn('After sending an b I get : ' + DataStr);
  ReadLn;

  // release the components
  DataEvent.Free;
  ComDataPacket1.Free;
  ComPort1.Free;
end.