2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.transport.modbus;
15 import org.apache.commons.lang.builder.StandardToStringStyle;
16 import org.apache.commons.lang.builder.ToStringBuilder;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * Implementation for writing coils
22 * @author Sami Salonen - Initial contribution
26 public class ModbusWriteCoilRequestBlueprint extends ModbusWriteRequestBlueprint {
28 private static StandardToStringStyle toStringStyle = new StandardToStringStyle();
31 toStringStyle.setUseShortClassName(true);
34 private final int slaveId;
35 private final int reference;
36 private final BitArray bits;
37 private final boolean writeMultiple;
38 private final int maxTries;
41 * Construct coil write request with single bit of data
43 * @param slaveId slave id to write to
44 * @param reference reference address
45 * @param data bit to write
46 * @param writeMultiple whether to use {@link ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS} over
47 * {@link ModbusWriteFunctionCode.WRITE_COIL}
48 * @param maxTries maximum number of tries in case of errors, should be at least 1
50 public ModbusWriteCoilRequestBlueprint(int slaveId, int reference, boolean data, boolean writeMultiple,
52 this(slaveId, reference, new BitArray(data), writeMultiple, maxTries);
56 * Construct coil write request with many bits of data
58 * @param slaveId slave id to write to
59 * @param reference reference address
60 * @param data bit(s) to write
61 * @param writeMultiple whether to use {@link ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS} over
62 * {@link ModbusWriteFunctionCode.WRITE_COIL}. Useful with single bit of data.
63 * @param maxTries maximum number of tries in case of errors, should be at least 1
64 * @throws IllegalArgumentException in case <code>data</code> is empty, <code>writeMultiple</code> is
65 * <code>false</code> but there are many bits to write.
67 public ModbusWriteCoilRequestBlueprint(int slaveId, int reference, BitArray data, boolean writeMultiple,
70 this.slaveId = slaveId;
71 this.reference = reference;
73 this.writeMultiple = writeMultiple;
74 this.maxTries = maxTries;
76 if (!writeMultiple && bits.size() > 1) {
77 throw new IllegalArgumentException("With multiple coils, writeMultiple must be true");
79 if (bits.size() == 0) {
80 throw new IllegalArgumentException("Must have at least one bit");
83 throw new IllegalArgumentException("maxTries should be positive, was " + maxTries);
88 public int getUnitID() {
93 public int getReference() {
98 public ModbusWriteFunctionCode getFunctionCode() {
99 return writeMultiple ? ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS : ModbusWriteFunctionCode.WRITE_COIL;
102 public BitArray getCoils() {
107 public int getMaxTries() {
112 public String toString() {
113 return new ToStringBuilder(this, toStringStyle).append("slaveId", slaveId).append("reference", reference)
114 .append("functionCode", getFunctionCode()).append("bits", bits).append("maxTries", maxTries).toString();
118 public void accept(ModbusWriteRequestBlueprintVisitor visitor) {