Lazarus

Programming => Packages and Libraries => Topic started by: michoux on June 19, 2020, 09:34:08 pm

Title: Laz Serial
Post by: michoux on June 19, 2020, 09:34:08 pm
Hello,

I am using Laz_serial to get data from com port.
I have following data from com port.

SOH STX DLE "MY DATA IN PACKET" EOT

So every packet is closed by EOT

I want to parse this packet to so I have each packet in own line.
I use example from laz serial:

*******************************************
  Str :=  Serial.ReadData;
  CurPos := Pos( Char(4) ,Str);
  if CurPos = 0 then begin
    FTempStr := FTempStr + Str;
  end
  else
  begin
    FTempStr := FTempStr + Copy( Str, 1, CurPos -1 );   

   Memo.Lines.BeginUpdate;
   Memo.Lines.Add(ftempstr);
    Memo.Lines.EndUpdate;
   Memo.SelStart := Length(Memo.Lines.Text)-1;
   Memo.SelLength:=0;
  FTempStr := Copy(Str,CurPos +1, Length(Str) - CurPos);     
end;   
*******************************************

But in many cases I see that in memo I do not get every packet in own line.
Any suggestions?
I am using com0com for virtual ports
Title: Re: Laz Serial
Post by: winni on June 19, 2020, 11:06:18 pm
Hi!

Please use the Code tags for your code - for better readability.

Your problem is memo1.lines.add - that creates a new line in the internal stringList .

Instead you should use

Code: Pascal  [Select][+][-]
  1. Memo1.Lines.Text := Memo1.Lines.Text + ftempstr ;

That should solve your problem.

Winni
Title: Re: Laz Serial
Post by: michoux on June 20, 2020, 12:59:39 am
Hi,

Thanks for the answer.
What  I did is
ftempstr:=serial.SynSer.RecvTerminated(1,char(4));
This gives me all the correct lines.

Not sure if I can ask here, but the chars I receive over serial are not correct one, I mean not in correct code.
all the latin chars (čćžž) are ?

Title: Re: Laz Serial
Post by: michoux on June 25, 2020, 09:05:55 pm
Hello,

I am still looking for a solution to get the correct chars from serial port.
My special chars(čćžđšž) are always received with ? - question mark.

Thanks a lot.
Title: Re: Laz Serial
Post by: lucamar on June 25, 2020, 09:19:28 pm
I am still looking for a solution to get the correct chars from serial port.
My special chars(čćžđšž) are always received with ? - question mark.

Use a RawByteString to read from the serial link and convert it to UTF8 with, for example, AnsiToUTF8(), or any of the more advanced conversion functions.
Title: Re: Laz Serial
Post by: Jurassic Pork on June 26, 2020, 02:13:14 am
hello,
you can also do this :
Code: Pascal  [Select][+][-]
  1. procedure TFMain.SerialRxData(Sender: TObject);
  2. var Str : string;
  3. var DLEpos : integer;
  4. begin
  5.   Str :=  Serial.ReadData;
  6.   CurPos := Pos( Char(4) ,Str);
  7.   if CurPos = 0 then begin
  8.     FTempStr := FTempStr + Str;
  9.   end
  10.   else begin
  11.     FTempStr := FTempStr + Copy( Str, 1, CurPos-1);
  12.     DLEpos := Pos(Char(16),FTempStr);
  13.     If DLEpos > 0 then begin
  14.        Memo.Lines.BeginUpdate;
  15.        Memo.Lines.Add(Copy(FtempStr,DLEpos + 1,Length(FtempStr)- DLEpos));
  16.        Memo.Lines.EndUpdate;
  17.        Memo.SelStart := Length(Memo.Lines.Text)-1;
  18.        Memo.SelLength:=0;
  19.        FTempStr := Copy(Str,CurPos +1, Length(Str) - CurPos);
  20.      end;
  21.    end;
  22. end;

generating serial data every 2 seconds with :
Code: Pascal  [Select][+][-]
  1. procedure TFMain.Timer1Timer(Sender: TObject);
  2. var
  3.   Str: String;
  4. begin
  5. // SOH STX DLE "MY DATA IN PACKET" EOT
  6. Str := Char(1) + Char(2) + Char(16) +
  7.        FormatDateTime('hhnnss".00,"',now ) +
  8.        '*héhé c''est écrit en français*' +
  9.        Char(4);
  10.    Serial.WriteData(Str);
  11. end  ;

result in attachment (click on attachment to see animation)

Friendly, J.P
TinyPortal © 2005-2018