title: read data from a com port and write it to a text box in java
body: <p>I am trying to read data from a com port (barcode scanner) and write it to a text box. I'm relatively new to java, so I believe I'm missing something basic, but documentation on serial communications is limited. I am trying to get the input from the barcode reader to write to a text box named "txtONE" built in my fxml documentation. Here is my code:</p>
  
  <pre><code>    package javafxapplication7;
  
      import javafx.application.Application;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.stage.Stage;
      import gnu.io.CommPort;
      import gnu.io.CommPortIdentifier;
      import gnu.io.SerialPort;
      import gnu.io.SerialPortEvent;
      import gnu.io.SerialPortEventListener;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
  
      /**
       *
  
       */
      public class JavaFXApplication7 extends Application {
  
          @Override
          public void start(Stage stage) throws Exception {
              Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
  
              Scene scene = new Scene(root);
  
              stage.setScene(scene);
              stage.show();
          }
  
          public JavaFXApplication7()
          {
              super();
          }
  
          void connect ( String portName ) throws Exception
          {
              CommPortIdentifier portIdentifier =         CommPortIdentifier.getPortIdentifier(portName);
              if ( portIdentifier.isCurrentlyOwned() )
              {
                  System.out.println("Error: Port is currently in use");
              }
      else
      {
          CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
  
          if ( commPort instanceof SerialPort )
          {
              SerialPort serialPort = (SerialPort) commPort;
              serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
  
              InputStream in = serialPort.getInputStream();
              OutputStream out = serialPort.getOutputStream();
  
              (new Thread(new SerialWriter(out))).start();
  
              serialPort.addEventListener(new SerialReader(in));
              serialPort.notifyOnDataAvailable(true);
  
          }
          else
          {
              System.out.println("Error: Only serial ports are handled by this program.");
          }
      }     
  }
  
  /**
   * Handles the input coming from the serial port. A new line character
   * is treated as the end of a block in this example. 
   */
  public static class SerialReader implements SerialPortEventListener 
  {
      private InputStream in;
      private byte[] buffer = new byte[1024];
  
      public SerialReader ( InputStream in )
      {
          this.in = in;
      }
  
      @Override
      public void serialEvent(SerialPortEvent arg0) {
          int data;
  
          try
          {
              int len = 0;
              while ( ( data = in.read()) &gt; -1 )
              {
                  if ( data == '\n' ) {
                      break;
                  }
                  buffer[len++] = (byte) data;
              }
              System.out.print(new String(buffer,0,len));
              txtOne.text = new String(buffer,0,len);
          }
          catch ( IOException e )
          {
              e.printStackTrace();
              System.exit(-1);
          }             
      }
  
  }
  </code></pre>