当端口存在时,为什么我会收到Java NoSuchPortException? [英] Why am I getting a Java NoSuchPortException when the port exists?

查看:156
本文介绍了当端口存在时,为什么我会收到Java NoSuchPortException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有以下生产代码,我将它用于新驱动程序。 portName是COM4,这个端口存在于PC上(我可以通过超级终端连接到它),那么为什么Javacomm会抛出NoSuchPortException呢? COM4在设备mgr中显示正常。

Got the following production code below, I'm using it for a new driver. portName is COM4 and this port exists on the PC (and I can connect to it with hyperterminal), so why does Javacomm throw a NoSuchPortException? COM4 shows up fine in device mgr. too

    final String portName = getSerialPort();
    try {
        final CommPortIdentifier id = CommPortIdentifier.getPortIdentifier(portName);
        port = (SerialPort) id.open(getName(), 1000);
    } catch (NoSuchPortException nspe) {
        report(SeverityCode.LEVEL2, getName(), "PIN Pad is not connected to " + portName + " port, or the port does not exist.");
        return;
    } catch (PortInUseException piue) {
        report(SeverityCode.LEVEL2, getName(), portName + " port is already in-use by some other device. Reason: " + piue.getMessage());
        return;
    }


推荐答案

尝试枚举端口列表使用 CommPortIdentifier.getPortIdentifiers()在您的系统上可用,并打印列出的内容。你确定你在正确的文件夹中安装了jarfiles和dll吗?如果是,则尝试使用 CommPortIdentifier.addPortName(java.lang.String portName,int portType,CommDriver driver)添加COM4。您可以将driver参数设置为null以使用默认驱动程序。

Try enumerating the list of ports that are available on your system using the CommPortIdentifier.getPortIdentifiers() and print what's listed. Are you sure that you installed the jarfiles and dll in the correct folders? If yes, then try adding COM4 using CommPortIdentifier.addPortName(java.lang.String portName, int portType, CommDriver driver) .You can set the driver parameter to null to use the default driver.

这篇关于当端口存在时,为什么我会收到Java NoSuchPortException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆