Go Down

Topic: serial communication problem (Read 6754 times) previous topic - next topic


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 : http://playground.arduino.cc/Interfacing/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.
The art of getting good answers lies in asking good questions.


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;
      if (portId == null) {
         System.out.println("Could not find COM port.");

      try {
         // open serial port, and use class name for the appName.
         serialPort = (SerialPort) portId.open(this.getClass().getName(),

         // set port parameters

         // open the streams
         input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
         output = serialPort.getOutputStream();

         // add event listeners
      } catch (Exception e) {

    * 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) {

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


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.
The art of getting good answers lies in asking good questions.


sorry for that. My sender is too simple :
void setup(){

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


Please I need the solution for this? please help me


Mar 21, 2014, 08:03 pm Last Edit: Mar 21, 2014, 08:08 pm by Robin2 Reason: 1
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 http://forum.arduino.cc/index.php?topic=225329.0. Its more comprehensive than what you are doing.

Two or three hours spent thinking and reading documentation solves most programming problems.


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

Code: [Select]

Hope this helped,




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.

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up