title: Read whole message from serial port
body: <p>I have box which sends me every 5s control message <strong><em>#2214</em></strong> over Serial Port, but when something happen this box sends me expected message eg. <strong><em>$22021CB47E06B9B97BA56600</em></strong> I need to work with this "dolar message", but I need whole message. When I ran code from java the output from box looks like</p>
  
  <blockquote>
    <p>AVAILABLE 6</p>
    
    <p>#2214</p>
    
    <p>AVAILABLE 1 </p>
    
    <p>AVAILABLE 7</p>
    
    <p>#2214</p>
    
    <p>AVAILABLE 1</p>
    
    <p>$</p>
    
    <p>AVAILABLE 14</p>
    
    <p>22021CB47E06B9</p>
    
    <p>AVAILABLE 12</p>
    
    <p>B97BA56600</p>
    
    <p>AVAILABLE 4</p>
    
    <p>#221</p>
    
    <p>AVAILABLE 3</p>
    
    <p>4</p>
  </blockquote>
  
  <p>It is some way how to wait on all bytes if you don't know the size of message?
  There is piece of my code. I was tried a lots of ways. Thanks for help.</p>
  
  <pre><code>public static class SerialReader implements Runnable {
  
      InputStream in;
      SerialPort comPort;
  
      public SerialReader(InputStream in, SerialPort comPort) {
          this.in = in;
          this.comPort = comPort;
      }
  
      @Override
      public void run() {
          comPort.addDataListener(new SerialPortDataListener() {
  
              @Override
              public int getListeningEvents() {
                  return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
              }
  
              @Override
              public void serialEvent(SerialPortEvent event) {
                  try {
                      StringWriter sw = new StringWriter();
  
                      if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
                          return;
                      }
  
                      int i = 0; 
                      while ((i = in.available()) &gt; 0) {
                           byte[] readBuffer = new byte[i];
                          in.read(readBuffer);
                          System.out.println("AVAILIBLE "+i);
                          System.out.println(new String(readBuffer));
                      }
                  } catch (IOException ex) {
                      Logger.getLogger(SerialPortReader.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
          });
      }
  }
  </code></pre>