Forum > Options

PascalSCADA COM Port

(1/2) > >>

anugrahs:
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:

--- Quote from: anugrahs on August 13, 2024, 07:56:40 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.

--- End quote ---
The standard procedure for listing com ports using pascalSCADA ?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SerialPortDriver1.COMPort := ComboBox1.Text; // Mengatur port yang dipilih...ComboBox1.Items.AddStrings(SerialPortDriver1.COMPort); 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 ?

bpranoto:
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:

--- Quote from: bpranoto 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.

--- End quote ---

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:
Go to https://github.com/geby/synapse, get function GetSerialPortNames from synaser.pas (it is at the end of file), then

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ComboBox1.Items.CommaText := GetSerialPortNames;

Navigation

[0] Message Index

[#] Next page

Go to full version