Ich schreibe einen Java-Code zum Empfangen von Daten aus Arduino mit RXTX-Bibliothek über COM-Ports. Wenn Java es in der Konsole empfängt und druckt, wenn das Arduino plötzlich entfernt wird, schließt JVM mit dem Fehler JAVA Ergebnis 255. Wie man diesen Fehler fängt.Wenn Arduino plötzlich entfernt wurde, sollte "Gerät entfernt" gedruckt werden.JVM Closing mit Java-Ergebnis 255 Fehler
package arduino.recieve;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
public class NewClass implements SerialPortEventListener {
SerialPort serialPort = null;
private static final String PORT_NAMES[] = {
// "/dev/tty.usbmodem", // Mac OS X
// "/dev/usbdev", // Linux
// "/dev/tty", // Linux
// "/dev/serial", // Linux
"COM3"};
private String appName;
private BufferedReader input;
private OutputStream output;
private static final int TIME_OUT = 1000; // Port open timeout
private static final int DATA_RATE = 9600; // Arduino serial port
public boolean initialize() {
try {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// Enumerate system ports and try connecting to Arduino over each
//
System.out.println("Trying:");
while (portId == null && portEnum.hasMoreElements()) {
// Iterate through your host computer's serial port IDs
//n2
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
System.out.println(" port" + currPortId.getName());
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)
|| currPortId.getName().startsWith(portName)) {
// Try to connect to the Arduino on this port
//
// Open serial port
serialPort = (SerialPort) currPortId.open(appName, TIME_OUT);
portId = currPortId;
System.out.println("Connected on port" + currPortId.getName());
break;
}
}
}
if (portId == null || serialPort == null) {
System.out.println("Oops... Could not connect to Arduino");
return false;
}
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// add event listeners
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
{
System.out.println(" Too Many Listeners ");
}
}
serialPort.notifyOnDataAvailable(true);
// Give the Arduino some time
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
//
// Handle serial port event
//
public synchronized void serialEvent(SerialPortEvent oEvent) {
try {
switch (oEvent.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
if (input == null) {
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
}
String inputLine = input.readLine();
System.out.println(inputLine);
break;
default:
break;
}
} catch (Exception e) {
System.out.println("Hello");
}
}
public static void main(String[] args) throws Exception {
NewClass test = new NewClass();
test.initialize();
}
}
Versucht es aber nicht funktioniert –