Recent

Author Topic: TLazSerial : serial port component for Lazarus (windows and linux).  (Read 403820 times)

CM630

  • Hero Member
  • *****
  • Posts: 1581
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #495 on: December 03, 2025, 02:27:14 pm »
I have uploaded some updated version.
1. Version changed from 0,6 to 0,7.
2. Another component added - SerialWatcher, all it does is generate events when a serial device is added or removed.
3. Some rearrangements are done, which makes some components public.
4. I have not tried to use the built-in sorting yet. But I feel safer to have sorting out of JP's code (now it is in LazSeialCommon.pas / procedure SortPorts).

5. Also there is LazSWather (also attached) - an app using SerialWatcher. Stays in the tray, showing info about available COM ports. Also shows notifications when a serial device is added/removed.
Maybe it could be useful for someone besides myself; possibly, I will upload it to SourceForge one day.
Popups do not work in KUbuntu; they seem to work in Mint Mate (GTK) on a PC, but the behaviour in a VM is not quite okay.

Once I know the correct repository, if you like I can either create a patch for you, or submit a pull request on your behalf?
If you mean me, I cannot handle git. It is @JurassicPorks repository. But the forum says he has not been around last two months.


You've got a file in there: NewSerialSelector.pas.  Does nothing but add .AppendFriendlyNames property to a TCustomComboBox
...
Thanks, I deleted both files with that name - nothing asked for them.
« Last Edit: December 03, 2025, 03:50:45 pm by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

CM630

  • Hero Member
  • *****
  • Posts: 1581
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #496 on: December 12, 2025, 01:54:55 pm »
Maybe someone could try this version on a real Mac (not worth trying on a normal computer - run the app in folder Test1 and start plugging and unplugging various serial devices to see what will happen). I also wonder if the Bluetooth device will be listed?

Also, it seems that wmic path Win32_PnPEntity where "Caption like '%%(COM%%)'" get Caption (in Windows DOS prompt) provides more reliable results than the currently used one, which gets the list of the device from the registry.
AFAIU wmic will be removed from the latest Windows versions, so Win32_PnPEntity shall be called directly. I think I have found a way to do it...
« Last Edit: December 12, 2025, 03:36:00 pm by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

rsmain

  • Newbie
  • Posts: 3
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #497 on: December 12, 2025, 04:23:18 pm »
Hello, I am new to Lazarus and try to migrate some applications from an older Delphi-version.
All my applications use serial ports and that is why I installed LazSerial 0.6 from the OPM.
I got stuck with GetSerialPortNames and I am not sure what is wrong.
I get an "Identifier not found"-error for GetSerialPortNames.

This is the stripped code for the function:

Please help

BR, Rene



unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, LazSerial;

type

  { TForm1 }

  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Label1: TLabel;
    LazSerial1: TLazSerial;
    procedure FormActivate(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;


implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormActivate(Sender: TObject);

var
  Ports: TStringList;
begin
  Ports := LazSerial.GetSerialPortNames;

end;

end. 
« Last Edit: December 12, 2025, 04:42:56 pm by rsmain »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6284
  • Compiler Developer
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #498 on: December 12, 2025, 08:21:35 pm »
This is the stripped code for the function:

Please use [code][/code]-tags to make your code better viewable and to avoid the forum software potentially interpreting your code.

tetrastes

  • Hero Member
  • *****
  • Posts: 734
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #499 on: December 12, 2025, 08:23:58 pm »
Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. uses lazsynaser;
  4.  
  5. {$R *.lfm}
  6.  
  7. { TForm1 }
  8.  
  9. procedure TForm1.FormActivate(Sender: TObject);
  10.  
  11. var
  12.   Ports: TStringList;
  13. begin
  14.   Ports.CommaText := GetSerialPortNames;
  15.  
  16. end;

CM630

  • Hero Member
  • *****
  • Posts: 1581
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #500 on: December 13, 2025, 07:55:15 am »
...
begin
  Ports := LazSerial.GetSerialPortNames;
...
When you right-click on GetSerialPortNames you will get a popup menu, which will take you to its definition. There you will see that it outputs a string, not a TStringlist, so you need to convert.
Also I think that GetSerialPortNames is located in LazSynaser, not in LazSerial (they are in the same package), so LazSerial.GetSerialPortNames; should not work.
What is your target OS? I doubt that the example in the previous post of @tetrastes will work on anything but Windows.

This should be a working multiplatform solution:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   StrUtils, LazSynaSer;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. {$IF FPC_FULLVERSION >= 30002}
  35. //Otional, used for sorting the list
  36. function NaturalSortCompare(List: TStringList; Index1, Index2: Integer): Integer;
  37. begin
  38.   Result := NaturalCompareText(List[Index1], List[Index2]);
  39. end;
  40. {$endif}
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. var
  44.   Ports: TStringList;
  45. begin
  46.   Memo1.Clear;
  47.   Ports := TStringList.Create;
  48.   Ports.CommaText := trim(GetSerialPortNames).Replace('  ',',',[rfReplaceAll]);
  49.   {$IF FPC_FULLVERSION >= 30002}Ports.CustomSort(@NaturalSortCompare);{$endif} //Sort the list, optional, might be done internally in later/other version of TLazSerial.
  50.   Memo1.Lines.Assign(Ports);
  51.   ports.free;
  52. end;
  53. end.
  54.  
  55.  


« Last Edit: December 13, 2025, 09:59:08 am by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

rsmain

  • Newbie
  • Posts: 3
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #501 on: December 13, 2025, 10:23:56 am »
Thank you CM630 und tetratses,

it becomes clearer now.

The reason for migration from old Delphi to Lazarus was future use on multi-platform (Windows, Linux, MacOs).
AI lead me to LazSerial as a component for the communication over serial ports.
AI also told me,that GetSerialPortNames is part of LazSerial, which is not true.
LazSynaser will not work on other than Windows.

As I need a function like GetSerialPortNames, both, LazSerial and LazSynaser will not work.

Let me instead put an open question here: Do you know any component for serial communication that works on various OS and provides such a function like GetSerialPortNames to fill a ComboBox for selection?

Thank you




tetrastes

  • Hero Member
  • *****
  • Posts: 734
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #502 on: December 13, 2025, 11:09:20 am »
I doubt that the example in the previous post of @tetrastes will work on anything but Windows.
It's worth to try before writing.

CM630

  • Hero Member
  • *****
  • Posts: 1581
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #503 on: December 13, 2025, 11:13:23 am »
...LazSynaser will not work on other than Windows.

As I need a function like GetSerialPortNames, both, LazSerial and LazSynaser will not work.
...
This is not true. Maybe Synaser does not work on Linux, but LazSynaser, which is a part of TLazSerial works.
TLazSerial 0.6 works on Windows and Linux (incl. RaspberryPI and probably other NonPCs). For MacOS it needs some very minor changes for GetSerialPortNames.
If you have a Mac, get TLazSerial 2025.12.12.7z  from my previous post (https://forum.lazarus.freepascal.org/index.php/topic,20481.msg572182.html#msg572182) and run the example from subfolder Test1.
It won't hurt if you try it on some other OS, too, but maybe some the other OSes were negatively affected by the MacOS fixes.

I doubt that the example in the previous post of @tetrastes will work on anything but Windows.
It's worth to try before writing.
That is why I used the verb doubt. And I definitely intend to try it.
« Last Edit: December 13, 2025, 11:16:33 am by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

hansotten

  • Full Member
  • ***
  • Posts: 106
    • The School of Wirth
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #504 on: December 13, 2025, 11:17:40 am »
Indeed, TlazSerial works fine on Linux and Windows.

Do not believe everything AI tells you! Check, read and investigate the source of your research.
http://pascal.hansotten.com/ Pascal for Small Machines. The School of Wirth, sources of old Pascal compilers,

tetrastes

  • Hero Member
  • *****
  • Posts: 734
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #505 on: December 13, 2025, 11:34:05 am »
...LazSynaser will not work on other than Windows.

As I need a function like GetSerialPortNames, both, LazSerial and LazSynaser will not work.
...
This is not true. Maybe Synaser does not work on Linux, but LazSynaser, which is a part of TLazSerial works.

Of course synaser works on Linux. Lazsynaser is slightly modified version of it, and it's the engine of all other LazSerial stuff.
In fact using synaser directly one can write more diverse and flexible code, and do things that cannot be done with LazSerial, at least easily.

rsmain

  • Newbie
  • Posts: 3
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #506 on: December 13, 2025, 12:11:34 pm »
Thank you for claryfing. Yes, I will be more careful with AI.
I now implemented the method CM630 proposed and it works on windows.
I will give it a try on the other OS later.
BR, Rene

bobihot

  • New Member
  • *
  • Posts: 44
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #507 on: December 13, 2025, 01:29:42 pm »
I was have problem with TLazSerial , what can`t to set own Baudrate. He accept only an old "standard". Which is already not very standard on Mbits speeds. In Synaser was not this problem. If possibly to "unlock" setup the transfer speed. Only like a remarks to be an old rates.
Thanks

CM630

  • Hero Member
  • *****
  • Posts: 1581
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #508 on: December 13, 2025, 02:20:24 pm »
I was have problem with TLazSerial , what can`t to set own Baudrate. He accept only an old "standard". Which is already not very standard on Mbits speeds. In Synaser was not this problem. If possibly to "unlock" setup the transfer speed. Only like a remarks to be an old rates.
Thanks
Recently I had to use baudrate 200 000, I do not remember having troubles with that. Maybe I just added the baudrate in
LazSerial.pas.
Code: Pascal  [Select][+][-]
  1. type
  2. {$IFDEF UNIX}
Indeed, maybe adding a Custom baudrate list should be fine?

EDIT: You can try the attached LPK as a proof of concept. A new property CustomBaudRate is added to TLazSerial, see the example in the Test1 subfolder.


I doubt that the example in the previous post of @tetrastes will work on anything but Windows.
It's worth to try before writing.
I tried it and works when .StrictDelimiter = False.


...
I now implemented the method CM630 proposed and it works on windows....
In Windows you might have troubles with the GetSerialPortNames with some devices (Prolific). As written a few posts above, a solution seems to be on the way.
« Last Edit: December 13, 2025, 10:20:58 pm by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

tetrastes

  • Hero Member
  • *****
  • Posts: 734
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #509 on: December 14, 2025, 09:57:52 pm »
I doubt that the example in the previous post of @tetrastes will work on anything but Windows.
It's worth to try before writing.
I tried it and works when .StrictDelimiter = False.
I see you silently changed your reply...  8)

StrictDelimiter = False by default, and I do not see that it is set to True in my example.

 

TinyPortal © 2005-2018