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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4493279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:36:55  来源:igfitidea点击:

How to get list of available serial ports in my pc using Java?

javaserial-portjavax.comm

提问by Vimal Basdeo

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?

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

My codes:

我的代码:

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());
               }

        }
}

Output : false

输出:假

采纳答案by Kurt Kaylor

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

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

  1. Placed the comm.jarfile in the jre/lib/extdirectory.
  2. Placed the javax.comm.propertiesfile in the jre/libdirectory.
  3. Placed the win32com.dllin the jre/bindirectory.
  1. comm.jar文件放在jre/lib/ext目录中。
  2. javax.comm.properties文件放在jre/lib目录中。
  3. 放置win32com.dlljre/bin目录中。

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

上述每个组件“都应该”在此处可用。

回答by Marcelo Amorim

I'm using ubuntu and my computer does not have any serial/pararel port.

我正在使用 ubuntu,我的电脑没有任何串行/并行端口。

You need to simulate this ports in this case.

在这种情况下,您需要模拟这些端口。

My answer:

我的答案:

serial port identification with java on ubuntu

在ubuntu上使用java进行串口识别