NeuronRobotics / nrjavaserial

A Java Serial Port system. This is a fork of the RXTX project that uses in jar loading of the native code.
Other
344 stars 142 forks source link

Method readArray returns 0 even though data is available for reading #250

Open ARZZEZZAN opened 5 months ago

ARZZEZZAN commented 5 months ago

I encountered an interesting problem: when calling the readArray(b, off, Minimum) method, which is called exclusively inside your code(namely RTXTPort#1492), the result is 0(Minimum = 1). The b parameter is not empty, the bytes are readable. Presumably the problem may also be caused by the int a = nativeavailable();(RTXTPort#1467) method, which returns 0, which is why Minimum = 1.

Important note, the problem is not static, i.e. it is periodic, sometimes it can read normally, and sometimes the problem pops and it can freeze for a few minutes up.

My code:

      byte[] buffer = new byte[4096];

      inputStream = serialPort.getInputStream();

      while (!isInterrupted())
      {

        // problem is here, it returns 0 sometimes ->
         int bytesRead = inputStream.read(buffer); //#RTXTPort inputStream 

        if (bytesRead == -1)
          break;

       ..........

        Thread.currentThread().sleep(500);

What the problem is and whether it's yours?