Recent

Author Topic: Laz Serial  (Read 1894 times)

michoux

  • Full Member
  • ***
  • Posts: 112
Laz Serial
« 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
« Last Edit: June 19, 2020, 09:39:25 pm by michoux »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Laz Serial
« Reply #1 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

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Laz Serial
« Reply #2 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 ?


michoux

  • Full Member
  • ***
  • Posts: 112
Re: Laz Serial
« Reply #3 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.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Laz Serial
« Reply #4 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Laz Serial
« Reply #5 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
« Last Edit: June 26, 2020, 02:18:23 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018