Recent

Author Topic: PascalSCADA COM Port  (Read 2509 times)

anugrahs

  • Newbie
  • Posts: 4
PascalSCADA COM Port
« on: August 13, 2024, 07:56:40 am »
Hi everyone,

I'm currently working on a project in Lazarus where I'm utilizing the PascalSCADA library to manage serial communication. My goal is to create a user-friendly interface that allows the selection of a COM port through a combo box in the application.

Problem:
I’ve implemented the UI for COM port selection, but I’m facing an issue where the available COM ports do not appear in the combo box as expected. I’ve followed the standard procedure for listing COM ports using PascalSCADA, but no ports show up when I run the application.

What I've Tried:

I’ve ensured that the PascalSCADA library is correctly installed and configured in my Lazarus environment.
I’ve reviewed the documentation for PascalSCADA and tried initializing the COM port settings in different parts of the application ( i used form initialization, 2 button , Combobox, and Serial Port Driver).
I’ve also confirmed that the COM ports are working properly on my machine by testing them with other applications.

**Code Snippet:**
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, SerialPort, commtypes;

type

  { TForm1 }

  TForm1 = class(TForm)
    BClose: TButton;
    BOpen: TButton;
    ComboBox1: TComboBox;
    SerialPortDriver1: TSerialPortDriver;
    procedure FormCreate(Sender: TObject);
    procedure BOpenClick(Sender: TObject);
    procedure BCloseClick(Sender: TObject);
    procedure SerialPortDriver1CommErrorReading(Error: TIOResult);
    procedure SerialPortDriver1CommPortOpened(Sender: TObject);
    procedure SerialPortDriver1CommPortClosed(Sender: TObject);
  private
    procedure SetupPortSettings;
    procedure PopulateSerialPorts;
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.SetupPortSettings;
begin
  if ComboBox1.ItemIndex <> -1 then
  begin
    SerialPortDriver1.COMPort := ComboBox1.Text; // Mengatur port yang dipilih
    SerialPortDriver1.BaudRate := br9600;     // Contoh: Mengatur BaudRate ke 9600
    SerialPortDriver1.DataBits := db8;        // Contoh: Mengatur DataBits ke 8
    SerialPortDriver1.Paridade := spNone;       // Contoh: Mengatur Parity ke None
    SerialPortDriver1.StopBits := sb1;        // Contoh: Mengatur StopBits ke 1
  end
  else
    ShowMessage('Please select a port from the ComboBox.');
end;

procedure TForm1.PopulateSerialPorts;
begin
  ComboBox1.Items.Clear;
  ComboBox1.Items.AddStrings(SerialPortDriver1.COMPort);
  if ComboBox1.Items.Count > 0 then
    ComboBox1.ItemIndex := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  PopulateSerialPorts;
end;

procedure TForm1.BOpenClick(Sender: TObject);
begin
  SetupPortSettings;
  try
    SerialPortDriver1.Active := True;  // Open the port
  except
    on E: Exception do
      ShowMessage('Failed to open port: ' + E.Message);
  end;
end;

procedure TForm1.BCloseClick(Sender: TObject);
begin
  try
    if SerialPortDriver1.Active then
  SerialPortDriver1.Active := False;
  except
    on E: Exception do
      ShowMessage('Failed to close port: ' + E.Message);
  end;
end;

procedure TForm1.SerialPortDriver1CommErrorReading(Error: TIOResult);
begin

end;

// Event handler ketika port dibuka
procedure TForm1.SerialPortDriver1CommPortOpened(Sender: TObject);
begin
  ShowMessage('Port opened successfully.');
end;

// Event handler ketika port ditutup
procedure TForm1.SerialPortDriver1CommPortClosed(Sender: TObject);
begin
  ShowMessage('Port closed successfully.');
end;

end.

Thank you in advance for your help!

Best regards, 
Anugrah

TRon

  • Hero Member
  • *****
  • Posts: 3294
Re: PascalSCADA COM Port
« Reply #1 on: August 13, 2024, 08:20:36 am »
I’ve implemented the UI for COM port selection, but I’m facing an issue where the available COM ports do not appear in the combo box as expected. I’ve followed the standard procedure for listing COM ports using PascalSCADA, but no ports show up when I run the application.
The standard procedure for listing com ports using pascalSCADA ?

Code: Pascal  [Select][+][-]
  1. SerialPortDriver1.COMPort := ComboBox1.Text; // Mengatur port yang dipilih
  2. ...
  3. ComboBox1.Items.AddStrings(SerialPortDriver1.COMPort);
  4.  
I hope you got the gist that besides the need to obtain the name of the port yourself that this self-provided-point-to-the-other loop is not going to work ?
This tagline is powered by AI

bpranoto

  • Full Member
  • ***
  • Posts: 183
Re: PascalSCADA COM Port
« Reply #2 on: August 13, 2024, 08:25:02 am »
The problem is in ComboBox1.Items.AddStrings(SerialPortDriver1.COMPort);

Assuming you have the source of the library, you can try to debug SerialPortDriver1.COMPort

If you in linux/freebsd make sure the logged user has the correct permission to access the serial port devices.

Semoga membantu.

anugrahs

  • Newbie
  • Posts: 4
Re: PascalSCADA COM Port
« Reply #3 on: August 13, 2024, 11:26:31 am »
The problem is in ComboBox1.Items.AddStrings(SerialPortDriver1.COMPort);

Assuming you have the source of the library, you can try to debug SerialPortDriver1.COMPort

If you in linux/freebsd make sure the logged user has the correct permission to access the serial port devices.

Semoga membantu.

are u indonesian too ?. I use windows for the OS, honestly im amateur for lazarus software then i'm so confused what to do for how to edit this code.... can u help me..

tetrastes

  • Hero Member
  • *****
  • Posts: 540
Re: PascalSCADA COM Port
« Reply #4 on: August 13, 2024, 12:06:51 pm »
Go to https://github.com/geby/synapse, get function GetSerialPortNames from synaser.pas (it is at the end of file), then
Code: Pascal  [Select][+][-]
  1. ComboBox1.Items.CommaText := GetSerialPortNames;

anugrahs

  • Newbie
  • Posts: 4
Re: PascalSCADA COM Port
« Reply #5 on: August 13, 2024, 02:01:32 pm »
Go to https://github.com/geby/synapse, get function GetSerialPortNames from synaser.pas (it is at the end of file), then
Code: Pascal  [Select][+][-]
  1. ComboBox1.Items.CommaText := GetSerialPortNames;

thank you for the reply, right now i'm so confused if i should modify the code that i made with the new one. Can help me make the simple code about com port selection and use combobox, button, etc...

TRon

  • Hero Member
  • *****
  • Posts: 3294
Re: PascalSCADA COM Port
« Reply #6 on: August 13, 2024, 02:17:43 pm »
It is not rocket science  :)

The component does not provide a list of serial devices (at least not that i am aware of in the version I skimmed through) so you have to add them yourself to the combobox in order to be able to select one from it.

tetrates suggested to use synapse to retrieve a list of serial ports (I presume on/for any given platform) but for testing purposes you can manually assign a name for the port (assuming that you know the name of the port to test).
« Last Edit: August 13, 2024, 02:26:27 pm by TRon »
This tagline is powered by AI

PascalDragon

  • Hero Member
  • *****
  • Posts: 5690
  • Compiler Developer
Re: PascalSCADA COM Port
« Reply #7 on: August 13, 2024, 09:23:42 pm »
**Code Snippet:**

Please use [code=pascal][/code] to avoid the forum software possibly interpreting your code and to make it better readable.

anugrahs

  • Newbie
  • Posts: 4
Re: PascalSCADA COM Port
« Reply #8 on: August 16, 2024, 04:06:29 am »
Hi everyone,

I'm currently working on a project in Lazarus where I'm utilizing the PascalSCADA library to manage serial communication. My goal is to create a user-friendly interface that allows the selection of a COM port through a combo box in the application.

Problem:
I’ve implemented the UI for COM port selection, but I’m facing an issue where the available COM ports do not appear in the combo box as expected. I’ve followed the standard procedure for listing COM ports using PascalSCADA, but no ports show up when I run the application.

What I've Tried:

I’ve ensured that the PascalSCADA library is correctly installed and configured in my Lazarus environment.
I’ve reviewed the documentation for PascalSCADA and tried initializing the COM port settings in different parts of the application ( i used form initialization, 2 button , Combobox, and Serial Port Driver).
I’ve also confirmed that the COM ports are working properly on my machine by testing them with other applications.

**Code Snippet:**
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, SerialPort, commtypes;

type

  { TForm1 }

  TForm1 = class(TForm)
    BClose: TButton;
    BOpen: TButton;
    ComboBox1: TComboBox;
    SerialPortDriver1: TSerialPortDriver;
    procedure FormCreate(Sender: TObject);
    procedure BOpenClick(Sender: TObject);
    procedure BCloseClick(Sender: TObject);
    procedure SerialPortDriver1CommErrorReading(Error: TIOResult);
    procedure SerialPortDriver1CommPortOpened(Sender: TObject);
    procedure SerialPortDriver1CommPortClosed(Sender: TObject);
  private
    procedure SetupPortSettings;
    procedure PopulateSerialPorts;
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.SetupPortSettings;
begin
  if ComboBox1.ItemIndex <> -1 then
  begin
    SerialPortDriver1.COMPort := ComboBox1.Text; // Mengatur port yang dipilih
    SerialPortDriver1.BaudRate := br9600;     // Contoh: Mengatur BaudRate ke 9600
    SerialPortDriver1.DataBits := db8;        // Contoh: Mengatur DataBits ke 8
    SerialPortDriver1.Paridade := spNone;       // Contoh: Mengatur Parity ke None
    SerialPortDriver1.StopBits := sb1;        // Contoh: Mengatur StopBits ke 1
  end
  else
    ShowMessage('Please select a port from the ComboBox.');
end;

procedure TForm1.PopulateSerialPorts;
begin
  ComboBox1.Items.Clear;
  ComboBox1.Items.AddStrings(SerialPortDriver1.COMPort);
  if ComboBox1.Items.Count > 0 then
    ComboBox1.ItemIndex := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  PopulateSerialPorts;
end;

procedure TForm1.BOpenClick(Sender: TObject);
begin
  SetupPortSettings;
  try
    SerialPortDriver1.Active := True;  // Open the port
  except
    on E: Exception do
      ShowMessage('Failed to open port: ' + E.Message);
  end;
end;

procedure TForm1.BCloseClick(Sender: TObject);
begin
  try
    if SerialPortDriver1.Active then
  SerialPortDriver1.Active := False;
  except
    on E: Exception do
      ShowMessage('Failed to close port: ' + E.Message);
  end;
end;

procedure TForm1.SerialPortDriver1CommErrorReading(Error: TIOResult);
begin

end;

// Event handler ketika port dibuka
procedure TForm1.SerialPortDriver1CommPortOpened(Sender: TObject);
begin
  ShowMessage('Port opened successfully.');
end;

// Event handler ketika port ditutup
procedure TForm1.SerialPortDriver1CommPortClosed(Sender: TObject);
begin
  ShowMessage('Port closed successfully.');
end;

end.

Thank you in advance for your help!

Best regards, 
Anugrah

Thank you for all the support, i can finished this PascalSCADA COM Port

 

TinyPortal © 2005-2018