Long size strings using WriteText() and ReadText()

Good evening, it is a pleasure to be part of this community.

My first post is a question that I have in the use of the Async32 component of TMS.

I have the following scenario:

An RS232 interface connected to my serial port with the Tx and Rx lines connected together for the purpose of sending a frame to the port and immediately receiving it.

My purpose is to send long String frames to the port using the WriteText () and ReadText () methods. I'm working at a speed of 4800bps.

I did a very basic program and it is the following one:

void __fastcall TForm2 :: Button1Click (TObject * Sender)

   AnsiString Text_rx;
   Comm3-> Open ();
   Memo1-> Text = "Sending a long enough frame to the serial port";
   Comm3-> WriteText (Memo1-> Text);
   Sleep (100);
   Texto_rx = Comm3-> ReadText ();
   Memo2-> Text = TextRx;
   Comm3-> Close ();


When I click on Button 1, on Memo2 I have only one part of the sent string: "Sending a long enough frame to the", only i receive truncated string.

If I increase the sleep time to 200 mS I get the complete frame sent ("Sending a long enough frame to the serial port").

My question is this: How could I detect the end of the frame in order to not use a Sleep() function and get in the reception buffer the complete frame sent without being truncated, maybe the trigger of some event ?.

Thanks in advance.

Nobody can give an idea? :-(

If you know how many bytes you should receive, you could check

VaComm.ReadBufUsed: integer to wait till sufficient bytes are received before reading the buffer with the ReadText() method.

That is the problem!, i don't know many bytes will be incoming in my RX buffer!. String length will be variable always. 

Any idea Bruno?.

If you do not know in advance how long your frame will be, you'll need to somehow write a loop that can scan incoming data and detect the end of the frame based on the incoming data. Typically in communication protocols, end of frame markers are added at the end of the text. An alternative, also  often used in communication protocols is that the frame length is sent as first bytes of data before the actual frame data. 

Ok, i think so.

String incoming with new line character (\n->0x0A) at end of frame. 
But how i can detect such character with ReadText() method?.
Thanks in advance

You'd need to do something like Pos($0A,...) in the text read or alternatively, use VaComm.ReadBuf() where you receive the serial data as an array of bytes and then scan this received array of bytes for byte 0x0A.

Ok, i will try. 

Thank you for your support.