Why am I getting a Java NoSuchPortException when the port exists?

不问归期 提交于 2019-12-20 02:15:13

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/3026804/why-am-i-getting-a-java-nosuchportexception-when-the-port-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!