如何使用 Java 获取我的电脑中可用串行端口的列表?

2022-01-18 00:00:00 serial-port java javax.comm

我只是运行一些代码来获取我的计算机上的可用端口列表,当我有 3 个空闲的 com 端口时它返回 false.如何解决这个问题?

I just run some codes to get a list of available ports n my cmputer and it returned me false when I have 3 com ports that are free. How do I solve this prob?

我的代码:

public static void main(String[] args) {
        //SerialParameters params=new SerialParameters();
       // System.out.println(CommPortIdentifier.PORT_SERIAL );
        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println(portList.hasMoreElements());
        while(portList.hasMoreElements()){
            System.out.println("Has more elements");
             CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
               if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
                    System.out.println(portId.getName());
               }
               else{
                     System.out.println(portId.getName());
               }

        }
}

输出:假的

推荐答案

看来您的 javax.comm API 设置可能不正确.确保您已完成以下操作:

It appears your setup of the javax.comm API may not be correct. Make sure you have done the following:

  1. comm.jar文件放到jre/lib/ext目录下.
  2. javax.comm.properties文件放在jre/lib目录下.
  3. win32com.dll放到jre/bin目录下.
  1. Placed the comm.jar file in the jre/lib/ext directory.
  2. Placed the javax.comm.properties file in the jre/lib directory.
  3. Placed the win32com.dll in the jre/bin directory.

上述每个组件都应该"在这里.

Each of the above components "should" be available here.

相关文章