[Lazarus] Advice needed for serial comm with Lazarus

Michael Schnell mschnell at lumino.de
Mon Nov 8 10:07:59 CET 2010

On 11/06/2010 12:49 AM, Bo Berglund wrote:
> How should I do here in order to get the code portable?
I have no infos about the SdpoSerial component, so I don't know whether 
it already uses threads. But the problem is best handled using a thread 
for the serial receiving that creates an event when something useful has 
been received. In a perfect world you would use an inter-thread event 
(captured by "Procedure..Message" in the main thread) but AFAIK, this is 
broken in (at least) the current Lazarus version in Linux. (I did 
successfully use it some time ago).

So - second best -  you could use a TTimer and poll in the timer event 
for a ready-flag set by the thread or - third best - receive and decode  
a many serial characters as are available in the TTimer event..

Moreover the TTimer event can manage a counter and use same to detect a 
timeout (beware: TTimer events can be dropped when the main thread is 
too busy, so the counter might run too slow and the timeout might be 

> In the Delphi implementation I have an incrementing progress bar
> inside the loop (whenever 512 new bytes have arrived it is
> incremented) plus a check for a user break condition on a Cancel
> button. There is also a timeout check for the case when characters
> stop arriving before all are collected.
Could be done in a TTimer event or in a "Procedure..Message" event.
> What is the recommended way to handle such situations? The data
> transfer may take several minutes for large data chunks and I have to
> keep the application responsive while giving feedback to the user
> about the progress...
A thread of course does not prevent the application from being 
responsive (That is why Async Pro is such a good tool for handling 
serial and TCP/IP streams, doing everything in thread and throwing 
Main-Thread events only when necessary).

But reading chunks of data in a TTimer event (and storing them in an 
appropriate buffer) should work, as well.


More information about the Lazarus mailing list