Skip to content

Commit

Permalink
Keep ModbusMaster open until the api user calls for it to close.
Browse files Browse the repository at this point in the history
  • Loading branch information
JustBru00 committed Jan 26, 2022
1 parent cd09085 commit b959d16
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<groupId>com.rbrubaker</groupId>
<artifactId>multizone4j</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<packaging>jar</packaging>

<name>multizone4j</name>
Expand Down
52 changes: 23 additions & 29 deletions src/main/java/com/rbrubaker/multizone4j/MultiZoneDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* Baud: Can be set on the Bacharach MultiZone device. The default is 19200.
* Parity: No Parity Bit
* Stop Bits: 1 Stop Bit
*
* Make sure to call {@link #disconnect()} when you are done using this class.
* @author Justin Brubaker
*
*/
Expand All @@ -28,6 +30,9 @@ public class MultiZoneDevice {
private int modbusAddress;
private String serialDeviceName;
private int baudRate = 19200;
private ModbusSerialMaster master;
private SerialParameters params;


public MultiZoneDevice(int _modbusAddress, String _serialDeviceName, int _baudRate) {
super();
Expand All @@ -36,6 +41,19 @@ public MultiZoneDevice(int _modbusAddress, String _serialDeviceName, int _baudRa
baudRate = _baudRate;
}

public void disconnect() {
master.disconnect();
}

private void checkAndSetupMaster() throws Exception {
if (master == null) {
params = new SerialParameters(serialDeviceName, baudRate, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
AbstractSerialConnection.FLOW_CONTROL_DISABLED, 8, AbstractSerialConnection.ONE_STOP_BIT, AbstractSerialConnection.NO_PARITY, false);
master = new ModbusSerialMaster(params);
master.connect();
}
}

/**
* Manual Section B.4.1.
* This method is blocks as it has to contact the modbus device
Expand All @@ -50,11 +68,7 @@ public CurrentZoneStatus getCurrentZoneStatus(int zoneNumber) throws ModbusExcep
throw new IllegalArgumentException("The zone number must be between 0-15. The zone number is base 0. Ex. Zone 1 = zoneNumber=0");
}

ModbusSerialMaster master;
SerialParameters params = new SerialParameters(serialDeviceName, baudRate, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
AbstractSerialConnection.FLOW_CONTROL_DISABLED, 8, AbstractSerialConnection.ONE_STOP_BIT, AbstractSerialConnection.NO_PARITY, false);
master = new ModbusSerialMaster(params);
master.connect();
checkAndSetupMaster();

InputRegister[] ppmRegs = master.readMultipleRegisters(modbusAddress, 2001 + zoneNumber, 1);
int ppmZone = ppmRegs[0].getValue();
Expand All @@ -64,8 +78,6 @@ public CurrentZoneStatus getCurrentZoneStatus(int zoneNumber) throws ModbusExcep

CurrentZoneStatus zone = new CurrentZoneStatus(ppmZone, alarmLevel);

master.disconnect();

return zone;
}

Expand All @@ -79,17 +91,11 @@ public CurrentZoneStatus getCurrentZoneStatus(int zoneNumber) throws ModbusExcep
public ArrayList<CurrentZoneStatus> getAllCurrentZoneStatuses() throws ModbusException, Exception {
ArrayList<CurrentZoneStatus> zones = new ArrayList<CurrentZoneStatus>();

ModbusSerialMaster master;
SerialParameters params = new SerialParameters(serialDeviceName, baudRate, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
AbstractSerialConnection.FLOW_CONTROL_DISABLED, 8, AbstractSerialConnection.ONE_STOP_BIT, AbstractSerialConnection.NO_PARITY, false);
master = new ModbusSerialMaster(params);
master.connect();
checkAndSetupMaster();

InputRegister[] ppmRegs = master.readMultipleRegisters(modbusAddress, 2001, 16);
InputRegister[] alarmRegs = master.readMultipleRegisters(modbusAddress, 2017, 16);

master.disconnect();

for (int i = 0; i < 16; i++) {
CurrentZoneStatus zone = new CurrentZoneStatus(ppmRegs[i].getValue(), alarmRegs[i].getValue());
zones.add(zone);
Expand All @@ -106,30 +112,18 @@ public ArrayList<CurrentZoneStatus> getAllCurrentZoneStatuses() throws ModbusExc
* @throws Exception
*/
private int readSingleFunction03RegisterAsInt(int registerNumber) throws ModbusException, Exception {
ModbusSerialMaster master;
SerialParameters params = new SerialParameters(serialDeviceName, baudRate, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
AbstractSerialConnection.FLOW_CONTROL_DISABLED, 8, AbstractSerialConnection.ONE_STOP_BIT, AbstractSerialConnection.NO_PARITY, false);
master = new ModbusSerialMaster(params);
master.connect();
checkAndSetupMaster();

InputRegister[] reg = master.readMultipleRegisters(modbusAddress, registerNumber, 1);

master.disconnect();

return reg[0].getValue();
}

private int readSingleFunction04RegisterAsInt(int registerNumber) throws ModbusException, Exception {
ModbusSerialMaster master;
SerialParameters params = new SerialParameters(serialDeviceName, baudRate, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
AbstractSerialConnection.FLOW_CONTROL_DISABLED, 8, AbstractSerialConnection.ONE_STOP_BIT, AbstractSerialConnection.NO_PARITY, false);
master = new ModbusSerialMaster(params);
master.connect();
checkAndSetupMaster();

InputRegister[] reg = master.readInputRegisters(modbusAddress, registerNumber, 1);

master.disconnect();


return reg[0].getValue();
}

Expand Down

0 comments on commit b959d16

Please sign in to comment.