Go Down

Topic: serial communication problem (Read 1 time) previous topic - next topic

temad46

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

PaulS

Quote
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.

temad46

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

PaulS

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.

temad46

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

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

sham999

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

Robin2

#6
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.

...R

Go Up