Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态查找字符串以寻址串行端口_Java_Ruby On Rails_Ruby_Serial Port - Fatal编程技术网

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

Java 动态查找字符串以寻址串行端口,java,ruby-on-rails,ruby,serial-port,Java,Ruby On Rails,Ruby,Serial Port,我有一个用Java开发的应用程序,另一个正在用RubyonRails开发,需要通过串行通信连接到Arduino。虽然我可以根据自己的计算机输入一个字符串,以寻址正确的串行端口,但该字符串甚至会根据我使用的USB端口而变化,这让我认为,用户最好能够从自己计算机列表中扫描的端口中选择一个有效的串行端口,而不是我预定义的端口。是否有人可以使用我的策略让用户扫描他们的计算机上的所有串行端口,并从数组/列表中选择正确的一个,使用Java或Ruby on Rails?来自: 这段代码正是我要寻找的,但是枚举

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

来自:


这段代码正是我要寻找的,但是枚举对象不使用元素填充。getPortIdentifiers方法返回一个空枚举。有什么想法吗?嗯,在这里工作过一段时间。这台笔记本电脑声称有两个并行端口…:$java CommPortLister端口LPT1是一个并行端口:javax.comm。CommPortIdentifier@11d50c0端口LPT2是一个并行端口:javax.comm。CommPortIdentifier@e53b93从Try using打开注册表项HARDWARE\DEVICEMAP\SERIALCOMMI used javaxcomm时出现错误2,当我将RXTXcomm.jar添加到构建路径并将导入从javax.io切换到gnu.io时,这段代码的第一个示例就成功了,再次感谢您的帮助
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);
      }
    }
  }
}