[Rxtx] RXTX serial port read() returns -1 unexpectedly and then blocks forever on close()
ryan.boder at gmail.com
Thu Mar 3 11:48:20 MST 2011
On Thu, Mar 3, 2011 at 1:08 PM, Kustaa Nyholm <Kustaa.Nyholm at planmeca.com>wrote:
> On 3/3/11 16:56, "Ryan Boder" <ryan.boder at gmail.com> wrote:
> >I'm not assuming the entire message has arrived, I'm just assuming it
> >either has arrived or will arrive soon which is why I'm using a
> >(supposedly) blocking read().
> I've been giving this some further thought.
> It does not feel healthy to block without timeout, especially
> from within a thread that is essentially internal to the rxtx.
> I've never done that, I always use a time out, read(byte,int,int)
> for the expected number of bytes, check how much I got and issue
> more reads until I get everything.
> You have not described how your communication works so I'm only
> If your device needs some response to send more bytes (from your
> code it looks like this is not the case) then a missing byte
> would block your read and prevent you from responding and
> getting more data. But as said, looks like that is not the case.
> Anyway, if this was my problem, I would enable the timeout
> and use read(byte,int,int) and loop until I get what I need.
You are right, I should and will use a timeout. In fact, my program has been
running flawlessly for 12 hours and the only change I made was to enable a
timeout, no change in the logic at all. Without the timeout read() was
returning -1 usually after it ran for an hour or two. I will change the code
to make use of the timeout and handle it appropriately.
It still seems to me that RXTX's behavior doesn't match the documentation
when rx timeout and rx threshold are disabled read() should block until data
is available instead of immediately returning -1. I don't believe that a
framing error is occurring immediately every time data is not available
causing read() to return -1. Especially since the C# test program above runs
all day with no error and the Java program above runs all day with no error
when a timeout is enabled. To me it seems like enabling the timeout is what
is causing read() to block until data is available, even if only for 1600ms,
and without the timeout read() is returning -1 immediately when it should be
Thanks for all the help and best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Rxtx