Hi - I'm afraid that I'm back with an old problem. I need to use two tLazSerial components in one program.
One speaks to an Arduino Mega on COM4 at 115200 Baud. The Arduino replies with the output from a laser. A question to the Arduino might be: @E121! The response would be 112 numbers and letters in a fromatted string.
The other tLazSerial communicates with a USB RS485 converter that connects to Alicat mass flow controllers. It runs at 9600 Baud on COM6. A question sent to this might be 'A'+#13. It will reply with a string of numbers and a gas identifier.
All other comms parameters are as usual - 8 databits, no parity, 1 stop bit etc.
The problem is that I cannot use both serial components; only one will work at a time and they will quickly lock up if I try to run both.
Here is my test code with one serial stream commented out and the tLazSerial component deleted. The timer is set to tick every 2 seconds.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
LazSerial;
type
{ TForm1 }
TForm1 = class(TForm)
LazSerial2: TLazSerial;
Memo1: TMemo;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
// lazSerial1.Open;
// lazSerial1.Active:= true ;
lazSerial2.Open;
lazSerial2.Active:= true ;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
// lazSerial1.WriteData('@E121!');
// while lazSerial1.DataAvailable do
// memo1.Lines.add (lazSerial1.ReadData) ;
lazSerial2.WriteData('A' + #13 );
while lazSerial2.DataAvailable do
memo1.Lines.add (lazSerial2.ReadData) ;
end;
end.
In this case on the Alicats are being talked to. If I replace lazSerial1 and uncomment the code, it stops working.
I've tried numerous variants, with separate timers for each component and with time gaps between the calls, but the outcome is the same.
Any suggestions would be gratefully received before I resort to running two programs and IPC.
Thanks