以下是生产代码,我正在为一个新的驱动程序使用它。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;
}