Solved

Comport in a DLL

Posted on 2014-04-24
10
537 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 26

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
 

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 26

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 26

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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