2 * Copyright (c) 2010-2023 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.binding.modbus.e3dc.internal.dto;
15 import java.nio.charset.StandardCharsets;
16 import java.util.BitSet;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.io.transport.modbus.ModbusBitUtilities;
20 import org.openhab.core.io.transport.modbus.ValueBuffer;
23 * The {@link DataConverter} Helper class to convert bytes from modbus into desired data format
25 * @author Bernd Weymann - Initial contribution
28 public class DataConverter {
31 * Get double value from 2 bytes with correction factor
36 public static double getUDoubleValue(ValueBuffer wrap, double factor) {
37 return round(wrap.getUInt16() * factor, 2);
40 public static String getString(byte[] bArray) {
41 return ModbusBitUtilities.extractStringFromBytes(bArray, 0, bArray.length, StandardCharsets.US_ASCII).trim();
44 public static int toInt(BitSet bitSet) {
46 for (int bit = 0; bit < bitSet.length(); bit++) {
47 if (bitSet.get(bit)) {
48 intValue |= (1 << bit);
54 public static double round(double value, int places) {
56 throw new IllegalArgumentException();
59 long factor = (long) Math.pow(10, places);
60 long tmp = Math.round(value * factor);
61 return (double) tmp / factor;