libftdi Archives

Subject: Re: Callback when data available to read?

From: Samuel Bryner <s.bryner@xxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 23 Jun 2014 12:16:40 +0200
On 06/21/2014 01:08 PM, Rogier Wolff wrote:
> On Thu, Jun 19, 2014 at 07:58:49PM +0200, Samuel Bryner wrote:
>> Hi,
>>
>> Is there a way to get a callback when there is data available to read?
>>
>> As far as I can see, you can either:
>>
>> - poll ftdi_read_data()
>> - create a ftdi_transfer_control with ftdi_read_data_submit() and poll
>> that (with the additional drawback that you need to know the amount of
>> data you want to read beforehand)
>> - directly use libusb, defeating the point of libftdi somewhat.
>>
>> This would be nice for asynchronous writes as well, maybe even with some
>> sort of intermediate progress callback.
>>
>> ftdi_readstream() seems to go into the right direction but uses a
>> special synchronous FIFO mode.
> 
> 
> I'd say: that'd be tricky to implement: your program "has control"
> over the thread, and when it is not checking the FTDI chip, it's
> simply not checking the FTDI chip! So the library would have to create
> a separate thread and call your callback from the other thread, or
> maybe something with signals, and then call your callback from the
> signal.
> 
> Definitively not "trivial to add".

Well, I'm already handing over control of handling asynchronous events
as described in [1], meaning I'm already using select() to wait for
libusb events.

The select() call actually returns when I have registered a read with
ftdi_read_data_submit() and I can receive data this way. But: select()
returns even when there's no data to read, reducing the whole thing to
simple polling. And I still have to manually reset the transfer_control
structure every time I receive something, because I'm not receiving a
fixed amount of bytes.

Basically, I'm trying to get it to work like a socket...

As for the implementation of such a feature: libusb already has
callbacks in place which libftdi uses to asynchronously receive data
('ftdi_read_data_cb()'). This callback could be extended to handle a
continuous stream of data and to call a user-defined function when it
received anything.

Regards,
Sam




[1] http://libusb.sourceforge.net/api-1.0/group__poll.html#polltime


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread