[Lazarus] Is porting Delphi program using AsyncPro to fpc/laz possible?

Michael Schnell mschnell at lumino.de
Thu Jan 21 11:17:32 CET 2016


On 01/21/2016 10:09 AM, Bo Berglund wrote:
> . which implements an *event driven* serial
> comm port, which can switch between serial and sockets communications.
> The incoming data are dealt with in the OnTriggerAvail event.
> So these are the only specific items used by me, but obviously in the
> background there are a whole lot of things going on...
> I tried to trace the tree of used units starting from the
> TApdWinsockPort and it just grows and grows. Seems like a project I
> could not do on my own.
>
> Has anyone here tried/succeeded?
>
Implementing Event triggered behavior (such as AsyncPro) no great 
problem. I did several test on that (in a *portable OS independent* way).

IMHO the best way to go is:

  - Do the receiving in a Worker Thread based on a blocking (serial or 
socket) receive function  (i.e. the thread always hangs in the receive 
and only does some work when (at least) a character has come in) .This 
is exactly how AsyncPro works.

  - The Thread collects the appropriate amount of data (based on 
dynamical settings provided to the class. This is exactly how AsyncPro 
works.

  - Once the appropriate data is collected you *create* a "transfer 
object" (an instance of a transfer class that allocates and includes 
buffer for the data (not just a pointer if the buffer could be reused by 
the thread ! - You also could use newly allocated managed buffers for 
data collection, avoiding doing a copy -).  This object provides a 
"APdataavailable" procedure that is to be called to extract the data and 
(of course) a destructor that frees the buffer.

  - You do TThread.Queue providing the transfer object's APdataavailable 
procedure. After that you don't need to store the pointer to that 
transfer object, as it will free itself (including the data buffer) 
later in the main thread.

  - The APdataavailable procedure will be executed as an event in the 
main thread. It will call the appropriate event handler defined as an 
AsynPro property.

  - The last statement of the APdataavailable procedure (after calling 
the user event handler) will be just "free".

have fun,
-Michael




More information about the Lazarus mailing list