当端口存在时,为什么我会收到 Java NoSuchPortException?
下面有以下生产代码,我将其用于新驱动程序。 portName 是 COM4,并且该端口存在于 PC 上(我可以使用超级终端连接到它),那么为什么 Javacomm 会抛出 NoSuchPortException? COM4 在设备管理器中显示良好。也
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
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 usingCommPortIdentifier.addPortName(java.lang.String portName, int portType, CommDriver driver)
.You can set the driver parameter to null to use the default driver.