当端口存在时,为什么我会收到Java NoSuchPortException?

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

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以使用默认驱动程序。