查找字符串以动态地寻址串行端口

我有一个用Java开发的应用程序,还有一个正在开发的Ruby on Rails应用程序,它需要通过串行通信连接到Arduino。 虽然我可以根据自己的计算机输入一个字符串来寻址正确的串口,但字符串也会根据我使用的USB端口而改变,这让我认为用户能够选择有效的串口更好从一个从他们自己的计算机上的列表扫描的,而不是我预定义的。 有没有人有一个策略我可以用来允许用户扫描他们的计算机上的所有串口并从数组/列表中选择正确的一个,无论是在Java还是Ruby on Rails?

从列表端口 :

import java.util.Enumeration; import javax.comm.CommPortIdentifier; /** * List the ports. * * @author Ian F. Darwin, http://www.darwinsys.com/ * @version $Id: CommPortLister.java,v 1.4 2004/02/09 03:33:51 ian Exp $ */ public class CommPortLister { /** Simple test program. */ public static void main(String[] ap) { new CommPortLister().list(); } /** Ask the Java Communications API * what ports it thinks it has. */ protected void list() { // get list of ports available on this particular computer, // by calling static method in CommPortIdentifier. Enumeration pList = CommPortIdentifier.getPortIdentifiers(); // Process the list. while (pList.hasMoreElements()) { CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement(); System.out.print("Port " + cpi.getName() + " "); if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println("is a Serial Port: " + cpi); } else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) { System.out.println("is a Parallel Port: " + cpi); } else { System.out.println("is an Unknown Port: " + cpi); } } } }