adeelahmedkhanabbasi / xbee-api

Automatically exported from code.google.com/p/xbee-api
GNU General Public License v3.0
0 stars 0 forks source link

Occasional XBeeParseException: Read -1 from input stream while reading packet #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The issue arises when xbee-api tries to read a byte from a input stream and 
there are no bytes available. Instead of blocking it returns -1 causing the 
exception. This might be because of RXTX version mismatch.

I have attached a patch that solves this issue for me(revision info is for my 
local repository, unsure how to diff with xbee-api repository). The new code 
makes the parser wait until the needed amount of bytes is available before 
trying to read the data. It has a timeout which probably needs to be modified 
for general use cases.

What steps will reproduce the problem?
1. Send packets to local XBee from a remote XBee.
2. A few parse attempts throws XBeeParseException: Read -1..

What is the expected output? What do you see instead?
No exceptions. I see exceptions.

What version of the product are you using? On what operating system?
xbee-api-0.9.zip
WARNING:  RXTX Version mismatch
    Jar version = RXTX-2.2pre1
    native lib Version = RXTX-2.2pre2

Please provide any additional information below.
Exception example:

[ERROR] [2011-12-04] [reamThread] api.PacketParser.parsePacket - Failed due to 
exception. Returning ErrorResponse. bytes read: 0x00,0x54,...,0x0ffffffff
com.rapplogic.xbee.api.XBeeParseException: Read -1 from input stream while 
reading packet!
     at com.rapplogic.xbee.api.PacketParser.read(PacketParser.java:239)
     at com.rapplogic.xbee.api.PacketParser.read(PacketParser.java:206)
     at 
com.rapplogic.xbee.api.PacketParser.readRemainingBytes(PacketParser.java:303)
     at 
com.rapplogic.xbee.api.zigbee.ZNetRxResponse.parse(ZNetRxResponse.java:56)
     at 
com.rapplogic.xbee.api.PacketParser.parsePacket(PacketParser.java:164)
     at 
com.rapplogic.xbee.api.InputStreamThread.run(InputStreamThread.java:151)
     at java.lang.Thread.run(Thread.java:662)
[ WARN] [2011-12-04] [3-thread-1] comm.Connection.processResponse - XBee error: 
Read -1 from input stream while reading packet!
[ WARN] [2011-12-04] [reamThread] api.InputStreamThread.run - expected start 
byte but got this 0x73, discarding

Original issue reported on code.google.com by exuvo@exuvo.se on 4 Dec 2011 at 3:33

Attachments:

GoogleCodeExporter commented 9 years ago
Additional XBee connection information. I create the connection with this code:
RxTxSerialComm serial = new RxTxSerialComm(); 
// void com.rapplogic.xbee.RxTxSerialComm.openSerialPort(String port, String 
appName, int timeout, int baudRate, int dataBits, int stopBits, int parity, int 
flowControl)
serial.openSerialPort("COM3", "XBee", 1000, 111111, SerialPort.DATABITS_8, 
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,SerialPort.FLOWCONTROL_RTSCTS_IN 
| SerialPort.FLOWCONTROL_RTSCTS_OUT);
xbee.initProviderConnection(serial);

Patch: I see now that IntArrayInputStream.java does not need the "throws 
IOException" declaration.

Original comment by exuvo@exuvo.se on 4 Dec 2011 at 3:43

GoogleCodeExporter commented 9 years ago
I got the same error on reading the remote address and on second thought it is 
much simpler and to add the following code to 
com.rapplogic.xbee.util.InputStreamWrapper.java instead of the attached path:

public int read() throws IOException {
+   if(in.available() == 0){
+       long start = System.currentTimeMillis();
+       while(in.available() == 0){
+           Thread.yield();
+           if(System.currentTimeMillis() - start > 100){
+               throw new IOException("Timeout while reading from InputStream");
+           }
+       }
+   }
    return in.read();
}

Why can't I edit the original post? Could clean up a few things.

Original comment by exuvo@exuvo.se on 4 Dec 2011 at 4:08

GoogleCodeExporter commented 9 years ago
That's great, thank you.
I have implemented you changes - it works fine now.

Attached you can find jar file with this patch. (JDK 1.6)

Original comment by kiryl.ro...@gmail.com on 27 Oct 2012 at 11:41

Attachments:

GoogleCodeExporter commented 9 years ago
Good to know that there is still some activity on xbee-api. But a 10 month 
delay is slighly too slow.

Original comment by exuvo@exuvo.se on 29 Oct 2012 at 9:54

GoogleCodeExporter commented 9 years ago
More activity! Thanks for this!

Original comment by ZackGr...@gmail.com on 12 Sep 2013 at 8:57

GoogleCodeExporter commented 9 years ago
I'm unable to reproduce this. Are you using the RXTX that comes with the 
library? What os, java version? -1 indicates the stream is closed and should 
always block on read

Original comment by andrew.rapp@gmail.com on 2 Feb 2014 at 4:28

GoogleCodeExporter commented 9 years ago
Since I last wrote here I have moved away from RXTX to nrjavaserial to fix java 
crashing (completely, no exceptions thrown) when a connected USB serial port 
disappears. I'm not sure if the problem in this issue still persists with 
nrjavaserial as I still have my patch applied.

Regarding versions I was using it says in the original message:
xbee-api-0.9.zip
WARNING:  RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2

Original comment by exuvo@exuvo.se on 3 Feb 2014 at 2:12

GoogleCodeExporter commented 9 years ago
Checked my recent logs and I get around 10 "[WARN] [15:32:50] 
rapplogic.xbee.RxTxSerialComm.serialEvent We were notified of new data but 
available() is returning 0" each minute. This is using nrjavaserial-3.8.8.jar 
as replacement for RXTX and xbee-api 0.9 with above mentioned patch (maybe 
something else as well, cant remember).
Occasionally I see the bug mentioned:
[ERROR] [15:35:21] xbee.api.PacketParser.parsePacket Failed due to exception.  
Returning ErrorResponse.  bytes read: 
0x00,0x3d,0x90,0x00,0x7d,0x33,0xa2,0x00,0x40,0x86,0x1e,0x0e,0x00,0x00,0x01,0x41,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
java.io.IOException: Timeout while reading from InputStream
    at com.rapplogic.xbee.util.InputStreamWrapper.read(InputStreamWrapper.java:20)
    at com.rapplogic.xbee.api.PacketParser.readFromStream(PacketParser.java:217)
    at com.rapplogic.xbee.api.PacketParser.read(PacketParser.java:235)
    at com.rapplogic.xbee.api.PacketParser.read(PacketParser.java:206)
    at com.rapplogic.xbee.api.PacketParser.readRemainingBytes(PacketParser.java:303)
    at com.rapplogic.xbee.api.zigbee.ZNetRxResponse.parse(ZNetRxResponse.java:56)
    at com.rapplogic.xbee.api.PacketParser.parsePacket(PacketParser.java:164)
    at com.rapplogic.xbee.api.InputStreamThread.run(InputStreamThread.java:151)
    at java.lang.Thread.run(Unknown Source)

Original comment by exuvo@exuvo.se on 3 Feb 2014 at 2:41

GoogleCodeExporter commented 9 years ago
Where is the edit button? OS Windows 7 64bit, using a sparkfun usb to xbee 
https://www.sparkfun.com/products/8687 and a xbee pro S2B.

Original comment by exuvo@exuvo.se on 3 Feb 2014 at 2:45

GoogleCodeExporter commented 9 years ago
I have Java 7 Update 45 (32-bit), 25 (64-bit) and JDK 7 Update 25 (64-bit) 
installed. Eclipse should be using the 64-bit version but I'm not certain.

Original comment by exuvo@exuvo.se on 3 Feb 2014 at 2:51

GoogleCodeExporter commented 9 years ago
I don't have windows hardware readily available anymore (since osx mavericks 
whacked my vmware), so can't test it. Can you try 32-bit java version if that's 
not what you're using. Also do you have access to linux or macosx to see if you 
can reproduce? 

Original comment by andrew.rapp@gmail.com on 3 Feb 2014 at 2:58

GoogleCodeExporter commented 9 years ago
I'll try to reproduce on Win7 64bit with 32bit java and linux 64bit when I get 
the time.

Original comment by exuvo@exuvo.se on 5 Feb 2014 at 5:24