On Mon, 31 Aug 2020 11:15:10 +0200 (CEST), Michael Van Canneyt via fpc-pascal <fpc-pascal@lists.freepascal.org> wrote:
> > >On Mon, 31 Aug 2020, Bo Berglund via fpc-pascal wrote: > >> On Fri, 21 Aug 2020 21:03:31 +0200, Bo Berglund via fpc-pascal >> <fpc-pascal@lists.freepascal.org> wrote: >> >> But when doing this I have run into a problem, how can I check how >> many Rx bytes are available to be read from the operating system? > >There is some ioctl() function for that, but using that is not recommended. > >The normal way of doing is is reading a buffer till no more data >is read and then treat the buffer Yes, but my problem is that I don't know in advance how many bytes there are coming... >> >> If I simply use the SerReadTimeout() function it will return either >> with 1 or more bytes or after the timeout. But there is a problem >> here: >> >> function SerReadTimeout(Handle: TSerialHandle; var Buffer; mSec: >> LongInt): LongInt; >> >> When calling this the Buffer length has to be set *beforehand* so the >> arriving data can fit inside the buffer, but how can I know how big >> Buffer must be? > >Serial normally does not handle buffers >4k. OK so if I assign an 8k TBytes buffer inside the read thread and use that then I can copy the buffer data into another TBytes container which is the size of the received data and supply that in the event function. Of course the event must be processed before the thread continues its read attempts, so for that I thought I'd have to use Synchronize() > >since fpRead will never read more than Count bytes >(count-result is always less than count for positive result) result can at >most be Count. > >(if fpRead returns a negative result, the algorithm is simply wrong, the >algorithm assumes the fpRead will always succeed) > >Disregarding the error condition you can perfectly use this function. Thanks, I was worried about reading more data than specified. Now I can let the loop in Execute read say 10 bytes or so with timeout ane process these, then loop back after firing off the event. -- Bo Berglund Developer in Sweden _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal