autodetect com port in windows

Saw "Investigate method for auto-detecting serial port on Windows (javax.usb?)" on the todo list so I thought I would offer this up. Enjoy :slight_smile:

It uses regedit to export some FTDI looking entries, and rxtx to list the active serial ports and looks at their intersection to see if there is an active ftdi com port. It does not require javax.usb, though that might come in handy someday.

package processing.app;
/**
 * Try to determine the com port connected to the ftdi chip based on the 
 * intersection of the ftdi registry entries and the active com ports
 */
import gnu.io.CommPortIdentifier;

import java.io.*;
import java.util.*;

public class ComPortLocator {

//return the most likely com port, maybe
      public String getComPort() throws RunnerException {
            String retstr = null;
            String userdir = System.getProperty("java.io.tmpdir") + File.separator;
            if (Base.isMacOS()) { // do a mac thing
            } else if (Base.isLinux()) { // do a linux thing
            } else { // do a windows thing
                  String commandSize[] = new String[] { "regedit", "/e",
                              userdir + "tmp.reg",
                              "HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Enum\\FTDIBUS" };
                  String tag = "\"PortName\"=";
                  HashSet r1 = new HashSet();
                  try {
                        Process process = Runtime.getRuntime().exec(commandSize);
                        process.waitFor();
                        BufferedReader r = new BufferedReader(new FileReader(userdir
                                    + "tmp.reg"));
                        String s = trimNulls(r.readLine());
                        while (null != s) {
                              int p = s.indexOf(tag);
                              if (p != -1)
                                    r1.add(s.substring(p + tag.length() + 1,
                                                s.length() - 1));
                              s = trimNulls(r.readLine());
                        }
                        r.close();

                  } catch (Exception e) {
                        throw new RunnerException(e.getMessage());
                  }

                  HashSet r2 = new HashSet();

                  Enumeration portList = CommPortIdentifier.getPortIdentifiers();
                  while (portList.hasMoreElements()) {
                        CommPortIdentifier portId = (CommPortIdentifier) portList
                                    .nextElement();
                        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                              r2.add(portId.getName());
                        }
                  }

                  r1.retainAll(r2);// intersection of live ports and ftdi known ports

                  String[] ports = (String[]) r1.toArray(new String[0]);
                  if(ports.length == 0){
                    throw new RunnerException("No Com port found");
                  }
                  if(ports.length > 1){
                        String c = "";
                        for(int x = 0; x < ports.length; x++)
                              c = c + " " + ports[x];
                        throw new RunnerException("Too many Com ports found: " + c);
                  }
                  retstr = ports[0];
            }
            return retstr;
      }

      public String trimNulls(String s) {//weird null thing going on with regedit
            if (s == null)
                  return null;
            String r = "";
            char[] t = s.toCharArray();
            for (int x = 0; x < t.length; x++) {
                  if (t[x] != 0)
                        r = r + t[x];
            }
            return r;
      }

      public static void main(String[] args) throws Exception { //just for testing
            System.out.println(new ComPortLocator().getComPort());
      }
}

Great!

When I have some time, I'll try to integrate this (and write Mac and Windows versions). It would be really useful.