Skip to content

Commit

Permalink
Modbus Serial new event to serial disconnected (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
caiokrauel authored Mar 11, 2024
1 parent b8c6060 commit 8e599f5
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ public void beforeResponseRead(AbstractSerialConnection port) {
*/
public void afterResponseRead(AbstractSerialConnection port, ModbusResponse res) {
}

/**
* Called whenever a device is disconnected
* @param port Port being used
*/
public void disconnected(AbstractSerialConnection port) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ private void notifyListenersAfterWrite(ModbusMessage msg) {
}
}

/**
* Calls any listeners with the given event and current port
*/
public void notifyListenersDisconnected() {
synchronized (listeners) {
for (AbstractSerialTransportListener listener : listeners) {
listener.disconnected(commPort);
}
}
}

/**
* <code>setCommPort</code> sets the comm port member and prepares the input
* and output streams to be used for reading from and writing to.
Expand Down
70 changes: 38 additions & 32 deletions src/main/java/com/ghgande/j2mod/modbus/net/SerialConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public AbstractModbusTransport getModbusTransport() {

@Override
public synchronized void open() throws IOException {
boolean reopen = serialPort != null;

if (serialPort == null) {
serialPort = SerialPort.getCommPort(parameters.getPortName());
if (serialPort.getDescriptivePortName().contains("Bad Port")) {
Expand All @@ -98,25 +100,26 @@ public synchronized void open() throws IOException {
}
}
serialPort.closePort();
applyConnectionParameters();

if (Modbus.SERIAL_ENCODING_ASCII.equals(parameters.getEncoding())) {
transport = new ModbusASCIITransport();
}
else if (Modbus.SERIAL_ENCODING_RTU.equals(parameters.getEncoding())) {
transport = new ModbusRTUTransport();
}
else {
transport = new ModbusRTUTransport();
logger.warn("Unknown transport encoding [{}] - reverting to RTU", parameters.getEncoding());
applyConnectionParameters();

if (!reopen) {
if (Modbus.SERIAL_ENCODING_ASCII.equals(parameters.getEncoding())) {
transport = new ModbusASCIITransport();
}
else if (Modbus.SERIAL_ENCODING_RTU.equals(parameters.getEncoding())) {
transport = new ModbusRTUTransport();
}
else {
transport = new ModbusRTUTransport();
logger.warn("Unknown transport encoding [{}] - reverting to RTU", parameters.getEncoding());
}
transport.setEcho(parameters.isEcho());
transport.setTimeout(timeout);

// Open the input and output streams for the connection. If they won't
// open, close the port before throwing an exception.
transport.setCommPort(this);
}
transport.setEcho(parameters.isEcho());
transport.setTimeout(timeout);

// Open the input and output streams for the connection. If they won't
// open, close the port before throwing an exception.
transport.setCommPort(this);

// Open the port so that we can get it's input stream
int attempts = 0;
while (!serialPort.openPort(parameters.getOpenDelay()) && attempts < CONNECT_RETRIES) {
Expand All @@ -137,20 +140,23 @@ else if (Modbus.SERIAL_ENCODING_RTU.equals(parameters.getEncoding())) {
throw new IOException(String.format("Port [%s] cannot be opened after [%d] attempts - valid ports are: [%s]", parameters.getPortName(), attempts, portList));
}
inputStream = serialPort.getInputStream();

serialPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_PORT_DISCONNECTED;
}

@Override
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED) {
serialPort.closePort();
}
}
});

if (!reopen) {
serialPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_PORT_DISCONNECTED;
}

@Override
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED) {
serialPort.closePort();
transport.notifyListenersDisconnected();
}
}
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ public void afterResponseRead(AbstractSerialConnection port, ModbusResponse res)
assertEquals("After response message has been read from port", 3, step);
step++;
}

@Override
public void disconnected(AbstractSerialConnection port) {
assertEquals("Device is disconnected", 3, step);
step++;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public void afterResponseRead(AbstractSerialConnection port, ModbusResponse res)
assertEquals("After response message has been read from port", 3, step);
step++;
}

@Override
public void disconnected(AbstractSerialConnection port) {
assertEquals("Device is disconnected", 3, step);
step++;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,11 @@ public void afterResponseRead(AbstractSerialConnection port, ModbusResponse res)
assertEquals("After response message has been read from port", 3, step);
step++;
}

@Override
public void disconnected(AbstractSerialConnection port) {
assertEquals("Device is disconnected", 3, step);
step++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public void afterResponseRead(AbstractSerialConnection port, ModbusResponse res)
assertEquals("After response message has been read from port", 3, step);
step++;
}

@Override
public void disconnected(AbstractSerialConnection port) {
assertEquals("Device is disconnected", 3, step);
step++;
}
}

}

0 comments on commit 8e599f5

Please sign in to comment.