title: How to read and write serial communication in Java?
body: <p>I'm using this <a href="http://www.arduino.cc/playground/Interfacing/Java" rel="nofollow">Java program</a> to communicate with an Arduino board. However, I'm having trouble reading and writing serial data to the Arduino.</p>
  
  <p>Currently it looks like this:</p>
  
  <pre><code>public synchronized void serialEvent(SerialPortEvent oEvent) {
      if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
          try {
              int available = input.available();
              byte chunk[] = new byte[available];
              input.read(chunk, 0, available);
  
              String display = new String(chunk);
              System.out.print(display);  
  
              if(display.equals("Request"))  // Having problems with this line. not entering the loop even though I received the String "Request"
              {
                  String reply = "Reply";
                  byte reply_byte[] = new byte[reply.length()];
                  reply_byte = reply.getBytes("UTF-16LE");
                  output.write(reply_byte);
              }
  
          } catch (Exception e) {
              System.err.println(e.toString());
          }
      }
  </code></pre>
  
  <p>I'm reading the input as a String and then converting a String reply to byte[] and replying. However this doesn't seem to be working. Can someone tell me a better way to do this? Maybe without converting byte[] to String and trying to interpret it.</p>