title: Get data from COM4 using Javax.comm
body: <p>I am using this code from <a href="http://www.java-samples.com/showtutorial.php?tutorialid=11" rel="nofollow noreferrer">this</a> website but I dont have any output on the console. Can anyone help me what's wrong?</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  import javax.comm.*;
  
  public class SimpleRead implements Runnable, SerialPortEventListener {
      static CommPortIdentifier portId;
      static Enumeration portList;
  
      InputStream inputStream;
      SerialPort serialPort;
      Thread readThread;
  
      public static void main(String[] args) {
          portList = CommPortIdentifier.getPortIdentifiers();
  
          while (portList.hasMoreElements()) {
              portId = (CommPortIdentifier) portList.nextElement();
              if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                   if (portId.getName().equals("COM4")) {
              //                if (portId.getName().equals("/dev/term/a")) {
                      SimpleRead reader = new SimpleRead();
                  }
              }
          }
      }
  
      public SimpleRead() {
          try {
              serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
          } catch (PortInUseException e) {System.out.println(e);}
          try {
              inputStream = serialPort.getInputStream();
          } catch (IOException e) {System.out.println(e);}
      try {
              serialPort.addEventListener(this);
      } catch (TooManyListenersException e) {System.out.println(e);}
          serialPort.notifyOnDataAvailable(true);
          try {
              serialPort.setSerialPortParams(9600,
                  SerialPort.DATABITS_8,
                  SerialPort.STOPBITS_1,
                  SerialPort.PARITY_NONE);
          } catch (UnsupportedCommOperationException e) {System.out.println(e);}
          readThread = new Thread(this);
          readThread.start();
      }
  
      public void run() {
          try {
              Thread.sleep(20000);
          } catch (InterruptedException e) {System.out.println(e);}
      }
  
      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:
              break;
          case SerialPortEvent.DATA_AVAILABLE:
              byte[] readBuffer = new byte[20];
  
              try {
                  while (inputStream.available() &gt; 0) {
                      int numBytes = inputStream.read(readBuffer);
                  }
                  System.out.print(new String(readBuffer));
              } catch (IOException e) {System.out.println(e);}
              break;
          }
      }
  }
  </code></pre>
  
  <p>This is what the output on the console looks like.</p>
  
  <p><a href="https://i.stack.imgur.com/cq1km.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cq1km.png" alt="enter image description here"></a></p>
  
  <p>I am sure that my COM port is 4 and all the other details. I just dont know what is the cause of it being terminated.</p>