title: BufferedReader skipping random characters when reading from USB
body: <p>I'm using a BufferedReader to read data from an USB gateway which periodically receives ZigBee network frames from an Arduino device.</p>
  
  <p>This is what the frame is supposed to look like:</p>
  
  <pre><code>~f�}3�@v&lt;-,R#}3�@v&lt;--mac:0013A20040763C2D -H:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35 
  </code></pre>
  
  <p>But instead, it's always missing some characters near the end of the MAC address, like so:</p>
  
  <pre><code>~f�}3�@v&lt;-,R#}3�@v&lt;--mac:0013A2004076D -H:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35 
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>~f�}3�@v&lt;-,R#}3�@v&lt;--mac:0013A2004076C2:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35 
  </code></pre>
  
  <p>The garbage at the beginning is low-level network header info, I guess.</p>
  
  <p>I'm on Ubuntu, and the frames show perfectly fine when reading from a terminal, using </p>
  
  <pre><code>cat /dev/ttyUSB0
  </code></pre>
  
  <p>The code I use to read from the USB port looks like this. It runs in its own Thread.</p>
  
  <pre><code>public void run() {
      Boolean keepRunning = true;
      BufferedReader br = new BufferedReader(new InputStreamReader(portReader.getInputStream()));
      String line;
  
      while (keepRunning) {
          try {   
              while ((line = br.readLine()) != null) {
                  handleData(line);
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I'm using RXTXcomm.jar, available here <a href="http://rxtx.qbang.org/wiki/index.php/Main_Page" rel="nofollow">http://rxtx.qbang.org/wiki/index.php/Main_Page</a></p>
  
  <p>This is where I open the port.</p>
  
  <pre><code>while (!connected &amp;&amp; !timedOut) {
                  System.out.print("\n\nConnecting to " + portName);
                  //Open Ports
                  CommPort commPort = portIdentifier.open(this.getClass()
                          .getName(), 9600);
  
                  //TODO Should we rule out other kinds?
                  if (commPort instanceof SerialPort) {
  
                      //Pass the open port
                      SerialPort serialPort = (SerialPort) commPort;
                      serialPort.enableReceiveTimeout(15000);
  
                      //Configure the port communication interface
                      serialPort.setSerialPortParams(bauds,
                              SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                              SerialPort.PARITY_NONE);
  
                      //Open a stream and read from the port
                      inputStream = serialPort.getInputStream();
                      int portBuffer = inputStream.read();
  
                      //Check if there is something in the buffer, which
                      //means that a connection was established
                      if (portBuffer &gt; -1) {
                          connected = true;                           
                      } else {
                          System.err.println("Connection to " + portName
                                  + " timed out");
                          serialPort.close();
                          inputStream.close();
                          timedOut = true;
                      }
                  } else {
                      System.err
                              .println("Error: Only serial ports are handled by this application.");
                  }
              }
  </code></pre>
  
  <p>Any ideas on what could be going wrong?</p>