[Rxtx] RXTX serial port read() returns -1 unexpectedly and then blocks forever on close()
ryan.boder at gmail.com
Thu Mar 3 07:56:16 MST 2011
On Thu, Mar 3, 2011 at 5:39 AM, Kustaa Nyholm <Kustaa.Nyholm at planmeca.com>wrote:
> As a workaround have you tried to perform another read() if it returns -1?
I tried that and it did work sometimes but not all the time.
> The javadoc says (see getInputStream):
> "Note, however, that framing errors may cause the Timeout and Threshold
> values to complete prematurely without raising an exception."
> I read this as a hint that read might return even if no data is available.
> Because of the above and that read() can only return -1 or the byte
> it would be somewhat logical to return -1 in case of framing error ?
> I'm not claiming that my interpretation of the specification, such as it
> is correct, just speculating that the behavior could be something like
Receive framing is not enabled by default.
My choice of word might be misleading, when I said frame I just meant a
contiguous message from the device. I am not enabling receive framing. Can
you have a framing error when receive framing is disabled?
> It might give you some more insight if you used the read(bytes,int,int)
> method, because
> this can and will return 0 in case of timeout (and presumably might do so
> in case of framing
> error, maybe some other error condition too).
> That might allow you to handle or work around the problem in this case.
> You have not shown your complete code (no need) but your comments and the
> makes me wonder if the code is otherwise as it should be, meaning are you
> that when you get the DATA_AVAILABLE event all of your frame has arrived
> and that
> you can just blindly read it away from the input stream? That is not
> guaranteed of course.
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)
> Also using read(), instead of read(bytes,int,int), is not very efficient
> might be masking other problems as mused above.
True, I'm reading one byte at a time because I don't know how long the
message will be until I read and parse some of it. I suppose I can use
read(bytes,int,int) once I figure out how long the message will be.
> BTE you can print a byte in hex with leading zeros more easily with:
I did it the dumb way in that test program so I could copy and paste the
code from C# to Java without having to figure out how to do the same in C#
which I'm not as familiar with.
Thanks and BR,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Rxtx