Recent

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

Bebeto

  • Newbie
  • Posts: 2
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #450 on: July 24, 2024, 07:19:15 pm »
Jurassic Pork: I'm looking for CPortLib component, because it has LED objects that indicate port activity and, in my application, it's interesting to monitor them, graphically.

I tried to install the "3.2_laz" version, that you made available in another post, but I've compilation errors and cannot use this component.
(TFont conversions on CPortReg.pas, lines 191 and 193).

I'm using Lazarus 3.4 x64 with FPC: 3.2.2.

Do you have a more up-to-date and functional version of this CPorLib component?

I believe that TLazSerial is a non-graphical component, right?

I thank you for your attention!
Thanks!
« Last Edit: July 24, 2024, 08:50:16 pm by Bebeto »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1251
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #451 on: July 25, 2024, 01:21:50 am »
Hello,
try to use lazcomport from here
ok for me with Lazarus 2.2.6 fpc 3.2.2
Friendly, J.P
« Last Edit: July 25, 2024, 01:24:41 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Bebeto

  • Newbie
  • Posts: 2
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #452 on: July 25, 2024, 07:12:38 pm »
J.P: Thank you very much!  :D
This was exactly what I was looking for.
You are the man!  8-)

Have a good day!

sunjob

  • New Member
  • *
  • Posts: 29
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #453 on: September 01, 2024, 03:03:28 pm »
good day!
open/build/install package LazSerialPort.lpk, but can't find this on toolbar-pallete.
however, i open your example from the 'test'-subdirectory, F12 -> it is present on the form
thank

Code: Pascal  [Select][+][-]
  1. lazarus_3.4.0_opt_gtk2 x86_64
  2. lazarus_3.4.0_opt_qt4  x86_64
  3. lazarus_3.4.0_opt_qt5  x86_64
  4. fpc_3.2.2
  5. linux/slackware 14.2/15.2
« Last Edit: September 01, 2024, 03:05:51 pm by sunjob »

eldonfsr

  • Hero Member
  • *****
  • Posts: 526
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #454 on: September 25, 2024, 06:11:58 pm »
I create this function on app to handle configuration serial port, some time we need show information

Code: Pascal  [Select][+][-]
  1.   function StrToBaudRate(Str: String ): TBaudRate;
  2.     function StrToParity(Str: String ): TParity;
  3.     function StrtoDataBits(Str: String ): TDataBits;
  4.     function StrtoStopBits(Str: String ): TStopBits;
  5.     function StrToFlowControl(Str: String ): TFlowControl;
  6.  
  7.     function BaudRateToStr(BaudRate: TBaudRate): string;
  8.     function StopBitsToStr(StopBits: TStopBits): string;
  9.     function DataBitsToStr(DataBits: TDataBits): string;
  10.     function ParityToStr(Parity: TParity): string;
  11.     function FlowControlToStr(FlowControl: TFlowControl): string;
  12.  
  13.  
i Think this variables all ready on serial but i case we need to create on const
Code: Pascal  [Select][+][-]
  1. Var
  2.   Port        : String;
  3.   SBaudRate  : String;
  4.   SParity    : String;
  5.   DataBits   : string;
  6.   StopBits   : string;
  7.   SoftFlow   : Boolean;
  8.   hardFlow   : Boolean;
  9.   flowcontrol:String;  
  10.  
  11. const
  12.     BaudRateStrings: array[TBaudRate] of string = ('110', '300', '600',
  13.       '1200', '2400', '4800', '9600', '14400', '19200', '38400', '56000', '57600',
  14.       '115200', '128000', '230400', '256000','460800', '921600');
  15.     StopBitsStrings: array[TStopBits] of string = ('1', '1.5', '2');
  16.     DataBitsStrings: array[TDataBits] of string = ('8', '7', '6', '5');
  17.     ParityBitsStrings: array[TParity] of string = ('None', 'Odd', 'Even',
  18.       'Mark', 'Space');
  19.     FlowControlStrings: array[TFlowControl] of string = ('None',
  20.       'Software', 'HardWare');
  21.  

I Hope help
Code: Pascal  [Select][+][-]
  1. function TFormMain.BaudRateToStr(BaudRate: TBaudRate): string;
  2. begin
  3.   Result := BaudRateStrings[BaudRate];
  4. end;
  5.  
  6. function TFormMain.StrToBaudRate(Str: String): TBaudRate;
  7. var
  8.   I: TBaudRate;
  9. begin
  10.   I := Low(TBaudRate);
  11.   while (I <= High(TBaudRate)) do
  12.   begin
  13.     if UpperCase(Str) = UpperCase(BaudRateToStr(TBaudRate(I))) then
  14.       Break;
  15.     I := Succ(I);
  16.   end;
  17.   if I > High(TBaudRate) then
  18.     Result := Br__9600
  19.   else
  20.     Result := I;
  21. end;
  22. function TFormMain.StrToStopBits(Str: string): TStopBits;
  23. var
  24.   I: TStopBits;
  25. begin
  26.   I := Low(TStopBits);
  27.   while (I <= High(TStopBits)) do
  28.   begin
  29.     if UpperCase(Str) = UpperCase(StopBitsToStr(TStopBits(I))) then
  30.       Break;
  31.     I := Succ(I);
  32.   end;
  33.   if I > High(TStopBits) then
  34.     Result := sbOne
  35.   else
  36.     Result := I;
  37. end;
  38.  
  39. // string to data bits
  40. function TFormMain.StrToDataBits(Str: string): TDataBits;
  41. var
  42.   I: TDataBits;
  43. begin
  44.   I := Low(TDataBits);
  45.   while (I <= High(TDataBits)) do
  46.   begin
  47.     if UpperCase(Str) = UpperCase(DataBitsToStr(I)) then
  48.       Break;
  49.     I := Succ(I);
  50.   end;
  51.   if I > High(TDataBits) then
  52.     Result := db8bits
  53.   else
  54.     Result := I;
  55. end;
  56.  
  57. // string to parity
  58. function TFormMain.StrToParity(Str: string): TParity;
  59. var
  60.   I: TParity;
  61. begin
  62.   I := Low(TParity);
  63.   while (I <= High(TParity)) do
  64.   begin
  65.     if UpperCase(Str) = UpperCase(ParityToStr(I)) then
  66.       Break;
  67.     I := Succ(I);
  68.   end;
  69.   if I > High(TParity) then
  70.     Result := pNone
  71.   else
  72.     Result := I;
  73. end;
  74.  
  75. // string to flow control
  76. function TFormMain.StrToFlowControl(Str: string): TFlowControl;
  77. var
  78.   I: TFlowControl;
  79. begin
  80.   I := Low(TFlowControl);
  81.   while (I <= High(TFlowControl)) do
  82.   begin
  83.     if UpperCase(Str) = UpperCase(FlowControlToStr(I)) then
  84.       Break;
  85.     I := Succ(I);
  86.   end;
  87.   if I > High(TFlowControl) then
  88.     Result := fcNone
  89.   else
  90.     Result := I;
  91. end;
  92.  
  93. function TFormMain.StopBitsToStr(StopBits: TStopBits): string;
  94. begin
  95.   Result := StopBitsStrings[StopBits];
  96. end;
  97.  // data bits to string
  98. function TFormMain.DataBitsToStr(DataBits: TDataBits): string;
  99. begin
  100.   Result := DataBitsStrings[DataBits];
  101. end;
  102. function TFormMain.ParityToStr(Parity: TParity): string;
  103. begin
  104.   Result := ParityBitsStrings[Parity];
  105. end;
  106. // flow control to string
  107. function TFormMain.FlowControlToStr(FlowControl: TFlowControl): string;
  108. begin
  109.   Result := FlowControlStrings[FlowControl];
  110. end;
  111.  
  112.  


eldonfsr

  • Hero Member
  • *****
  • Posts: 526
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #455 on: September 26, 2024, 04:48:13 pm »
JP there is possible to add some exception for example when connect and is not possible to connect send a message error and can handle  .... or some like try..


Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1251
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #456 on: September 27, 2024, 05:33:02 am »
Hello,
JP there is possible to add some exception for example when connect and is not possible to connect send a message error and can handle  .... or some like try..
When you try to open a port, if it is not possible you have an error message (lattachment):
Code: Pascal  [Select][+][-]
  1. procedure TLazSerial.DeviceOpen;
  2. begin
  3.   FSynSer.Connect(FDevice);
  4.   if FSynSer.Handle=INVALID_HANDLE_VALUE then
  5.     raise Exception.Create('Could not open device '+ FSynSer.Device);
  6.  
  7.   FSynSer.Config(ConstsBaud[FBaudRate],
  8.                  ConstsBits[FDataBits],
  9.                  ConstsParity[FParity],
  10.                  ConstsStopBits[FStopBits],
  11.                  FSoftflow, FHardflow);
  12.  
  13.   // Launch Thread
  14.   ReadThread := TComPortReadThread.Create(true);
  15.   ReadThread.Owner := Self;
  16.   ReadThread.MustDie := false;
  17. //  ReadThread.Resume;   --> deprecated
  18.   ReadThread.Start;
  19. end;  
     
if you want another thing , try to modify tlazserial or create your own module with synaser for example.         

Friendly  J.P
« Last Edit: September 27, 2024, 05:39:17 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

eldonfsr

  • Hero Member
  • *****
  • Posts: 526
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #457 on: October 03, 2024, 03:30:17 am »
Hi I tried to add functions StopBitstoString but i don't get works can you help me how i can implement this
function is working but not part of lazserial i define all values on my app but i tried to implement on tlazserial the way we can save configuration of modem on ini file and restore setting from there to
return number of 0 not SbOne


Code: Pascal  [Select][+][-]
  1. procedure TLazSerial.LoadSettings(Const NameFile:String);
  2. Var IniF:Tinifile;
  3. begin
  4.   if( (NameFile<>'') and (FileExists(NameFile))) then begin
  5.     Inif:= TInifile.Create(NameFile);
  6.     FDevice  := iniF.ReadString('Serial','Port','COM1') ;
  7.     FBaudRate:= StrtoBaudRate(iniF.ReadString('Serial','BaudRate','br_110'));
  8.     FDataBits:= StrToDataBits(iniF.ReadString('Serial','DataBits','8') );
  9.     FStopBits:= StrToStopBits(iniF.ReadString('Serial','StopBits','1') );
  10.     FParity  := StrToParity(iniF.ReadString('Serial','Parity','pNone'));
  11.     FFlowControl:= StrToFlowControl(iniF.ReadString('Serial','FlowControl','fcNone'));
  12.   end else begin
  13.     Inif:= TInifile.Create(GetCurrentDir+'\Serial.ini');
  14.   end;
  15. end;
  16.  
  17. procedure TLazSerial.SaveSettings(Const NameFile:String);
  18. Var IniF:Tinifile;
  19. begin
  20.   if( NameFile<>'' ) then begin
  21.     Inif:= TInifile.Create(NameFile);
  22.   end else begin
  23.     Inif:= TInifile.Create('Serial.ini');
  24.   end;
  25.   iniF.Writestring('Serial','Port',FDevice) ;
  26.   iniF.WriteString('Serial','BaudRate',BaudRateToSTr(FBaudRate));
  27.   iniF.WriteString('Serial','DataBits',DataBitsToSTr(FDataBits)) ;
  28.   iniF.WriteString('Serial','StopBits',StopBitsToStr(fStopBits));
  29.   IniF.writeString('Serial','Parity',ParityToSTr(FParity));
  30.   iniF.WriteString('Serial','FlowControl',FlowControlToSTr(FFlowControl));
  31. end;
  32.  
  33.  
  34.  
  35. function TLazSerial.StopBitsToStr(SB: TStopBits): string;
  36. Var Value:Integer;
  37. begin
  38.   Result := IntToStr(ConstsStopBits[StopBits);
  39. end;    
  40.  
  41.  
 

PeterX

  • Sr. Member
  • ****
  • Posts: 422
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #458 on: November 14, 2024, 11:33:03 am »
Since about a week I use TSerialPort.

Some users do use my application with USR-VCOM  (a virtual SerialPort emulation to Ethernet)
to connect to a SerialToEthernet device on the other end of the TCP connection -  such as Waveshare 16529 = RS485 To ETH.
The connection is configured to 9600, 8, 1, according to the machine's needs, connected with RS485.

Whyever, when opening my application, the Connection changes due to activated RFC2217 - see screenshot.
Looks like it has switched from 8 Bit to 7 bit. ???

In the WaveShare device web interface I can deactivate RFC2217, also in the USR-VCOM software.
But some devices "USB to RS485" do not have such a Checkbox to disable RFC2217 ..


Can TSerialPort anyhow disable RFC2217 ?

I do not understand "who" generates this "RFC2217 function byte".
Is is Windows itself ?
Or the USR-VCOM software ?
The "USB to RS485" device driver ?
« Last Edit: November 14, 2024, 11:41:29 am by PeterX »
usually using latest Lazarus release version with Windows 10

mas steindorff

  • Hero Member
  • *****
  • Posts: 551
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #459 on: November 14, 2024, 08:57:18 pm »
I have not used a RFC2217 or an ethernet based VCP but with other VCP (USB and BT bridges), windows will assign it to a new port each time it gets "installed" unless something in the device give windows an ID or serial number so it knows it can reuse it's last port.  This can be very troubling after a while requiring you to go into the registry to clear out the old connections. 
Also, if the "install" process fires while the port is connected, windows will also assign a new port :(
make sure your code closes the port when it's not being used to avoid this one.
You define the 7 or 8 bit on open. this is passed to the driver. if you see this change, perhaps the driver has another client you are fighting with.
I still use TLasSerial's  predecessor synaser which has a GetSerialPortNames().  I can then use these names to zero in on the current port the device is active on.
Hoping something here helps - MAS
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

PeterX

  • Sr. Member
  • ****
  • Posts: 422
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #460 on: November 15, 2024, 12:15:41 am »
Not enough emojis in this Forum Software to be able to reply in an appropriate way ..

And not able to properly integrate my PIC for Your answer ..
But look ..  8)
usually using latest Lazarus release version with Windows 10

CM630

  • Hero Member
  • *****
  • Posts: 1195
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #461 on: November 21, 2024, 01:22:18 pm »
I have an issue with FlowControl.
When trying to retrieve data from a multimeter, it occurs that the FlowControl setting shall be

   09 00 00 00 40 00 00 00 00 08 00 00 00 02 00 00
and more precisely FlowReplace shall be equal to 00.

But if I set my COM port the following way:
Code: Pascal  [Select][+][-]
  1.     LazSerial1.DataBits := TDataBits.db7bits;
  2.     LazSerial1.StopBits := TStopBits.sbTwo;
  3.     LazSerial1.Parity := TParity.pOdd;
  4.     LazSerial1.FlowControl := TFlowControl.fcHardware;  
my app sets FlowControl to
  09 00 00 00 80 00 00 00 00 08 00 00 00 02 00 00.

I found a workaround with which the multimeter sends data to my appl: in LazSynaSer , procedure TBlockSerial.Config(baud, bits: integer; parity: char; stop: integer;  softflow, hardflow: boolean);  I changed


Code: Pascal  [Select][+][-]
  1.   if hardflow then
  2.     dcb.Flags := dcb.Flags or dcb_OutxCtsFlow or dcb_RtsControlHandshake

to

Code: Pascal  [Select][+][-]
  1.   if hardflow then
  2.     dcb.Flags := dcb.Flags or dcb_OutxCtsFlow or $00000000


But is it possible to make it with the currently present setting of TLazSerial?

Also I noticed that:
  1. Termite 3.4 has the following settings for Flow Control:           None; RTS/CTS; DTR/DSR; XON/XOFF (multimeter sends data to it when
DTR/DSR is selected)
  2. RealTerm 2.0.0.70 has the following settings for Flow Control: None; RTS/CTS; DTR/DSR; RS485-rts (multimeter sends data to it when RS485-rts is selected)
3. LabView has the following settings for Flow Control: None; XON/XOFF; RTS/CTS; XON/XOFF & RTS/CTS; DTR/DSR; XON/XOFF & DTR/DSR

  4.TLazSerial has 3 settings only: fcNone,fcXonXoff,fcHardware

« Last Edit: November 22, 2024, 11:49:07 am by CM630 »
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

CM630

  • Hero Member
  • *****
  • Posts: 1195
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #462 on: December 04, 2024, 10:50:21 am »
I have issues with another device, so I started looking the code again.
This is from LazSynaSer:


Code: Pascal  [Select][+][-]
  1. procedure TBlockSerial.Config(baud, bits: integer; parity: char; stop: integer;
  2.   softflow, hardflow: boolean);
  3. begin
  4.   FillChar(dcb, SizeOf(dcb), 0);
  5.   GetCommState;
  6.   dcb.DCBlength := SizeOf(dcb);
  7.   dcb.BaudRate := baud;
  8.   dcb.ByteSize := bits;
  9.   case parity of
  10.     'N', 'n': dcb.parity := 0;
  11.     'O', 'o': dcb.parity := 1;
  12.     'E', 'e': dcb.parity := 2;
  13.     'M', 'm': dcb.parity := 3;
  14.     'S', 's': dcb.parity := 4;
  15.   end;
  16.   dcb.StopBits := stop;
  17.   dcb.XonChar := #17;
  18.   dcb.XoffChar := #19;
  19.   dcb.XonLim := FRecvBuffer div 4;
  20.   dcb.XoffLim := FRecvBuffer div 4;
  21.   dcb.Flags := dcb_Binary;
  22.   if softflow then
  23.     dcb.Flags := dcb.Flags or dcb_OutX or dcb_InX;
  24.   if hardflow then
  25.     dcb.Flags := dcb.Flags or dcb_OutxCtsFlow or dcb_RtsControlHandshake
  26.   else
  27.     dcb.Flags := dcb.Flags or dcb_RtsControlEnable;
  28.   dcb.Flags := dcb.Flags or dcb_DtrControlEnable;
  29.   if dcb.Parity > 0 then
  30.     dcb.Flags := dcb.Flags or dcb_ParityCheck;
  31.   SetCommState;
  32. end;





Is it sure that dcb.Flags := dcb.Flags or dcb_DtrControlEnable; is in the right place?
No matter what handshaking is chosen dcb_DtrControlEnable is enabled  :o
« Last Edit: December 04, 2024, 11:52:32 am by CM630 »
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

tetrastes

  • Hero Member
  • *****
  • Posts: 594
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #463 on: December 04, 2024, 02:15:26 pm »
This code sets so called RTS/CTS handshaking hardware flow control, so there is no matter what is DTR signal. If you need to turn it off, just use
Code: Pascal  [Select][+][-]
  1. port.DTR := false;
after port.Config().

If you need DTR/DSR handshaking, then f.e.
Code: Pascal  [Select][+][-]
  1.         port.DTR := false;    // just in case
  2.         port.GetCommState;
  3.         port.dcb.Flags := port.dcb.Flags and not (dcb_OutxCtsFlow or dcb_RtsControlHandshake);    // if RTS/CTS handshaking was enabled and you don't need it,
  4.                                                                                                   // note that there are cases when both RTS/CTS and DTR/DSR flow controls are used
  5.         port.dcb.Flags := port.dcb.Flags and not (dcb_DtrControlEnable or dcb_DsrSensivity);
  6.         port.dcb.Flags := port.dcb.Flags or dcb_OutxDsrFlow or dcb_DtrControlHandshake;
  7.         port.SetCommState;
« Last Edit: December 04, 2024, 02:36:53 pm by tetrastes »

CM630

  • Hero Member
  • *****
  • Posts: 1195
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #464 on: December 05, 2024, 09:16:23 am »
Thanks!
With your help I suceeded to make it work.
Here is the part of the code:

Code: Pascal  [Select][+][-]
  1.     LazSerial1.FlowControl := fcHardware;
  2.  
  3.     LazSerial1.Active := True;
  4.     LazSerial1.SynSer.DCB.flags := LazSerial1.SynSer.DCB.flags and not dcb_RtsControlHandshake;
  5.     LazSerial1.SynSer.SetCommState;  
  6.  
Actually, I am not sure if LazSerial1.FlowControl := fcHardware; is usable at all, it seems to me it does too many things that it should not do.
Sincerely, I know nothing about Serial communication so I might be wrong, but I have compared the behaviour of several other serial terminals and they do not behave this way.
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018