title: Send String from Java to Arduino (simple example)
body: <blockquote>
    <p>It is solved. I put a <code>Thread.sleep(4000);</code> after opening the port in the java code and now it works. The problem was that the arduino is reset everytime the port is opened. When i was sending the data, the arduino wasn't ready to listen.</p>
  </blockquote>
  
  <p>I'm new to arduino and Java, but i made a program so simple that I don't get why isn't working.</p>
  
  <p>I send a String to the serial port that correspond to arduino (COM5):</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  import gnu.io.*;
  
  public class SimpleWrite {
  
  static Enumeration portList;
  static CommPortIdentifier portId;
  static String messageString = "color FF00FFEND";
  static SerialPort serialPort;
  static OutputStream outputStream;
  
  public static void main(String[] args) {
      portList = CommPortIdentifier.getPortIdentifiers();
  
  
      while (portList.hasMoreElements()) {
  
          portId = (CommPortIdentifier) portList.nextElement();
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  
               if (portId.getName().equals("COM5")) {
  
                  try {
                      serialPort = (SerialPort)
                      portId.open("SimpleWriteApp", 2000);
  
                      outputStream = serialPort.getOutputStream();
  
                      serialPort.setSerialPortParams(9600,
                          SerialPort.DATABITS_8,
                          SerialPort.STOPBITS_1,
                          SerialPort.PARITY_NONE);
  
                      outputStream.write(messageString.getBytes());
                      System.out.println(messageString);
  
                      outputStream.close();
                      serialPort.close();
                  } 
                  catch (IOException e) {System.out.println("err3");}
                  catch (PortInUseException e) {System.out.println("err");}
                  catch (IOException e) {System.out.println("err1");}
                  catch (UnsupportedCommOperationException e) {System.out.println("err2");}
              }
          }
      }
  }
  }
  </code></pre>
  
  <p>and the code in arduino to get that string:</p>
  
  <pre><code>char inputBuffer[10];   
  
  void setup() {                
    Serial.begin(9600);  
  }
  
  void loop() {
      while (true) 
      {
        if (Serial.available() &gt; 0) {
            Serial.readBytes(inputBuffer, Serial.available());
            delay(5000);
            Serial.print("I got this -&gt;");
            Serial.print(inputBuffer);
            Serial.println("&lt;-");
        }
      }
  }
  </code></pre>
  
  <p>the while(true) is for testing purposes. I dont get nothing printed, and I dont know where the problem is. I have seen all the post about arduino and java here and i dont find nothing that helps. 
  Thanks for the help and sorry if it is a stupid question, im a newbie to this</p>
  
  <p>Im using RXTXcomm.jar. Version: RXTX-2.2-20081207</p>