Solved

Comport in a DLL

Posted on 2014-04-24
10
531 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 220
Dev Express grid collapse 2 45
Moving (cutting/pasting) controls in a TTabbedNotebook... 7 34
How to build JSON File in Delphi 6 3 35
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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