Skip to content
Advertisement

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

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

}

Advertisement

Answer

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…

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…

Advertisement