[Rxtx] RXTX serial port read() returns -1 unexpectedly and then blocks forever on close()

Ryan Boder 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
> received
> 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,
> is correct, just speculating that the behavior could be something like
> that.
>

Receive framing is not enabled by default.

http://download.oracle.com/docs/cd/E17802_01/products/products/javacomm/reference/api/javax/comm/CommPort.html#enableReceiveFraming%28int%29

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
> example
> makes me wonder if the code is otherwise as it should be, meaning are you
> assuming
> 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)
blocking read().



>
> Also using read(), instead of read(bytes[],int,int), is not very efficient
> and
> 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:
>
>
>  System.out.printf("%02X",x);
>

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,
-- 
Ryan Boder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qbang.org/pipermail/rxtx/attachments/20110303/42ed700a/attachment-0536.htm>


More information about the Rxtx mailing list