简体   繁体   English

comm.jar通讯串口Java

[英]comm.jar communicating serial ports java

I am facing problem with using comm.jar. 我在使用comm.jar时遇到问题。

Problem is I connected the devices and I started the application in pooling using this code 问题是我已连接设备,并使用此代码在池中启动了应用程序

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

output : 输出:

port::COM1
port::COM10

After one polling , I have ejected the device. 经过一轮轮询后,我弹出了设备。 I still get the response as 我仍然得到答复

port::COM1
port::COM10

can anyone help me out / suggest to get dynamic response in polling. 谁能帮助我/建议在轮询中获得动态响应。

You could try something like that since the CommPortIdentifier should be recreated every time. 您可以尝试类似的操作,因为每次都应重新创建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());

        }
    }
}

EDIT : 编辑:

I've just tested the program on Windows XP SP3 with my BlackBerry on usb. 我刚刚使用USB上的BlackBerry在Windows XP SP3上测试了该程序。 When I start the program I see the normal COM1 and two COM ports for the BlackBerry. 启动程序时,我看到了BlackBerry的普通COM1和两个COM端口。 Once I disconnect the BlackBerry, the ports stay in the device manager. 断开BlackBerry后,端口将保留在设备管理器中。 If I remove them manually, they dissapear in the program (without restart). 如果我手动删除它们,它们将消失在程序中(无需重新启动)。

https://community.oracle.com/thread/2063873?start=0&tstart=0 https://community.oracle.com/thread/2063873?start=0&tstart=0

I found the above resource extremely valuable while finding solution for a similar issue. 在寻找类似问题的解决方案时,我发现上述资源极为宝贵。

The main problem here is that static block in CommPortIdentifier gets loaded only once and caches the ports information in field variable portList . 这里的主要问题是CommPortIdentifier中的静态块仅加载一次,并将端口信息缓存在字段变量portList When you call the getPortIdentifiers() method, it will return the ports from portList which were detected during the initial load. 调用getPortIdentifiers()方法时,它将从portList返回在初始加载期间检测到的端口。

The workaround this would be to reload the static block in CommPortIdentifier class and then call getPortIdentifiers() , which will reload the drivers and give you an updated list of COM ports (this was done using Java Reflection API in the link referenced). 解决方法是重新加载CommPortIdentifier类中的静态块,然后调用getPortIdentifiers() ,这将重新加载驱动程序并为您提供COM端口的更新列表(这是在引用的链接中使用Java Reflection API完成的)。

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM