Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Comport in a DLL

Posted on 2014-04-24
10
Medium Priority
?
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 750 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 28

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 28

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 28

Accepted Solution

by:
Sinisa Vuk earned 750 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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