Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve O'Hara committed Sep 13, 2023
2 parents f56dd05 + 7be005f commit 124f572
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.8.5</version>
<version>2.9.3</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public class ModbusSlaveFactory {

private static final Map<String, ModbusSlave> slaves = new HashMap<String, ModbusSlave>();
private static final Map<String, ModbusSlave> slaves = new HashMap<>();

/**
* Prevent instantiation
Expand Down Expand Up @@ -143,7 +143,7 @@ public static synchronized ModbusSlave createUDPSlave(InetAddress address, int p
* @throws ModbusException If a problem occurs e.g. port already in use
*/
public static synchronized ModbusSlave createSerialSlave(SerialParameters serialParams) throws ModbusException {
ModbusSlave slave = null;
ModbusSlave slave;
if (serialParams == null) {
throw new ModbusException("Serial parameters are null");
}
Expand All @@ -152,9 +152,8 @@ else if (ModbusUtil.isBlank(serialParams.getPortName())) {
}

// If we have a slave already assigned to this port
if (slaves.containsKey(serialParams.getPortName())) {
slave = slaves.get(serialParams.getPortName());

slave = getSlave(ModbusSlaveType.SERIAL, serialParams.getPortName());
if (slave != null) {
// Check if any of the parameters have changed
if (!serialParams.toString().equals(slave.getSerialParams().toString())) {
close(slave);
Expand All @@ -165,8 +164,7 @@ else if (ModbusUtil.isBlank(serialParams.getPortName())) {
// If we don;t have a slave, create one
if (slave == null) {
slave = new ModbusSlave(serialParams);
slaves.put(serialParams.getPortName(), slave);
return slave;
slaves.put(ModbusSlaveType.SERIAL.getKey(serialParams.getPortName()), slave);
}
return slave;
}
Expand All @@ -176,40 +174,44 @@ else if (ModbusUtil.isBlank(serialParams.getPortName())) {
*
* @param slave Slave to remove
*/
public static void close(ModbusSlave slave) {
public static synchronized void close(ModbusSlave slave) {
if (slave != null) {
slave.closeListener();
slaves.remove(slave.getType().getKey(slave.getPort()));
if (slave.getType().is(ModbusSlaveType.SERIAL)) {
slaves.remove(slave.getType().getKey(slave.getSerialParams().getPortName()));
} else {
slaves.remove(slave.getType().getKey(slave.getPort()));
}
}
}

/**
* Closes all slaves and removes them from the running list
*/
public static void close() {
for (ModbusSlave slave : new ArrayList<ModbusSlave>(slaves.values())) {
public static synchronized void close() {
for (ModbusSlave slave : new ArrayList<>(slaves.values())) {
slave.close();
}
}

/**
* Returns the running slave listening on the given IP port
* Returns the running slave listening on the given port
*
* @param port Port to check for running slave
* @return Null or ModbusSlave
*/
public static ModbusSlave getSlave(int port) {
return slaves.get(port + "");
public static synchronized ModbusSlave getSlave(ModbusSlaveType type, int port) {
return type == null ? null : slaves.get(type.getKey(port));
}

/**
* Returns the running slave listening on the given serial port
* Returns the running slave listening on the given port
*
* @param port Port to check for running slave
* @return Null or ModbusSlave
*/
public static ModbusSlave getSlave(String port) {
return ModbusUtil.isBlank(port) ? null : slaves.get(port);
public static synchronized ModbusSlave getSlave(ModbusSlaveType type, String port) {
return type == null || ModbusUtil.isBlank(port) ? null : slaves.get(type.getKey(port));
}

/**
Expand All @@ -226,5 +228,4 @@ public static synchronized ModbusSlave getSlave(AbstractModbusListener listener)
}
return null;
}

}
13 changes: 13 additions & 0 deletions src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveType.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ public boolean is(ModbusSlaveType... types) {
public String getKey(int port) {
return toString() + port;
}

/**
* Returns a unique key for this port and type
*
* @param port Port number
* @return Unique key
*/
public String getKey(String port) {
if (ModbusUtil.isBlank(port)) {
throw new IllegalArgumentException("Port must not be null or empty");
}
return this + port;
}
}

0 comments on commit 124f572

Please sign in to comment.