Electronic – arduino – Issue on Serial Port Communication (in JAVA)

arduinojavaserial

I've written a Java program to read my Arduino's outputs through Serial port (using RXTX Library). There's a small issue I'm facing.

Arduino continuously writes to serial. But every time, after running the Java program I have to reset the Arduino to make the program see the serial port values. Otherwise it won't read any value.

Hope it's clear. I need to know whether there's a way to fix this. How can I make my program to read serial port values when it is started without doing anything to external devices.

Thanks!

EDIT
This is the method which'll initialize the serial port.

SerialPort serialPort;
private static final String PORT = "COM32";
private InputStream input;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;

public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if (currPortId.getName().equals(PORT)) {
                portId = currPortId;
                break;
            }
        }
        if (portId == null) {
            System.out.println("Could not find COM port.");
            return;
        }
        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);

            serialPort.setSerialPortParams(DATA_RATE,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            input = serialPort.getInputStream();
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

Best Answer

The serial flow control signals (DTR and/or RTS) must be set accordingly to the Arduino specification and to the specification of USB-SERIAL adapter you're using.

Looking briefly at the schematic of your board, I can see that the RTS signal is disconnected, but DTR signal from FTDI chip is coupled with the ATMEGA's RESET pin (I imagine that this is done to allow Arduino uploader to reset the micro and upload new firmware to it).

This means that you have to set DTR signal (in your Java code) to avoid unwanted resets.