故事背景:
我们在项目中有一些采集数据的需求,在实现的时候我们实现了一些驱动来进行不同通信协议的适配,包括modbus,TCP等通信协议。在串口modbus(serialModbus)采集过程中,我们发现重新连接一直失败。通过我的研究发现,在jssc中,串口通信并不需要手动进行重连,因此删除了重连代码解决了问题。为了证明串口通信无需重连并且查找重连失败的原因,做了以下记录。
jar包使用:
jamod-2.0.0-SNAPSHOT.jar:这边我们对网络上的jar包进行了挑选,选择了可以使用的,加入我们的maven私服,版本号定位2.0.0.
jssc:jssc-2.6.2.jar:这个版本目前maven服务器中好像也没有,不过没有进行尝试,最新版可能也可以使用。
jssc中连接失败的原因:
关闭端口实现原理
以下为jssc中关闭端口的代码:
public boolean closePort() throws SerialPortException {
this.checkPortOpened("closePort()");
if (this.eventListenerAdded) {
this.removeEventListener();
}
boolean returnValue = this.serialInterface.closePort(this.portHandle);
if (returnValue) {
this.maskAssigned = false;
this.portOpened = false;
}
return returnValue;
}
可以看出关闭接口一共做了两件事:
- 移出listener
- 调用this.serialInterface的关闭功能。
打开端口实现原理
public boolean openPort() throws SerialPortException {
if (this.portOpened) {
throw new SerialPortException(this.portName, "openPort()", "Port already opened");
} else if (this.portName == null) {
throw new SerialPortException(this.portName, "openPort()", "Null not permitted");
} else {
boolean useTIOCEXCL = System.getProperty("JSSC_NO_TIOCEXCL") == null && System.getProperty("JSSC_NO_TIOCEXCL".toLowerCase()) == null;
this.portHandle = this.serialInterface.openPort(this.portName, useTIOCEXCL);
i