[Lazarus] Reading and writing TMemoryStream in events?

Paul Breneman list2010 at BrenemanLabs.com
Tue Nov 9 14:45:48 CET 2010

Michael Schnell wrote:
> On 11/08/2010 05:39 PM, Henry Vermaak wrote:
>> It's pretty portable, being POSIX and all. 
> With "portable", here I meant "same code for Windows and Linux" (I'd 
> better called it "Cross-Platform").
>> For Windows you'd use overlapped io, iocp and wait functions.  Or 
>> libevent, which is supposed to be completely cross platform, but I 
>> haven't tried that yet.
> So there is some cross platform method in libevent that can be used ?
> (AFAIK, he wants to wait for serial characters and for a timer at the 
> same time in a project without a GUI.)

It is a little hard to explain since my Comm DataModule has 2200 lines 
with input and output threads and a TThreadList for each, and that 
DataModule is created by another thread with message processing.  The 
same unit works with AsyncPro in Windows (Delphi) and SynaSer in Linux 
(Free Pascal and fpGUI for GUI programs).  There are console and GUI 
programs in each OS.

The way I write serial communication "engines" would never work for 
servers with a high load, but industrial serial communication almost 
always presents very little load to the CPU.  Therefore I can add 
additional layers of threads (on top of the 3 threads that AsyncPro 
itself creates) as needed to best abstract the code so it isn't coupled 
into the GUI.  The additional threads also allow me to avoid writing 
state machines with multiple event handlers interacting (for received 
data, timeouts, etc) which is the standard way to use AsyncPro and which 
is heavily coupled into the GUI.  I find it nice to write "linear" code 
in a separate thread so all error handling is inline.

In the main project for this code the protocol contains device addresses 
so an embedded Linux touchscreen controller can receive a command to set 
its clock and the engine can deal with that and send back the 
acknowledgment to the requesting master device at the same time the user 
is using the touchscreen to interact with a different device.  So its 
not just a simple request/response situation.

There are examples of basic "engines" using AsyncPro on this page:

I'd like to duplicate some of those with SynaSer or other cross-platform 
libraries (and add them to the page below) so if anyone has some extra 
time please contact me...  :)

Paul Breneman
www.dbReplication.com - VCL database replication components
www.TurboControl.com - Hardware and software development services
- Educational programming project for environment monitoring
- Information on using FreePascal for embedded systems
- Support information for the TurboPower open source libraries

More information about the Lazarus mailing list