1

我必须将几个测量设备连接到我的应用程序(即卡尺、体重秤……),而不是绑定到特定的品牌或型号,所以在客户端我使用带有通用方法的接口(QueryValue)。设备连接在 COM 端口上并以异步方式访问:

  1. 请求一个值(= 在 COM 端口上发送一个特定的字符序列)
  2. 等待回复

在“业务”方面,我的组件在内部使用 TComPort,数据接收事件是TComPort.OnRxChar. 我想知道如何通过界面触发此事件?这是我到目前为止所做的:

IDevice = interface
  procedure QueryValue;
  function GetValue: Double;
end;

TDevice = class(TInterfacedObject, IDevice)
private
  FComPort: TComPort;
  FValue: Double;
protected
  procedure ComPortRxChar;
public
  constructor Create;
  procedure QueryValue;
  function GetValue: Double;
end;

constructor TDevice.Create;
begin
  FComPort := TComPort.Create;
  FComPort.OnRxChar := ComPortRxChar;
end;

// COM port receiving data
procedure TDevice.ComPortRxChar;
begin
  FValue := ...
end;

procedure TDevice.GetValue;
begin
  Result := FValue;
end;

但我需要一个事件来知道何时GetValue在客户端调用。执行这种数据流的常用方法是什么?

4

1 回答 1

1

您可以将事件属性添加到界面

IDevice = interface
  function GetValue: Double;
  procedure SetMyEvent(const Value: TNotifyEvent);
  function GetMyEvent: TNotifyEvent;
  property MyEvent: TNotifyEvent read GetMyEvent write SetMyEvent;
end;

并在 TDevice 类中实现

TDevice = class(TInterfacedObject, IDevice)
private
  FMyEvent: TNotifyEvent;
  procedure SetMyEvent(const Value: TNotifyEvent);
  function GetMyEvent: TNotifyEvent;
public
  function GetValue: Double;
  procedure EmulChar;
end;

然后通常FMyEventComPortRxChar.

 Tform1...
  procedure EventHandler(Sender: TObject);

procedure TForm1.EventHandler(Sender: TObject);
var
  d: Integer;
  i: IDevice;
begin
  i := TDevice(Sender) as IDevice;
  d := Round(i.GetValue);
  ShowMessage(Format('The answer is %d...', [d]));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  id: IDevice;
begin
  id:= TDevice.Create;
  id.MyEvent := EventHandler;
  (id as TDevice).EmulChar; //emulate rxchar arrival
end;

procedure TDevice.EmulChar;
begin
  if Assigned(FMyEvent) then
    FMyEvent(Self);
end;

function TDevice.GetMyEvent: TNotifyEvent;
begin
  Result := FMyEvent;
end;

function TDevice.GetValue: Double;
begin
  Result := 42;
end;

procedure TDevice.SetMyEvent(const Value: TNotifyEvent);
begin
  FMyEvent := Value;
end;
于 2016-04-19T10:00:47.387 回答