Recent

Author Topic: LazSerial splits incoming string at 32 bytes  (Read 2928 times)

OldAndTired

  • New member
  • *
  • Posts: 9
LazSerial splits incoming string at 32 bytes
« on: September 02, 2021, 10:23:55 am »
[Edit: changed title]
Hi

I'm sure this is something simple. I send a query over serial and get a reply string which is 51 bytes long terminated with #13.

All works fine in a terminal program, but LazSerial is splitting into two packets of 32 + 19 bytes. I can't see any way of changing the component properties, so what's the best way around it?

This is Win 10 x64 by the way.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Ser.Open;
  4. end;
  5.  
  6. procedure TForm1.Button2Click(Sender: TObject);
  7. begin
  8.   Ser.WriteData(':,1'+#13);
  9. end;
  10.  
  11. procedure TForm1.SerRxData(Sender: TObject);
  12. var
  13.   s: string;
  14. begin
  15.   s :=  Ser.ReadData;
  16.   memo1.Lines.add(IntToStr(length(s))+' '+s);
  17. end;
  18.  
Thanks
« Last Edit: September 02, 2021, 10:42:03 am by OldAndTired »

ccrause

  • Hero Member
  • *****
  • Posts: 849
Re: LazSerial splits incoming string at 32 bytes
« Reply #1 on: September 02, 2021, 11:25:07 am »
All works fine in a terminal program, but LazSerial is splitting into two packets of 32 + 19 bytes. I can't see any way of changing the component properties, so what's the best way around it?
There is no underlying protocol to ensure what gets sent as a contiguous packet of bytes is reported as such in user space.  You as consumer of the data stream will have to assemble the fragments (at least the order of the bytes should be preserved) into whatever format you want.

In your case the packet is terminated by #13, so add received data into your own buffer until you receive the terminating #13. Then take this user buffer, add it to the memo as a line and clear the user buffer.

Edit: Refer to this post for a similar example.
« Last Edit: September 02, 2021, 11:35:00 am by ccrause »

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: LazSerial splits incoming string at 32 bytes
« Reply #2 on: September 02, 2021, 12:01:08 pm »
You initiate a data stream by sending the string 
Code: Pascal  [Select][+][-]
  1. Ser.WriteData(':,1'+#13);
  2.  

In this case you should wait for 32 bytes in the serial port buffer. After the buffer filled by 32 bytes, read the data.
The most simple way of implementation is adding code to TTimer.OnTimer event.
I never use TLazSerial(as I know it use TBlockSerial from SynaSer library)

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.Timer1Timer(Sender: TObject);
  2. begin
  3.   memo1.Lines.add(BlockSerial.RecvTerminated(10, #13));
  4. end;              
  5.  

 
Other way is a creation of TTheard to read data in the loop.
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

OldAndTired

  • New member
  • *
  • Posts: 9
Re: LazSerial splits incoming string at 32 bytes
« Reply #3 on: September 02, 2021, 12:51:04 pm »
OK, so just to clarify - this component does not have any form of line mode? (i.e. receive data until #13 or #10 then trigger an event to read the data)

Yes I can assemble the data that's no problem, I guess I was misled by the fact there are properties like RcvLineCRLF and LineBuffer in the object inspector.

ccrause

  • Hero Member
  • *****
  • Posts: 849
Re: LazSerial splits incoming string at 32 bytes
« Reply #4 on: September 02, 2021, 01:02:13 pm »
OK, so just to clarify - this component does not have any form of line mode?
I am not familiar with this specific component.  A quick view of the component's source should show you what functionality is available.  One way to jump to the source of LazSerial is to press Ctrl and left click with the mouse on the Ser.ReadData method in your code.  Or place the cursor inside ReadData and press Alt and up arrow.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: LazSerial splits incoming string at 32 bytes
« Reply #5 on: September 02, 2021, 02:00:06 pm »
hello,
you can try the property RcvLineCRLF

Quote
property FRcvLineCRLF : if this property is true, you use RecvString in place of RecvPacket when you read data from the port.
RecvString.
This method waits until a terminated data string is received. The string is terminated by a CR/LF sequence. The resulting string is returned without the terminator (CR/LF)! If no data is received within the Timeout (in milliseconds) period, LastError is set to ErrTimeout.

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

OldAndTired

  • New member
  • *
  • Posts: 9
Re: [SOLVED] LazSerial splits incoming string at 32 bytes
« Reply #6 on: September 02, 2021, 02:09:57 pm »
Thanks, looking the code gave me enough a nudge to get it working
Code: Pascal  [Select][+][-]
  1.   if FRcvLineCRLF then
  2.   result:=FSynSer.RecvString(0)
  3.   else
  4.   result:=FSynSer.RecvPacket(0);

So I checked the RcvLineCRLF option and modified the incoming data to provide both CR/LF and it's working just fine now.

(Thanks JP also, you posted just between me getting it working & typing this post. Nice to see the author active, and thanks for the component)

 

TinyPortal © 2005-2018