title: Reading data from serial port in java
body: <p>I want to read data (which is weight ) from a weigh bridge which is connected to my computer with <code>com1</code> port. I search the web and i am able to connect to the port and read data but the problem is that the data is not meaning full. </p>
  
  <p>I am getting somewhat like below</p>
  
  <p><img src="https://i.stack.imgur.com/awJ9B.png" alt="enter image description here"></p>
  
  <p>There is a weight of 145 is on the weigh bridge but i am getting these symbols.
  Here is the code with which i am reading the data. </p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package readingports;
  
  import java.io.BufferedInputStream;
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.Enumeration;
  import javax.comm.CommPortIdentifier;
  import javax.comm.SerialPort;
  import javax.comm.SerialPortEvent;
  import javax.comm.SerialPortEventListener;
  
  /**
   *
   * @author IamUsman
   */
  public class ReadingPorts implements SerialPortEventListener, Runnable {
  
      static CommPortIdentifier portId;
      static Enumeration portList;
      static SerialPort port;
      static InputStream inputStream;
      static Thread readThread;
      static byte buffer[];
      static BufferedReader br;
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          // TODO code application logic here
          portList = CommPortIdentifier.getPortIdentifiers();
          while (portList.hasMoreElements()) {
              portId = (CommPortIdentifier) portList.nextElement();
              if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                  if (portId.getName().equals("COM1")) {
                      if (!portId.isCurrentlyOwned()) {
                          ReadingPorts rp = new ReadingPorts();
                      } else {
                          System.out.println("This port is already used by some other program");
                      }
  
                  }
              }
          }
      }
  
      public ReadingPorts() {
          try {
              port = (SerialPort) portId.open("Custom", 500);
              inputStream = port.getInputStream();
              br = new BufferedReader(new InputStreamReader(inputStream));
              System.out.println("** Connected To COM6 **");
              port.addEventListener(this);
              port.notifyOnDataAvailable(true);
              port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
              port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
              port.enableReceiveTimeout(500);
              System.out.println("................................");
              readThread = new Thread(this);
              readThread.start();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  
      public void serialEvent(SerialPortEvent event) {
  
          switch (event.getEventType()) {
  
              case SerialPortEvent.DATA_AVAILABLE:
                  buffer = new byte[8];
                  try {
                      while (inputStream.available() &gt; 0) {
                          int numBytes = inputStream.read(buffer);
                          System.out.println(new String(buffer,0,numBytes));
                      }
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }
  
                  break;
  
          }
  
  
  
  
      }
  
      @Override
      public void run() {
          try {
              System.out.println("In Run");
              Thread.sleep(2000);
          } catch (Exception ex) {
              ex.printStackTrace();;
          }
  
      }
  }
  </code></pre>
  
  <p>Hyper terminal reads correct data.</p>
  
  <p><img src="https://i.stack.imgur.com/jdAqq.png" alt="enter image description here"></p>