serial communication problem

Hi, I'm new in arduino programming. I've to test serial communication between arduino and java program on windows 7 plateform.
When I tried example given in : Arduino Playground - Java It works but one time by tree it shows an error:
java.io.IOException: Underlying input stream returned zero bytes
I think, the problem is in :
String inputLine=input.readLine();
please help me

I think, the problem is in :
String inputLine=input.readLine();
please help me

Is that ALL of your code? No wonder it doesn't work.

here is all my code, thanks for your speed reply:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;

public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
/** The port we're normally going to use. /
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM25", // Windows
};
/
*

  • A BufferedReader which will be fed by a InputStreamReader
  • converting the bytes into characters
  • making the displayed results codepage independent
    /
    private BufferedReader input;
    /
    * The output stream to the port /
    private OutputStream output;
    /
    * Milliseconds to block while waiting for port open /
    private static final int TIME_OUT = 2000;
    /
    * Default bits per second for COM port. */
    private static final int DATA_RATE = 9600;

public void initialize() {
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);
} catch (Exception e) {
System.err.println(e.toString());
}
}

/**

  • This should be called when you stop using the port.
  • This will prevent port locking on platforms like Linux.
    */
    public synchronized void close() {
    if (serialPort != null) {
    serialPort.removeEventListener();
    serialPort.close();
    }
    }

/**

  • Handle an event on the serial port. Read the data and print it.
    */
    public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
    try {
    String inputLine=input.readLine();
    System.out.println(inputLine);
    } catch (Exception e) {
    System.err.println(e.toString()+" : prb de lecture");
    }
    }
    // Ignore all the other eventTypes, but you should consider the other ones.
    }

public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
main.initialize();
Thread t=new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}
}

I really hate code that prints unidentified stuff to the console. Try adding some identifying prefix to all the catch blocks. Knowing which one caught the exception is the first step in dealing with it.

So, now we know what the receiver looks like. The sender is still a mystery.

sorry for that. My sender is too simple :
void setup(){
Serial.begin(9600);
}

void loop(){
Serial.println("Hello world");
delay(1000);
}

IM FACING THE SAME PROBLEM.. MY ARDUINO AND JAVA CODES ARE VERY SIMILAR TO THIS CODE. MAY BE 100% SAME.
Please I need the solution for this? please help me

Java programs are so convoluted I can't figure out how it is supposed to work. Perhaps you can describe it?

One common problem is that the PC program doesn't wait long enough to allow the Arduino to reset after the serial port connection is opened, but that may not be an issue here as the Java program seems only to listen.

Do you know for sure that the Java program can read any stuff from the serial port. Most programs I have seen use the RxTx library, or more recently JSSC. RxTx seems to be fading into oblivion. Try JSSC. I have used it successfully with JRuby.

Edit to add ...
I don't see where the Java program calls the function that actually reads the serial port.

Do you have to use Java? How about using JRuby if you want to use the JVM. Or Python. I have written a demo program using Python to communicate with the Arduino, and also a JRuby version. Both are in this Thread Demo of PC-Arduino comms using Python - Interfacing w/ Software on the Computer - Arduino Forum. Its more comprehensive than what you are doing.

...R

Make sure you have the latest version of RXTX and then just add these two lines of code after you open the port

serialPort.disableReceiveTimeout();
serialPort.enableReceiveThreshold(1);

Hope this helped,

Georgios

Hi,

Could you explain to why we have to add these lines to solve the problem ? I don't understand the function of your two lines...

Thank you !

@Leoleoleo24, this is a very old Thread. Just post your own program and explain the problem.

If you are using RXTX it may make sense to switch to the newer JSSC which (IMHO) is a easier to use.

...R