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