title: Detecting serial COM port using Java
body: <p>My Java program is failing to detect the serial port device connected to the computer.</p>
  
  <p>I am using a Windows 7 (64-bit) computer ().</p>
  
  <p>The serial port device is a GPS module which outputs text GPS strings at 37860 baud,  connected to a Serial-USB converter plugged into the laptop USB port.</p>
  
  <p>The Windows control panel/device manager shows this device as COM7.</p>
  
  <p>The GPS application "MiniGPS" running on the computer is able to communicate in both directions with the GPS chip as COM7.</p>
  
  <p>An application in Processing is able to detect that the computer has 2 serial ports (com3 and com7,  com3 is the mouse).</p>
  
  <p>I want to write an application in Java (or C++)  to collect the data from the serial port, from the GPS.</p>
  
  <p>I copied the following code (below) from somewhere,  not sure where now,  the same or similar code appears in a bunch of other questions here already.</p>
  
  <p>When I run this, the <em>portList</em> enumeration comes back empty,  it can't find any serial port at all.</p>
  
  <p>I know only one program can see the serial port at a time,  I shut down the device manager app,  the GPS app and the Processing app before I tried this, still no serial port gets identified by the <em>CommPortIdentifier</em> object.</p>
  
  <p>Any suggestions ?</p>
  
  <pre><code>public class SimpleRead implements Runnable, SerialPortEventListener {
  static CommPortIdentifier portId;
  static Enumeration portList;
  
  InputStream inputStream;
  SerialPort serialPort;
  Thread readThread;
  
  public static void main(String[] args) 
  {
      System.out.println("Hello World");
      portList = CommPortIdentifier.getPortIdentifiers();
  
      JOptionPane.showMessageDialog(null,"portlist :"+portList.hasMoreElements());
      int numelt=0 ;
      while (portList.hasMoreElements()) 
      {
          numelt++;
          portId = (CommPortIdentifier) portList.nextElement();
  </code></pre>