使用RXTX Java

时间:2018-11-30 10:28:24

标签: java serial-port rxtx

我有一个Arduino草图,可将数据打印到串行监视器。
而且我正在尝试使用RXTX库从Java代码读取数据。

但是我收到一个错误消息:

  A fatal error has been detected by the Java Runtime Environment:

  SIGSEGV (0xb) at pc=0x00007f8b985ccd9d, pid=23812, tid=0x00007f8b985c5700

 JRE version: Java(TM) SE Runtime Environment (8.0_181-b13) (build 1.8.0_181-b13)
 Java VM: Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode linux-amd64 compressed oops)
 Problematic frame:
 C  [librxtxSerial.so+0x6d9d]  read_byte_array+0x3d

 Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again

 An error report file with more information is saved as:
 /home/user1/javaprograms/IoTData/hs_err_pid23812.log

 If you would like to submit a bug report, please visit:
 http://bugreport.java.com/bugreport/crash.jsp
 The crash happened outside the Java Virtual Machine in native code.
 See problematic frame for where to report the bug.

即使它不起作用,我也尝试了ulimit命令。我的串行测试代码如下:

public void initialize() {

    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    //First, Find an instance of serial port as set in PORT_NAMES.
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        if(currPortId.getName().equals("/dev/ttyUSB0")) {
            System.out.println("PortID: "+ currPortId.getName());
            try {
                SerialPort port= (SerialPort) currPortId.open("PortListOpen",20);
                System.out.println("Port Opened Successfully");
                try {
                    int baudRate= 9600;
                    port.setSerialPortParams(baudRate, SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_EVEN);
                    port.setDTR(true);
                    System.out.println("Properties are set");
                    input = new BufferedReader(new InputStreamReader(port.getInputStream()));
                    output = port.getOutputStream();
                    // add event listeners
                    port.addEventListener(this);
                    port.notifyOnDataAvailable(true);
                }catch(UnsupportedCommOperationException e) {
                    System.out.println(e);
                }
            }catch(Exception e) {
                System.out.println("In exception: "+ e);
            }
        }
    }
}

/**
 * This should be called when you stop using the port.
 * This will prevent port locking on platforms like Linux.
 */
public synchronized void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

/**
 * Handle an event on the serial port. Read the data and print it.
 */
public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine=input.readLine();
            System.out.println(inputLine);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    // Ignore all the other eventTypes, but you should consider the other ones.
}

public static void main(String[] args) throws Exception {
    SerialData main = new SerialData();
    main.initialize();
    Thread t=new Thread() {
        public void run() {
            //the following line will keep this app alive for 1000 seconds,
            //waiting for events to occur and responding to them (printing incoming messages to console).
            try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
        }
    };
    t.start();
    System.out.println("Started");
}

在这方面请帮助我,为什么会发生核心转储。

0 个答案:

没有答案