title: Java SerialPortEvent issue: DATA_AVAILABLE calls faster then incoming data
body: <p>I'm posting for the first time so I hope that I write everything according to the format.</p>
  
  <p>Currently I'm working on a project using Serial Communication with RXTX between a java application and a measuring device. This works great, but now I want to catch the data that the device sends with events.</p>
  
  <p>The code below works but has the following issue:
  <code>DATA_AVAILABLE</code> will be called 4 times before all the data will be send.
  I catch this in a string called <code>vBuffer</code> and I'm able to catch the data to get the full string.</p>
  
  <p>Now I want to return this data (the full string), but cannot find a <code>SerialPortEvent</code> that will wait till all data is send to return this string.</p>
  
  <p>In the example below I use <code>OUTPUT_BUFFER_EMPTY</code> but this is called at the beginning of sending a command. This means that when sending the command for the 2nd time, the <code>OUTPUT_BUFFER_EMPTY</code> event will return vBuffer with data from the first command, and immediately after starts the 2nd command. At 3rd time <code>OUTPUT_BUFFER_EMPTY</code> sends the data from the 2nd command and starts the 3rd etc.</p>
  
  <p>Is there a way in <code>DATA_AVAILABLE</code> to wait till all data is send, or is there another event that will be called after all data is send?</p>
  
  <p>Extra info: A command is send with a <code>Stringbuilder</code> of chars to make sure the right format for the device is send. The lay-out of a command is as follows: <code>&lt;STX&gt;&lt;COMMAND&gt;&lt;RTX&gt;&lt;CR&gt;&lt;LF&gt;</code>.
  Might I be able to catch the end by looking at when the command ends? If so, how?</p>
  
  <p>Update: This is the code how I send a function:</p>
  
  <pre><code>  StringBuilder message = new StringBuilder();
    message.append(new Character((char) 2));   // STX (Start of Text)
    message.append("M");                       // Command character
    message.append(new Character((char) 3));   // ETX (End of Text
    message.append(new Character((char) 13));  // CR (Carriage Return)
    message.append(new Character((char) 10));  // LF (Line Feed)
    outputStream.write(message.toString().getBytes());
  </code></pre>
  
  <p>After this the <code>DATA_AVAILABLE</code> will kick in. But doesn't wait till all received data is done.</p>
  
  <p>Edit: To bump this up, still not further with the problem.</p>
  
  <p><strong>serialEvent Method :</strong></p>
  
  <pre><code>public void serialEvent(SerialPortEvent event)
   {
    switch (event.getEventType())
    {
     case SerialPortEvent.BI:
     case SerialPortEvent.OE:
     case SerialPortEvent.FE:
     case SerialPortEvent.PE:
     case SerialPortEvent.CD:
     case SerialPortEvent.CTS:
     case SerialPortEvent.DSR:
     case SerialPortEvent.RI:
     case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
      if (vBuffer != "")
       {
        System.out.println(vBuffer);
       }
       break;  
     case SerialPortEvent.DATA_AVAILABLE:
      byte[] readBuffer = new byte[40];
  
      try
      {
        while (inputStream.available() &gt; 0)
        {
          int numBytes = inputStream.read(readBuffer);
        }
        vBuffer += new String(readBuffer);
        System.out.print(new String(readBuffer));
      }
      catch (IOException e)
      {
        System.out.println(e);
      }
      break;
  </code></pre>