【问题标题】:Serial Communication with Arduino using Java RXTX in linux在 Linux 中使用 Java RXTX 与 Arduino 进行串行通信
【发布时间】:2015-07-12 14:56:36
【问题描述】:

我正在尝试使用 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();
    }

}

【问题讨论】:

    标签: java linux arduino serial-communication rxtx


    【解决方案1】:

    好在折腾后找到了解决办法,好像是

    这里是为 Raspberry pi 编写的,导致它... 评论出来解决了问题...

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

    还是不知道到底是什么问题……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多