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

发布于 2024-10-08 04:53:35 字数 865 浏览 0 评论 0原文

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

我的代码:

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

        }
}

输出: 错误的

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀梦 2024-10-15 04:53:35

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

  1. comm.jar 文件放置在 jre/lib/ext 目录中。
  2. javax.comm.properties 文件放置在 jre/lib 目录中。
  3. win32com.dll 放置在 jre/bin 目录中。

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

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

  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.

哎呦我呸! 2024-10-15 04:53:35

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

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

我的答案:

ubuntu上使用java进行串口识别

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

You need to simulate this ports in this case.

My answer:

serial port identification with java on ubuntu

久随 2024-10-15 04:53:35

如果你碰巧做了我天真所做的事情,我还有一个额外的答案。

我只是忘记将我的用户添加到 dialout 组,结果是找不到串口,而我的 /dev/ttyUSB0 被正确转发到我的 VirtualBox 实例Lubuntu 机器。

I have an additional answer, if you happen to do what I naively did.

I just forgot to add my user to the dialout group, and the result was to find no serial ports, while my /dev/ttyUSB0 was forwarded correctly to my VirtualBox instance of a Lubuntu machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文