JVM关闭与Java结果255错误

时间:2016-06-03 16:37:12

标签: java arduino

我编写了一个Java代码,通过COM端口使用RXTX Library从arduino接收数据。当Java在控制台中接收并打印它时,如果arduino突然删除,那么JVM关闭时出现错误JAVA Result 255.如何捕获该错误。当arduino突然删除时,它应该打印"设备被删除"。

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();
    }

}

1 个答案:

答案 0 :(得分:0)

串口生成的其他几种事件类型可以尝试检测,以确定当arudino断开连接时是否会发生这种情况。不幸的是,看起来没有DTR事件可能是出现问题的最佳指标,但SerialPort类确实有一个isDTR()方法。您可以定期检查,如果这是错误的,那么这可能意味着您已经失去了与arduino的连接。您可以在单独的线程中进行检查,也可以在每次CTS更改事件中进行检查。

            serialPort.notifyOnDataAvailable(true);
            serialPort.notifyOnCTS(true);

...

            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;

                case SerialPortEvent.CTS:
                    if (!serialPort.isDTR()) {
                      // do something
                    }
                    break;

                default:
                    break;
            }

可能有一个只检查DTR状态的小工作线程会是一个更好的解决方案。