Skip to content

Commit

Permalink
Tested with a fake modbus slave. Current code works.
Browse files Browse the repository at this point in the history
Waiting on Bacharach MultiZone detector to come back to the shop for
actual testing.
  • Loading branch information
JustBru00 committed Jan 3, 2022
1 parent f73edaf commit 4efc270
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/com/rbrubaker/multizone4j/MultiZoneDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@

/**
* This class represents a single Bacharach MultiZone device.
* This class assumes the following default Bacharach MultiZone modbus communication parameters.
* Mode: RTU
* Baud: 19200
* Parity: No Parity Bit
* Stop Bits: 1 Stop Bit
* @author Justin Brubaker
*
*/
public class MultiZoneDevice {

private int modbusAddress;
private String serialDeviceName;

public MultiZoneDevice(int _modbusAddress) {
public MultiZoneDevice(int _modbusAddress, String _serialDeviceName) {
super();
modbusAddress = _modbusAddress;
serialDeviceName = _serialDeviceName;
}

/**
Expand All @@ -32,11 +39,11 @@ public MultiZoneDevice(int _modbusAddress) {
*/
public CurrentZoneStatus getCurrentZoneStatus(int zoneNumber) throws ModbusException, Exception, IllegalArgumentException {
if (zoneNumber < 0 || zoneNumber > 15) {
throw new IllegalArgumentException("The zone number must be between 0-15");
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("/dev/serial1", 19200, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
SerialParameters params = new SerialParameters(serialDeviceName, 19200, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
AbstractSerialConnection.FLOW_CONTROL_DISABLED, 8, AbstractSerialConnection.ONE_STOP_BIT, AbstractSerialConnection.NO_PARITY, false);
master = new ModbusSerialMaster(params);
master.connect();
Expand All @@ -47,7 +54,7 @@ public CurrentZoneStatus getCurrentZoneStatus(int zoneNumber) throws ModbusExcep
InputRegister[] alarmRegs = master.readMultipleRegisters(modbusAddress, 2017 + zoneNumber, 1);
int alarmLevel = alarmRegs[0].getValue();

CurrentZoneStatus zone = new CurrentZoneStatus(ppmZone, alarmLevel);
CurrentZoneStatus zone = new CurrentZoneStatus(ppmZone, alarmLevel);

master.disconnect();

Expand All @@ -64,7 +71,7 @@ public ArrayList<CurrentZoneStatus> getAllCurrentZoneStatuses() throws ModbusExc
ArrayList<CurrentZoneStatus> zones = new ArrayList<CurrentZoneStatus>();

ModbusSerialMaster master;
SerialParameters params = new SerialParameters("/dev/serial1", 19200, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
SerialParameters params = new SerialParameters(serialDeviceName, 19200, AbstractSerialConnection.FLOW_CONTROL_DISABLED,
AbstractSerialConnection.FLOW_CONTROL_DISABLED, 8, AbstractSerialConnection.ONE_STOP_BIT, AbstractSerialConnection.NO_PARITY, false);
master = new ModbusSerialMaster(params);
master.connect();
Expand All @@ -74,7 +81,7 @@ public ArrayList<CurrentZoneStatus> getAllCurrentZoneStatuses() throws ModbusExc

master.disconnect();

for (int i = 0; i > 16; i++) {
for (int i = 0; i < 16; i++) {
CurrentZoneStatus zone = new CurrentZoneStatus(ppmRegs[i].getValue(), alarmRegs[i].getValue());
zones.add(zone);
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/rbrubaker/multizone4j/TestModbusRead.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.rbrubaker.multizone4j;

import com.ghgande.j2mod.modbus.ModbusException;

public class TestModbusRead {

public static void main(String[] args) throws IllegalArgumentException, ModbusException, Exception {
// This object represents a Bacharach MultiZone device on the modbus serial network.
// /dev/serial1 - This is the default RS485 port name on the Comfile PI
MultiZoneDevice one = new MultiZoneDevice(1, "/dev/serial1");

// Read the current ppm and alarm status for zone 1.
CurrentZoneStatus single = one.getCurrentZoneStatus(0);
System.out.println(String.format("Single Read - Zone 1: %s ppm - %s alarm status", single.getPPM() + "", single.getAlarmStatus() + ""));

int address = 1;
for (CurrentZoneStatus status : one.getAllCurrentZoneStatuses()) {
System.out.println(String.format("Zone %s: %s ppm - %s alarm status", address + "", status.getPPM() + "", status.getAlarmStatus() + ""));
address++;
}


}

}

0 comments on commit 4efc270

Please sign in to comment.