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