[Rxtx] noob trouble with rxtx, losing bytes somewhere
aawolfe at gmail.com
Mon Dec 14 11:35:56 MST 2009
Sorry to reply to myself (and top post), just wanted to put the
solution I found out there in case someone else has this problem in
I tried Mariusz's sample, it is essentially the same as things I've
tried before. Still I had the problem that sometimes bytes would be
in a buffer somewhere, but not in the inputstream. to be clear, the
problem seems to be that the device sends A B C, and I get only 2
events, with A and B. Then the device sends D, and I get an event,
but its the data C. And so on, sometimes many bytes behind. I'm not
actually losing any data at all that I can tell, it's just stacking up
somewhere. Whether I do events, or busy waiting on a call to
available(), or even just read constantly and throw away -1, this
happens to some degree.
The solution to this was to set enableReceiveThreshold =1 and
enableRecieveTimeout = some large number, 3000 works fine.
I don't know why this fixes it, maybe I should have set these all
along. I don't understand what the values are for, I found some posts
with descriptions but my system does not work as described.
Specifically, setting both to 0 is supposed to make reads wait till
data is available, but this doesn't work at all on my system.
Anyway, by setting values like this, any of the methods for reading
seem to work just fine. Hope that helps someone.
On Sun, Dec 13, 2009 at 5:49 AM, Aaron Wolfe <aawolfe at gmail.com> wrote:
> Apologies if this is something I should be able to figure out on my
> own, I have honestly tried for many hours to sort this out with no
> I have a pretty simple application that needs to read from the serial
> port. I think all I need is a blocking read, as the code that handles
> the port is in its own thread. I haven't found a way to do this
> simply, so I've tried many combinations of event handlers and reading
> techniques. Nothing I've come up with is reliable.
> The "best" technique I've found is to loop in the main code constantly
> checking inputStream.available() and immediately reading a byte when
> its > 0. Unfortunately this drives CPU load to 100%. If I do so much
> as a Thread.sleep(1) in the loop body, I miss bytes... the longer I
> sleep the more frequently I lose data.
> All manners of event driven handlers seem to be even worse. I've
> tried using the event to load characters into a circular buffer, into
> a PipedInputStream, not reading the data when a data event occurs but
> instead sending a notify to a waiting method in the main thread which
> reads it... nothing is reliable.
> Even weirder, often it seems that the data is being held "somewhere".
> Although available() on the input stream returns 0, sending a new byte
> to the port results in an old byte coming out the stream... sending
> enough new bytes results in the first new byte coming out, and so on.
> sometimes there are as many as 30 bytes in limbo that can only be read
> by sending in new data. So somehow the inputstream thinks there is no
> data, yet there is data somewhere in a fifo buffer? This behavior
> occurs in both event driven and direct reading styles.
> Although the 100% CPU use technique is reliable as long as I don't do
> much with the data, as soon as I actually try to process it, the time
> taken away from the reading thread results in lost data within a few
> minutes of operation.
> I've scanned through the archives and I'm not finding a similar issue.
> What the heck have I done? Any clues are much appreciated. The host
> is Windows 7 32bit with a Prolific USB to Serial adapter. I'm not
> using any flow control, the device I'm talking to doesn't support any.
> I can post code if it's of any use, but I've rewritten it so many
> times, so many ways, I don't know what exactly to post as an example.
> If I could just find a way to reliably receive the data, I'll glady
> rewrite everything else around it. Thanks for any advice.
More information about the Rxtx