I encountered a problem from library nrjavaserial when we comunicate with com-port for emulating NMEA device: when calling the RXTXPort#SerialInputStream.read(byte[] bytes) method, the result is 0. The bytes parameter is never empty, bytes are read. Presumably the problem can also be caused by the int a = nativeavailable();(RXTXPort#1467) method, which returns 0, so Minimum = 1, with the consequence that in the readArray(b, off, Minimum)(RXTXPort#1492) method the result is 0. Here is the code from the library itself where the problem occurs: `public synchronized int read( byte b[], int off, int len ) throws IOException { ..... int Minimum = len;
if( threshold==0 )
{
/*
* If threshold is disabled, read should return as soon
* as data are available (up to the amount of available
* bytes in order to avoid blocking)
* Read may return earlier depending on the receive time
* out.
*/
int a = nativeavailable(); // i think main problem is here a = 0
if( a == 0 )
Minimum = 1;
else
Minimum = Math.min( Minimum, a );
}
....
IOLockedMutex.readLock().lock();
try
{
waitForTheNativeCodeSilly();
result = readArray( b, off, Minimum);//result = 0
if (debug_read_results)
z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") returned " + result + " bytes" /*+ new String(b) */);
return( result );
}`
It is important to note that the problem is not static, i.e. periodic, sometimes it can read normally, and sometimes the problem pops up and it can hang for a few minutes.
My code:
byte[] buffer = new byte[4096];
inputStream = serialPort.getInputStream();
while (!isInterrupted())
{
// problem is here, it returns 0 sometimes ->
int bytesRead = inputStream.read(buffer); ////RXTXPort#SerialInputStream.read(byte[] bytes)
if (bytesRead == -1)
break;
..........
Thread.currentThread().sleep(500);`
What is the problem and is it definitely related to my code?