Recent

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

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #315 on: July 29, 2020, 02:14:47 am »
hello,
you cant'use events with TLazserial if your project isn't a GUI project (in GUI projects you have an event loop).
For synaser have a look here
Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

brochagdl

  • Newbie
  • Posts: 1
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #316 on: August 30, 2020, 07:34:47 pm »
Hello J.P., i make changes in your component for work in macos.

Do you want this changes?

hello,
Strange !   :-\  because cport.pas belongs to the comport library not lazserial.

Friendly, J.P

Hello esvignolo, can you share this modification?, I'm looking this for MacOS. Thanks.

MarkT1993

  • Newbie
  • Posts: 2
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #317 on: September 17, 2020, 08:24:33 am »
Hi
I am using TLazSerial V0.3 to transfer data to and from a HAAS milling terminal,
The transfer to the terminal is brilliant works great(really quick transfer too) as below:

LS is the name of my TLazserial component on the form,
txprog is just a simple memo box with 3600 lines.

Code: Pascal  [Select][+][-]
  1.   LS.Device := 'COM1';
  2.   LS.BaudRate := TBaudRate.br115200;
  3.   LS.DataBits := TDataBits.db7bits;
  4.   LS.FlowControl := TFlowControl.fcXonXoff;
  5.   LS.StopBits := TStopBits.sbOne;
  6.   LS.Parity := TParity.pEven;
  7.   LS.Active := True;
  8. For a := 0 To txprog.Lines.Count Do
  9.    Begin
  10.     b := txProg.Lines[a] + #13 + #10;
  11.     LS.WriteData(b);
  12.     If (a >= txprog.Lines.Count - 3) Then
  13.      Begin
  14.       Sleep(100);
  15.      End;
  16.    End;
  17.   LS.Active := False;

But I cant get the return from the terminal using the below

Code: Pascal  [Select][+][-]
  1. Var
  2.   a: Boolean;
  3.   b: String;
  4.   C: Integer;
  5. Begin
  6.   LS.Device := 'COM1';
  7.   LS.BaudRate := TBaudRate.br115200;
  8.   LS.DataBits := TDataBits.db7bits;
  9.   LS.FlowControl := TFlowControl.fcXonXoff;
  10.   LS.StopBits := TStopBits.sbOne;
  11.   LS.Parity := TParity.pEven;
  12.   LS.Active := True;
  13.   a:= true;
  14.   While (a = True) Do
  15.    Begin
  16.     If (B = '%') Then
  17.      Begin
  18.       C := C + 1;
  19.      End;
  20.     If (C = 2) Then
  21.      Begin
  22.       A := False;
  23.      End;
  24.     B := LS.ReadData();
  25.     txprog.Text := txprog.Text + B +#13+#10;
  26.    End;
  27.    LS.Close();

I know that the terminal and cables are working because I made a similar (albeit much slower) program in visual C# and the transfer and return come up.
Any thoughts?

(EDITED to  add code tags - please see Using the Forum)
« Last Edit: September 17, 2020, 11:32:04 am by trev »

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #318 on: September 17, 2020, 09:22:06 am »
It looks like you have not assigned the OnRxData event handler...
AFAIK LazSerial is an event driven component so the read should be done inside the OnRxData event. It is fired when the read thread in the component has detected data available on the serial line and is notifying you that it is time to read.
I don't know if it is possible to switch off the internal read thread in LazSerial but I believe not.
If you want to read yourself I think you need at least CheckSynchronize inside your own loop (or maybe Application.ProcessMessages if it is a GUI app).

PS: You should use code tags on your source code to make it better readable...
Select the code type pascal and it would look like this:
Code: Pascal  [Select][+][-]
  1. Var
  2.   a: Boolean;
  3.   b: String;
  4.   C: Integer;
  5. Begin
  6. LS.Device := 'COM1';
  7.   LS.BaudRate := TBaudRate.br115200;
  8.   LS.DataBits := TDataBits.db7bits;
  9.   LS.FlowControl := TFlowControl.fcXonXoff;
  10.   LS.StopBits := TStopBits.sbOne;
  11.   LS.Parity := TParity.pEven;
  12.   LS.Active := True;
  13.   a:= true;
  14.   While (a = True) Do
  15.    Begin
  16.     If (B = '%') Then
  17.      Begin
  18.       C := C + 1;
  19.      End;
  20.     If (C = 2) Then
  21.      Begin
  22.       A := False;
  23.      End;
  24.     B := LS.ReadData();
  25.     txprog.Text := txprog.Text + B +#13+#10;
  26.    End;
  27.    LS.Close();
  28.  
« Last Edit: September 17, 2020, 09:24:42 am by BosseB »
--
Bo Berglund
Sweden

MarkT1993

  • Newbie
  • Posts: 2
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #319 on: September 22, 2020, 01:54:37 pm »
Brilliant thanks BosseB thats sorted it now.
Thanks for the tip on Code tags too.

TopCat

  • New member
  • *
  • Posts: 9
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #320 on: January 09, 2021, 05:54:53 pm »
I would appreciate help understanding how to install the TLazSerial components under newly installed Lazarus 2.0.0 running on Raspbian Pi OS. I have successfully downloaded the TLazSerial files. I have edited LazSynaser.pas to comment out the UNIX specific baud rates and changed the value of MaxRates from 30 to 19 to avoid compile errors. When I attempt to Install the package I get the message "The package 'LazSerialPort-0.2.pas' was marked for installation. Currently Lazarus only supports static linked packages. The real installation needs rebuilding and restartng of Lazarus.". Attempts to rebuildLazarus are met with a dialog showing ""Cannot find fpdebug used by Lazarus".
Apologies if this is an issue with Lazarus rather than LazSerial but I'd be very grateful for any guidance members can offer to a Linux / Lazarus newbie.

hansotten

  • Jr. Member
  • **
  • Posts: 88
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #321 on: January 09, 2021, 07:12:25 pm »
I would appreciate help understanding how to install the TLazSerial components under newly installed Lazarus 2.0.0 running on Raspbian Pi OS. I have successfully downloaded the TLazSerial files. I have edited LazSynaser.pas to comment out the UNIX specific baud rates and changed the value of MaxRates from 30 to 19 to avoid compile errors. When I attempt to Install the package I get the message "The package 'LazSerialPort-0.2.pas' was marked for installation. Currently Lazarus only supports static linked packages. The real installation needs rebuilding and restartng of Lazarus.". Attempts to rebuildLazarus are met with a dialog showing ""Cannot find fpdebug used by Lazarus".
Apologies if this is an issue with Lazarus rather than LazSerial but I'd be very grateful for any guidance members can offer to a Linux / Lazarus newbie.
Use the Pacakage manager, no need to update.

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #322 on: January 09, 2021, 11:57:52 pm »
I would appreciate help understanding how to install the TLazSerial components under newly installed Lazarus 2.0.0 running on Raspbian Pi OS.
How come you are installing such an old version of Lazarus?
It is at version 2.0.10 now...
Quote
I have successfully downloaded the TLazSerial files.
How did you "download the files"?
What you should do is to use the OnLine PackageManager available as menu Package/OnLine Package Manager.
Here you will find the LazSerial package, just select it and use the Install button on the bottom and it will work right away.

As always with these GUI style components you need to "install" them into Lazarus, which means that Lazarus is rebuilt in the process. Fully normal...
--
Bo Berglund
Sweden

TopCat

  • New member
  • *
  • Posts: 9
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #323 on: January 10, 2021, 12:14:58 pm »
Many thanks for the feedback. I downloaded the TLazSerial zip file pointed to from https://forum.lazarus.freepascal.org/index.php?topic=20481.0 and (almost) followed the instructions. Thanks Bo for highlighting the menu items "Package / Online Package Manager. That part worked up to the point where the recompile failed with "Cannot find fpdebug used by Lazarus". I sense that is a different problem and unrelated to to TLazSerial. The old versions of  fpc and Lazarus are the versions installed by Raspbian OS using Preferences | Add / Remove programs or sudo apt-get install fpc and sudo apt-get install lazarus.
Seems I need to work out how to install the current versions of fpc and lazarus before attempting to install LazSerial again. That will be a new learning opportunity for me.
Many thanks for you help and understanding.

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #324 on: January 10, 2021, 03:32:07 pm »
Thanks Bo for highlighting the menu items "Package / Online Package Manager.
That part worked up to the point where the recompile failed with "Cannot find fpdebug used by Lazarus".
I sense that is a different problem and unrelated to to TLazSerial.
The old versions of  fpc and Lazarus are the versions installed by Raspbian OS using Preferences | Add / Remove programs or sudo apt-get install fpc and sudo apt-get install lazarus.
Seems I need to work out how to install the current versions of fpc and lazarus before attempting to install LazSerial again.
That will be a new learning opportunity for me.
Many thanks for you help and understanding.
I have created a blogpost on my server where I describe how to install Fpc/Lazarus on Raspberry Pi using sources from Subversion.
I have scripts there, which I always use when installing on new devices.
Please have a look!
--
Bo Berglund
Sweden

TopCat

  • New member
  • *
  • Posts: 9
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #325 on: January 10, 2021, 03:51:52 pm »
In case it helps others, I've solved my problem and successfully installed TLazSerial in Lazarus 2.0.10 running under Raspbian OS.
I used Raspbian Preferences | Add / Remove programs to remove the previous 2.0.0 version installed by Rasbian OS by default. I kept the Raspbian installed 3.0.4 version of fpc (Free Pascal Compiler). I then followed the instructions at <https://wiki.freepascal.org/Installing_Lazarus_on_Linux#Build_Lazarus_from_Source> to install Lazarus 2.0.10.
I had to use the Update option on a dialog when trying to run Lazarus as it seems Raspbian had not deleted the previous version of Lazarus (This is probably down to me not getting the uninstall correct).
From the Lazarus main menu I used Package |  Online Package Manager to install LazSerial. This time, after a successful installation of LazSerial, Lazarus did a successful compile to install LazSerial into the IDE.
Next task is to do a clean install of Raspbian OS and install current versions of fpc, Lazarus and LazSerial to avoid any potentially remaining issues.
MANY THANKS to previous contributors who helped get me here!
Should it be useful to others, here is the code I used to test that I had successfully installed LazSerial. It uses two buttons, a memo component and the all important TLazSerial component to read a device.
+++++++++++++++++++++++++++++++++++++
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, LazSerial, Forms, Controls, Graphics, Dialogs, StdCtrls
  , LazSynaSer;
type
  { TForm1 }
  TForm1 = class(TForm)
    btnOpen: TButton;
    btnClose: TButton;
    LazSerial1: TLazSerial;
    Memo1: TMemo;
    procedure btnCloseClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure LazSerial1RxData(Sender: TObject);
  privateremaining
  public
  end;
var
  Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.btnOpenClick(Sender: TObject);
Begin
  LazSerial1.ShowSetupDialog;
  LazSerial1.Open;
  Memo1.Clear;
end;
procedure TForm1.btnCloseClick(Sender: TObject);
begin
  LazSerial1.Close;
  Close;
end;
procedure TForm1.LazSerial1RxData(Sender: TObject);
begin
  Memo1.lines.Add(LazSerial1.ReadData);
end;
end.

TopCat

  • New member
  • *
  • Posts: 9
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #326 on: January 10, 2021, 04:00:22 pm »
BosseB,
I've just seen your latest reply. It looks to be very helpful. I'll try that with a fresh install of Raspbian OS, though using scripts will be a new experience for me. Life is full of opportunities ;-).
Thanks again for your support.

CJ

  • New Member
  • *
  • Posts: 10
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #327 on: February 17, 2021, 01:37:54 pm »
Hi, I am porting a working PCDIMMER Delphi program to Lazarus in particular replacing the ComDrv32 comportdriver with LazSerial. I used the instruction of TopCat in his post. Thanks for sharing your experience.
TopCat
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #325 on: January 10, 2021, 03:51:52 pm »

LazSerial is now in the Lazarus IDE.
compilation of PCDIMMER.PAS at  PCDIMMER.PAS(241,46) Error: Duplicate identifier "Action". I don’t see something wrong in the code and this module was giving no problems in the past. It is a mystery to me. I have included the erroneous Lazarus version.
Many thanks in advance,
Chris

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #328 on: February 17, 2021, 02:15:13 pm »
hello,
try to change the name of your variable for the tCloseAction argument. For example :
Code: Pascal  [Select][+][-]
  1.  procedure FormClose(Sender: TObject; var ActionClose: TCloseAction);

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

CJ

  • New Member
  • *
  • Posts: 10
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #329 on: February 17, 2021, 05:43:29 pm »
Hi JP. the correction makes compilation go a bit further. I am now replacing the calls from ComDrv32 into LazSerial ones. The first try: replace comport.senddata(@b,1);  into comport.WriteBuffer(@b,1); gives already troubles. The compiler complains with: PCDIMMER.PAS(322,33) Error: Can't assign values to an address. I am new in Pascal but I thought that @b means “the address of byte b” and that is a pointer. The WriteBuffer is declared as : function WriteBuffer(var buf; size: integer): integer; I don’t see the problem but I don’t understand the meaning of Pointer(@buf) in the body of the function. What does it means?


Here the ComDrv32 function
function TCommPortDriver.SendData( DataPtr: pointer; DataSize: dword ): dword;
var nToSend, nsent,t1: dword;
//    t1: longint;
   // dd:TdateTIME;
begin
  // DD:=DATE;
 //   IF DD>STRTODATE('99-07-01') THEN SHOWMESSAGE('ASDFASDFASDFSADFASDF');///


  { 0 bytes sent }
  Result := 0;
  { Do nothing if not connected }
  if not Connected then
    exit;
  { Current time }
  t1 := GetTickCount;
  { Loop until all data sent or timeout occurred }
  while DataSize > 0 do
  begin
    { Get output buffer free space }
    nToSend := OutFreeSpace;
    { If output buffer has some free space... }
    if nToSend > 0 then
    begin
      { Don't send more bytes than we actually have to send }
      if nToSend > DataSize then
        nToSend := DataSize;
      { Send }
      WriteFile( FComPortHandle, DataPtr^, DataSize, nsent, nil );
      { Update number of bytes sent }
      Result := Result + abs(nsent);
      { Decrease the count of bytes to send }
      DataSize := DataSize - abs(nsent);
      { Get current time }
      t1 := GetTickCount;
      { Continue. This skips the time check below (don't stop
        trasmitting if the FOutputTimeout is set too low) }
      continue;
    end;
    { Buffer is full. If we are waiting too long then
      invert the number of bytes sent and exit }
    if (GetTickCount-t1) > FOutputTimeout then
    begin
      Result := -Result;
      exit;
    end;
  end;
end;

Here the TLazSerial function
function TLazSerial.WriteBuffer(var buf; size: integer): integer;
begin
//  if FSynSer.Handle=INVALID_HANDLE_VALUE then
 //   ComException('can not write to a closed port.');
  result:= FSynSer.SendBuffer(Pointer(@buf), size);
end;

Is there an example of code available to learn more about the use of LazSerial?
The program itself is the user interface of a 16-port dimmer steered by a serial link. It is not a complicated thing, it needs to open, close the port and send data to it.
Thanks for the help,
CJ


 

TinyPortal © 2005-2018