1

Topic: Questions about HID Sharp

I am attempting to write a Library around Zer's HidSharp code to talk to a USB device for a project I am working on.

My question is:

What is the proper way to setup the HIDStream to allow for asynchronous communication from both ends (PC or USB device can send a message @ any time).

Right now I have back to back stream.BeginReads that will continuously poll the stream for data and then use stream.BeginWrite to send data when I receive it from my application.

What I have noticed is that after about 5 failed reads (long period of no messages) the HIDStream becomes unreliable and I stop receiving any data packets.

My current solution is to simply restart the HIDStream after 5 failed reads, but I was hoping for a more elegant solution.

Thanks.

2

Re: Questions about HID Sharp

Hmm... If you are calling HidSharp asynchronously, it might be best to disable timeouts -- HidSharp supports Timeout.Infinite (-1). That'll cause you to stop getting TimeoutExceptions. The class sets default read and write timeouts of 3 seconds on the assumption that most folks are doing blocking reads/writes.

As far as unreliability goes, may I ask what platform you are using? It wouldn't be MacOS by any chance? MacOS provides no way to do timeouts on writes, so while HidSharp will throw a TimeoutException there to let you get back to your code, it still can't cancel the underlying write which pretty much has to take its sweet time. Reads should be fine however... if not, there's a bug to fix. :-)

3

Re: Questions about HID Sharp

Hmm, actually, reviewing the Windows timeout code, it throws but doesn't get rid of the read request.

So, if data *does* come in later, it'll get eaten by a read that doesn't correspond to anything you can get to...
If your device sends continuously based on (say) sensor data, this isn't a big deal as the same data will come in the next packet. However if the rate of timeouts became on average greater than the rate of data, reads would accumulate as you describe.

I'll try out a fix later today.

4

Re: Questions about HID Sharp

Using Windows.

Device sends data sporadically. It is an adapter that allows a PC to communicate on a communication bus. Device will only send data when there is traffic on the bus, or in response to me sending data (send confirmation and such).

Sad to say I was forced to swap our your library for another. Kept getting application crashing exceptions that seemed to be related to the failed reads.

5

Re: Questions about HID Sharp

OK. Thanks for the bug report though. It's been fixed.