Closed Tarkon33 closed 1 year ago
Please add the following to your test code and let me know what the output is when bytesAvailable()
starts returning -1:
if (comPort.bytesAvailable() < 0)
System.out.println("Error code was " + comPort.getLastErrorCode() + " at Line " + comPort.getLastErrorLocation());
I tried it again with your additional code and the output was Error code was 25 at Line 864
Sorry for the long delay in getting to this. I've looked into the low-level code, and I can't see any reason (from a library standpoint) that this would be happening. The bytesAvailable()
call simply forwards a request to the underlying driver to see if there's any data available to read. The error code you posted is a Linux-based error code which usually indicates that the underlying file descriptor is not a serial port, which obviously isn't the case if your code was working for awhile before it stops.
One thing to try is changing the while (comPort.bytesAvailable() == 0)
line to while (comPort.bytesAvailable() <= 0)
so that you don't get your array index out-of-bounds error. (Perhaps every once in awhile the underlying driver is simply unable to process the bytes-available read request and is returning the error, in which case, it might be fine if you just ignore it and continue on). The other thing to verify is that your serial port isn't somehow getting disconnected somewhere else (either from some other program, or even from a loose/faulty USB cable) which would cause the file descriptor in the OS itself to change and result in the error code you're seeing being thrown.
Hey Will, thanks for looking into it again and I think that I found the reason for the error by now. It seems like I addressed the port incorrectly (ttyAMA vs serial0) and when I changed this it worked.
Hey there, I'm trying to read my MH_Z19 sensor and so far it did not work with a Java solution (with python it works fine) and so I tried to find out why. I used this example https://github.com/Fazecast/jSerialComm/wiki/Nonblocking-Reading-Usage-Example to see if it works and originally the port is open but after some time it is closed and I get an exception because the array has a negative size then. The port that it finds with getCommPorts()[0] is Serial Device (ttyAMA) and I verified that the port is open with .isOpen() Running the program with sudo didn't solve it.
Does somebody have an idea why the port is suddenly closed again?
(I'm working with a raspberry pi 3B and the standard raspbian OS)