Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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
comm.jar通信串行端口java_Java_Serial Port - Fatal编程技术网

comm.jar通信串行端口java

comm.jar通信串行端口java,java,serial-port,Java,Serial Port,我在使用comm.jar时遇到问题 问题是我连接了设备,并使用此代码在池中启动了应用程序 public static void main(String[] args) { Enumeration portList; CommPortIdentifier portId = null; portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMore

我在使用comm.jar时遇到问题

问题是我连接了设备,并使用此代码在池中启动了应用程序

 public static void main(String[] args) {
        Enumeration portList;
        CommPortIdentifier portId = null;
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            System.out.println("port::" + portId.getName());

        }
        while (true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaComPortFinding.class.getName()).log(Level.SEVERE, null, ex);
            }
            main(args);
        }
    }
输出:

port::COM1
port::COM10
在一次轮询之后,我弹出了设备。我仍然得到了如下回应:

port::COM1
port::COM10

有谁能帮我/建议在轮询中获得动态响应。

你可以尝试类似的方法,因为每次都应该重新创建CommPortIdentifier

class TestProgram
{
    public static void main(String[] args)
    {
        while(true)
        {
            try
            {
                Thread.sleep(2000);
            }
            catch(InterruptedException ex)
            {
                Logger.getLogger(TestProgram.class.getName()).log(Level.SEVERE, null, ex);
            }

            scanPorts();
        }
    }

    private static void scanPorts()
    {
        Enumeration portList;
        CommPortIdentifier portId = null;
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            System.out.println("port::" + portId.getName());

        }
    }
}
编辑:

port::COM1
port::COM10

我刚刚在WindowsXPSP3上测试了这个程序,黑莓手机在usb上。当我启动程序时,我会看到黑莓手机的普通COM1和两个COM端口。断开BlackBerry连接后,端口将保留在设备管理器中。如果我手动删除它们,它们将在程序中消失(无需重新启动)。

您可以尝试类似的操作,因为每次都应重新创建CommPortIdentifier

class TestProgram
{
    public static void main(String[] args)
    {
        while(true)
        {
            try
            {
                Thread.sleep(2000);
            }
            catch(InterruptedException ex)
            {
                Logger.getLogger(TestProgram.class.getName()).log(Level.SEVERE, null, ex);
            }

            scanPorts();
        }
    }

    private static void scanPorts()
    {
        Enumeration portList;
        CommPortIdentifier portId = null;
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            System.out.println("port::" + portId.getName());

        }
    }
}
编辑:

port::COM1
port::COM10
我刚刚在WindowsXPSP3上测试了这个程序,黑莓手机在usb上。当我启动程序时,我会看到黑莓手机的普通COM1和两个COM端口。断开BlackBerry连接后,端口将保留在设备管理器中。如果我手动删除它们,它们将在程序中消失(无需重新启动)。

在为类似问题找到解决方案时,我发现上述资源极为宝贵

这里的主要问题是,
CommPortIdentifier
中的静态块只加载一次,并将端口信息缓存在字段变量
portList
中。调用
getPortIdentifiers()
方法时,它将从
portList
返回初始加载期间检测到的端口

解决方法是在
CommPortIdentifier
类中重新加载静态块,然后调用
getPortIdentifiers()
,这将重新加载驱动程序并为您提供COM端口的更新列表(这是在引用的链接中使用Java反射API完成的)

祝你好运

在为类似问题找到解决方案时,我发现上述资源极为宝贵

这里的主要问题是,
CommPortIdentifier
中的静态块只加载一次,并将端口信息缓存在字段变量
portList
中。调用
getPortIdentifiers()
方法时,它将从
portList
返回初始加载期间检测到的端口

解决方法是在
CommPortIdentifier
类中重新加载静态块,然后调用
getPortIdentifiers()
,这将重新加载驱动程序并为您提供COM端口的更新列表(这是在引用的链接中使用Java反射API完成的)



祝你好运

首先,您的应用程序是递归的,这很糟糕。第二,你有没有验证过你的司机是否有车?(在Windows下,您可以在设备管理器>端口下看到)。您是对的@AlexandreLavoie@rajuthoutu以防万一,它的意思是:为什么要在其内部调用
main
方法?您好@AlexandreLavoie,感谢您的响应,在删除设备管理器之后>端口不会被保留。实际上,我正在开发独立的应用程序,在这种情况下,我需要经常检查设备是否已连接(如果已连接,msgs将自动发送给相关人员)。但在第二种情况下,我遇到了不应该出现的故障。真奇怪,我从来没有使用过RXTX与USB串行的时刻,所以我不能帮助你更多。如果再次运行程序,即:连接设备,运行程序(检测到COM10),断开设备连接,运行程序(检测到COM10与否?)@AlexandreLavoie not detected。当它是在池的情况下,只有我面临的问题首先,你的应用程序是递归的,这是坏的。第二,你有没有验证过你的司机是否有车?(在Windows下,您可以在设备管理器>端口下看到)。您是对的@AlexandreLavoie@rajuthoutu以防万一,它的意思是:为什么要在其内部调用
main
方法?您好@AlexandreLavoie,感谢您的响应,在删除设备管理器之后>端口不会被保留。实际上,我正在开发独立的应用程序,在这种情况下,我需要经常检查设备是否已连接(如果已连接,msgs将自动发送给相关人员)。但在第二种情况下,我遇到了不应该出现的故障。真奇怪,我从来没有使用过RXTX与USB串行的时刻,所以我不能帮助你更多。如果再次运行程序,即:连接设备,运行程序(检测到COM10),断开设备连接,运行程序(检测到COM10与否?)@AlexandreLavoie not detected。当它是在池的情况下,只有我面临的问题,我尝试过这件事,但结果是一样的。我有一个疑问,像端口列表是一旦得到初始化,然后直到应用程序是活的得到相同的响应。你同意这一点吗?是的,这似乎是windows的问题。但问题对我来说仍然是一样的。我试着像你一样使用我的设备。但是结果是一样的(仅限windows xp sp3)。@rajuthoutu这方面有什么进展吗?@emecas没有,仍然是一样的。我认为这是dll和comm.jar的问题。你能研究一下吗?我之前尝试过这件事,但结果是一样的。我怀疑端口列表是否在初始化之后,直到应用程序处于活动状态时才得到相同的响应。你同意这一点吗?是的,这似乎是windows的问题。但问题对我来说仍然是一样的。我试着像你一样使用我的设备。但是结果是一样的(仅限windows xp sp3)。@rajuthoutu这方面有什么进展吗?@emecas没有,仍然是一样的。我认为这是dll和comm.jar的问题。你能研究一下吗。