Solved

Comport in a DLL

Posted on 2014-04-24
10
507 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 101
delphi parse string to params 3 100
drawing animated level bar based on numbers 3 88
Firemonkey DbLookupComboBox equivalent ? 2 38
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

864 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

22 Experts available now in Live!

Get 1:1 Help Now