error on using tcomport in delphi dll

1.5k views Asked by At

I working on a small dll and I use TComport component on it. I have a function in this dll that takes some parameter and return a character. I add a Datamodule to the project an I put TComport and TComDataPacket on it. everything work good but TComport can't catch any events. for example I want to take the string from device in OnPacket evet of the TComDataPacket component. any suggestion my apologies for my bad english.

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.
1

There are 1 answers

0
Ali Sarmadi On

My problem is solved, Actually the problem wasn't on DataModule but it was I couldn't Catch any ComPort events in the Dll project, Finally I knew I must use a loop and check continual until I can take the value I expected it. I put the correct code here,
Thank you all.

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.