From: Brian Candler Date: 2003-04-17T06:30:42+09:00 Subject: Re: example On Thu, Apr 17, 2003 at 05:54:49AM +0900, daniel wrote: > I'm goint to write a ruby extension for an extern hardware tool. > When my extension receives data (i.e.) from the serial > interface (com1), a function in the ruby code should be > ``executed?? > > So I'm searching now for a simple example which show how > to implement callback functions. > > Does anybody known a good link for this? I don't know about good links... but if I were doing this, I would run the data receiver as a separate thread. It then simply has to receive messages, and when it has a complete message that it wants to do something with, it just does. This might mean putting it in a queue which the main thread picks from. You can use mutexes and condition variables to make the queue thread-safe, and to allow the main thread to sleep until a message arrives. I'd recommend you start with the Pickaxe chapter on threading, and then there are a number of threading examples on the Wiki (http://www.rubygarden.org/ruby) I seem to remember seeing a module for handling serial ports on RAA somewhere. Cheers, Brian.