在Linux中使用Java RXTX与Arduino进行串行通信

时间:2015-07-12 14:56:37

标签: java linux arduino serial-communication rxtx

我正在尝试使用java程序连接Arduino,当从pc发送数据到串口连接的arduino并且程序崩溃时,它会发出错误

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f0dbc202462, pid=11386,       tid=139696967497472
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b14) (build   1.8.0_45-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode   linux-amd64 compressed oops)
# Problematic frame:
# C  [librxtxSerial.so+0x6462]  read_byte_array+0x52
#
# Failed to write core dump. Core dumps have been disabled. To enable   core dumping, try "ulimit -c unlimited" before starting Java again
#
# 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.
#

这些是我的初始化方法和发送串行方法

串行连接的初始化

public void initialize() {
    // the next line is for Raspberry Pi and
    // gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
    System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

    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();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }
    if (portId == null) {
        System.out.println("Could not find COM port.");
        return;
    }

    try {
        // open serial port, and use class name for the appName.
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);

        // set port parameters
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();

        // add event listeners
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);


        //added

    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

通过串行连接发送数据

public synchronized void send_command(String command) {
    try {
        output = serialPort.getOutputStream();

        output.write(command.getBytes());
        System.out.println(command);

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e, "Serial Connection Error!", JOptionPane.WARNING_MESSAGE);
    }

}

串行连接关闭方法

 public synchronized void close() {
    try{
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }

    }catch (Exception e){
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:2)

好好搞乱后发现了一个解决方案,似乎是

这里为Raspberry pi写的这个权利引起了它... 评论它解决了问题...

System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

//System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

还不知道究竟是什么问题......