简体   繁体   English

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

[英]Serial Communication with Arduino using Java RXTX in linux

I am trying to connect Arduino using java program and it gives out error when sending data from the pc to arduino from a serial-connection and the program crashes 我正在尝试使用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.
#

And these are my initialization methods and send methods for serial 这些是我的初始化方法和串行发送方法

Initialization of Serial Connection 串行连接的初始化

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

Sending data via Serial Connection 通过串行连接发送数据

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

}

Serial connection close method 串行连接关闭方法

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

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

}

Well after messing around found a solution,It seems to be 好吧,乱搞后发现了一个解决方案,似乎是

This right in here written for Raspberry pi caused it... Commenting it out solved problem... 在这里为Raspberry pi写的这个权利引起了它...评论它解决了问题......

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

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

Still don't know what exactly the problem is... 还是不知道到底是什么问题......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM