From b959d163d1b4c23fa5dc6939d2cfe54d076686f6 Mon Sep 17 00:00:00 2001 From: JustBru00 Date: Wed, 26 Jan 2022 09:27:59 -0500 Subject: [PATCH] Keep ModbusMaster open until the api user calls for it to close. --- pom.xml | 2 +- .../multizone4j/MultiZoneDevice.java | 52 ++++++++----------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 6216e46..68978b0 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ com.rbrubaker multizone4j - 0.1.0 + 0.2.0 jar multizone4j diff --git a/src/main/java/com/rbrubaker/multizone4j/MultiZoneDevice.java b/src/main/java/com/rbrubaker/multizone4j/MultiZoneDevice.java index 84e9f37..e7bac99 100644 --- a/src/main/java/com/rbrubaker/multizone4j/MultiZoneDevice.java +++ b/src/main/java/com/rbrubaker/multizone4j/MultiZoneDevice.java @@ -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 * */ @@ -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(); @@ -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 @@ -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(); @@ -64,8 +78,6 @@ public CurrentZoneStatus getCurrentZoneStatus(int zoneNumber) throws ModbusExcep CurrentZoneStatus zone = new CurrentZoneStatus(ppmZone, alarmLevel); - master.disconnect(); - return zone; } @@ -79,17 +91,11 @@ public CurrentZoneStatus getCurrentZoneStatus(int zoneNumber) throws ModbusExcep public ArrayList getAllCurrentZoneStatuses() throws ModbusException, Exception { ArrayList zones = new ArrayList(); - 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); @@ -106,30 +112,18 @@ public ArrayList 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(); }