0

我在处理一个小 dll 并在其上使用 TComport 组件。我在这个 dll 中有一个函数,它接受一些参数并返回一个字符。我在项目中添加了一个数据模块,并在其上放置了 TComport 和 TComDataPacket。一切正常,但 TComport 无法捕获任何事件。例如,我想从 TComDataPacket 组件的 OnPacket evet 中的设备中获取字符串。任何建议我为我糟糕的英语道歉。

library VoteService;
uses
  System.SysUtils,
  System.Classes,
  Extra in 'Extra\Extra.pas',
  Un_Dm in 'DataModule\Un_Dm.pas' {DM: TDataModule},
  CPort in 'CPort\CPort.pas';


var ComPort1 : TComPort;
    ComDataPacket1 : TComDataPacket;

{$R *.res}

function getVote(personnelCode:Pchar; docCode: Pchar):Integer; stdcall;
var
  intStatus, intIdentifier: Integer;
  strStatus_message: string;
  Port: TPort;
  StopBits: TStopBits;
  Parity: TParityBits;
  DataBits: TDataBits;
  BaudRate: TBaudRate;
  i, j : Integer;
begin
  Answer := 0;
  SerialPortSetting(Port, StopBits, Parity, DataBits, BaudRate);
  dm := TDM.Create(nil);
  try
  DM.ComPort1.Port := Port;
  DM.ComPort1.StopBits := StopBits;
  DM.ComPort1.Parity.Bits := Parity;
  DM.ComPort1.DataBits := DataBits;
  DM.ComPort1.BaudRate := BaudRate;

  DM.ComPort1.Connected := True;
  DM.ComPort1.WriteStr('*');
  DM.ComPort1.Close;
  DM.ComPort1.Open;
  for i  := 0 to 5 do
  begin
    j := 0;
    while Answer = 0 do
    begin
      //setAnswer;
    end;
    Result := Answer;
  end;
  finally
    dm.Free;
  end;
end;

exports
  getVote;

begin

end.
4

1 回答 1

0

我的问题解决了,实际上问题不在 DataModule 上,而是我无法在 Dll 项目中捕获任何 ComPort 事件,最后我知道我必须使用循环并持续检查,直到我可以得到我期望的值。我把正确的代码放在这里,
谢谢大家。

library VoteService;

uses
  System.SysUtils, DateUtils,
  System.Classes,
  Extra in 'Extra\Extra.pas',
  Un_Dm in 'DataModule\Un_Dm.pas' {DM: TDataModule},
  CPort in 'CPort\CPort.pas';

{$R *.res}

function getVote(personnelCode:Pchar; docCode: Pchar; waitSecound : Integer):Integer;   stdcall;
var
  intStatus, intIdentifier: Integer;
  strStatus_message: string;
  Port: TPort;
  StopBits: TStopBits;
  Parity: TParityBits;
  DataBits: TDataBits;
  BaudRate: TBaudRate;
  s : string;
  tmpTime : TTime;
begin
  Answer := 0;
  SerialPortSetting(Port, StopBits, Parity, DataBits, BaudRate);
  dm := TDM.Create(nil);
  try
    DM.ComPort1.Port := Port;
    DM.ComPort1.StopBits := StopBits;
    DM.ComPort1.Parity.Bits := Parity;
    DM.ComPort1.DataBits := DataBits;
    DM.ComPort1.BaudRate := BaudRate;
    DM.ComPort1.Connected := True;
    DM.ComPort1.WriteStr('*');
    DM.ComPort1.Close;
    DM.ComPort1.Open;
    tmpTime := Now;
    s := '';
    repeat DM.ComPort1.ReadStr(s, len);
    until (SecondsBetween(Now, tmpTime) > waitSecound) or (length(s)>0);
    if s = '' then
       Result := 0
    else
       Result := StrToInt(s);
  finally
    dm.Free;
  end;
end;
exports
  getVote;

begin

end.
于 2014-01-05T13:35:14.033 回答