Solved

Comport in a DLL

Posted on 2014-04-24
10
487 Views
Last Modified: 2014-04-28
Hello,
We are trying to build a DLL with the Comport component.
It just doesn’t work :(
I get an « Access violation error » as soon as I set any of the Comport parameters.

I have built a very simple example for testing purpose and I get the exact same error.
Please see my code and help me to fix problem.

It looks like if the DLL behaves as if it had all the memory of the computer at disposal.
Environment is :  Windows 7, Delphi 7, Comport 3.1

program Project1;
uses
  ShareMem,
  Forms,
  testDLL in 'testDLL.pas' {Form1};
{$R *.res}
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Open in new window

Unit
unit testDLL;

interface

uses
  ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TShowPort = procedure;stdcall;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    ShowPort: TShowPort;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
  DLLModule:THandle;
  s:string;
begin
  DLLModule := LoadLibrary('mydll.dll');
  @ShowPort := nil;
  @ShowPort := GetProcAddress(DLLModule, 'ShowPort');
  ShowPort;
end;

end.

Open in new window

DLL
library mydll;

uses
  ShareMem,SysUtils,Dialogs,CPort,
  Classes;

  var
    ComPort1:Tcomport;
{$R *.res}

procedure ShowPort;stdcall;
begin
  ShowMessage('address COMMPORT:'+IntToStr(Integer(@ComPort1)));
  ShowMessage('adresse COMMPORT BAUDRATE:'+IntToStr(Integer(@ComPort1.BaudRate)));

 ComPort1.BaudRate:=br2400; // <== access violation is here
end;

exports ShowPort;


begin
end.

Open in new window

0
Comment
Question by:PierreAlyon
10 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 40020820
Where do you initialize your comport object?
0
 

Author Comment

by:PierreAlyon
ID: 40021164
I tried to initialize it in the DLL like this

procedure ShowPort;stdcall;
begin
  ShowMessage('address COMMPORT:'+IntToStr(Integer(@ComPort1)));
  ShowMessage('adresse COMMPORT BAUDRATE:'+IntToStr(Integer(@ComPort1.BaudRate)));
 ComPort1:=ComPort1.Create(nil);
 ComPort1.BaudRate:=br2400; // <== access violation is here
end;

Open in new window

 but it does not work for the same reason.
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 total points
ID: 40021910
i guess you'll be removing the showmessage's later on ?

ComPort1:=TComPort.Create(nil);

the T was forgotten ... and 1 was too much
0
 

Author Comment

by:PierreAlyon
ID: 40022049
Yes it works, thanks you !

I will remove the showmessage's.

Now i have another problem : how can I use RxChar event without access to the object inspector in my DLL?

I tried
Comport1.rxchar:= ComPort1RxChar;

Open in new window

with ComPort1RxChar declared like this :
 procedure ComPort1RxChar(Sender: TObject; InQue: Cardinal);virtual;

Open in new window

but it doesn't work.
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 40022252
must set which events  you want to use like:

Comport1.Events := [evRxChar]; //there are more if you need

Open in new window


... adnremove virtual keyword too.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:PierreAlyon
ID: 40023651
I think that
Comport1.Events := [evRxChar]; 

Open in new window

isn't sufficient because I  receive nothing on my comport component.
Maybe I have to indicate the routine associated with the event?
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 40023829
did you send something before waiting to receive?

you must set correct settings:
        FComPort.BaudRate:=br9600;
        FComPort.StopBits:=sbOneStopBit;
        FComPort.DataBits:=dbEight;
        FComPort.Port:='COM1';
        FComPort.Parity.Bits:=prNone;

        FComPort.FlowControl.FlowControl := fcHardware; 
//or  FComPort.FlowControl.FlowControl := fcNone;
...
//dtr/rts...

Open in new window

0
 

Author Comment

by:PierreAlyon
ID: 40024417
I send an ENQ before waiting to receive and I set all settings of ComPort component.
0
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 250 total points
ID: 40024556
Made simple function which send some command and wait for:

function GetComPortAnswer(sText: String; var sReadStr: String): Boolean;
const
  C_PRINT_BUFF_SIZE = 512;
var
  FComPort: TComPort;
  sIn, sBuf: String;
begin
  Result:=False;
  sReadStr:='';

  try
    FComPort:=TComPort.Create(nil);
    try
      FComPort.BaudRate:=...
      FComPort.StopBits:=...
      FComPort.DataBits:=...
      FComPort.Port:=...
      FComPort.Events:=[];
      FComPort.FlowControl.FlowControl :=...
      FComPort.FlowControl.ControlDTR := ...
      FComPort.FlowControl.DSRSensitivity :=....

      //TFlowControl = (fcHardware, fcSoftware, fcNone, fcCustom);
      //TDTRFlowControl = (dtrDisable, dtrEnable, dtrHandshake);
      //TRTSFlowControl = (rtsDisable, rtsEnable, rtsHandshake, rtsToggle);

      FComPort.Open;
      if FComPort.Connected then
      begin
        FComPort.ClearBuffer(True, True);

        repeat
          sBuf := Copy(sText, 1, C_PRINT_BUFF_SIZE);
          sText := Copy(sText, C_PRINT_BUFF_SIZE+1, Length(sText)-C_PRINT_BUFF_SIZE);
          if sBuf<>'' then
          begin
            Result := (FComPort.Writestr(sBuf) > 0);
            Sleep(100);
          end;
        until Length(sBuf)=0;

        while (FComPort.InputCount>0) do
        begin
          FComPort.ReadStr(sIn, FComPort.InputCount);
          sReadStr := sReadStr+sIn;
        end;

        Result:=Result and True;
      end;
    finally
      if FComPort.Connected then
        FComPort.Close;

      FComPort.Free;
    end;
  except
  end;
end;

Open in new window


... you need to set proper com parameters and in sReadStr you will get an answer.
You can add timeout logic too - this is almost a must.
0
 

Author Comment

by:PierreAlyon
ID: 40026678
Thank you for your help but I finally put my ComPort component into a form to create the RxChar event with the object inspector and it works.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now