查找字符串以动态寻址串行端口 [英] Finding string to address serial port dynamically
问题描述
我有一个用 Java 开发的应用程序,另一个用 Ruby on Rails 开发,它需要通过串行通信连接到 Arduino.虽然我可以根据我自己的计算机输入一个字符串来寻址正确的串行端口,但字符串会根据我使用的 USB 端口而变化,这让我认为用户能够选择一个有效的串行端口会更好从他们自己计算机上的列表中扫描出来的,而不是我预定义的.有没有人有我可以使用的策略来允许用户扫描他们的计算机的所有串行端口并从数组/列表中选择正确的一个,无论是在 Java 还是 Ruby on Rails 中?
I have an application developed in Java, and a second under development in Ruby on Rails, which require connecting to Arduino by serial communication. While I can input a string based on my own computer to address the correct serial port, the string changes depending even on which USB port I use, which makes me think it would be better for the user to be able to select a valid serial port from one that is scanned from a list on their own computers, not one predefined by me. Does anyone have a strategy I can use for allowing the user to scan their computer for all serial ports and select the correct one out of an array/list, either in Java or 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);
}
}
}
}
这篇关于查找字符串以动态寻址串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!