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 java.util.stream.Stream;
17 import org.eclipse.jdt.annotation.NonNull;
19 import net.wimpi.modbus.Modbus;
22 * Modbus write function codes supported by this transport
24 * @author Sami Salonen - Initial contribution
26 public enum ModbusWriteFunctionCode {
27 WRITE_COIL(Modbus.WRITE_COIL),
28 WRITE_MULTIPLE_COILS(Modbus.WRITE_MULTIPLE_COILS),
29 WRITE_SINGLE_REGISTER(Modbus.WRITE_SINGLE_REGISTER),
30 WRITE_MULTIPLE_REGISTERS(Modbus.WRITE_MULTIPLE_REGISTERS);
32 private final int functionCode;
34 ModbusWriteFunctionCode(int code) {
39 * Get numeric function code represented by this instance
43 public int getFunctionCode() {
48 * Construct {@link ModbusWriteFunctionCode} from the numeric function code
50 * @param functionCode numeric function code
51 * @return {@link ModbusWriteFunctionCode} matching the numeric function code
52 * @throws IllegalArgumentException with unsupported functions
54 @SuppressWarnings("null")
55 public static @NonNull ModbusWriteFunctionCode fromFunctionCode(int functionCode) throws IllegalArgumentException {
56 return Stream.of(ModbusWriteFunctionCode.values()).filter(v -> v.getFunctionCode() == functionCode).findFirst()
57 .orElseThrow(() -> new IllegalArgumentException("Invalid functionCode"));