Question

I have problem with delphi code... I have code:

MAIN FORM

unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CPort, Menus, ComObj, StdCtrls;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    Berkas1: TMenuItem;
    Alat1: TMenuItem;
    erminal1: TMenuItem;
    ComPort1: TComPort;
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure erminal1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ComPort1RxChar(Sender: TObject; Count: Integer);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses
  ChildForm;

{$R *.dfm}

procedure TMainForm.erminal1Click(Sender: TObject);
var
  ChildForm: TChildForm;
begin
  ChildForm := TChildForm.Create(Application);
  ChildForm.Show;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
  ComPort1.ShowSetupDialog;
end;

procedure TMainForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
  ComPort: TComPort;
  data: string;
begin
  inherited;
  ComPort := TComPort.Create(Self);
  ComPort1.ReadStr(data, 5);
  ChildForm.Memo1.Text := ChildForm.Memo1.Text+''+data+'';
end;

end.

CHILD FORM:

unit ChildForm;

interface

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

type
  TChildForm = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  ChildForm: TChildForm;

implementation

uses
  MainForm;

{$R *.dfm}

procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TChildForm.Button1Click(Sender: TObject);
begin
  MainForm.ComPort1.Open;
end;

end.

I want to show data input from my device to memo in child form. I put the comport component in main form. But when I run the program, it says:

Project Data.exe raised exception class EAccessViolation with message 'Access violation at address 00466051 in module 'Data.exe'. Read of address 000002F8'. Process stopped. Use Step or Run to continue.

How can i solve the problem?

Was it helpful?

Solution

There are many problems with your code as mentioned in the comments.

To make a better implementation of your parent/child form interaction with the comport component, do as follows:

Create a TDataModule (ex: DataModule1), put the comport component there.

Now you can access the comport component from the main form and the child form.

Add a private method to your child form:

procedure TChildForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
  data: string;
begin
  DataModule1.ComPort1.ReadStr(data, 5);
  Self.Memo1.Text := Self.Memo1.Text+''+data+'';
end;

When you open the comport in the child form, set the comport OnRxChar event to your TChildForm.ComPort1RxChar method.

In the TChildForm.OnClose event, set the comport OnRxChar event to nil and close the comport.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top