]> git.basschouten.com Git - openhab-addons.git/blob
7b787b02bb61e21b8c1769d3e7091db924c9f19e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.transport.modbus;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 import net.wimpi.modbus.procimg.SimpleInputRegister;
18
19 /**
20  * Basic {@link ModbusRegister} implementation
21  *
22  * @author Sami Salonen - Initial contribution
23  */
24 @NonNullByDefault
25 public class ModbusRegister {
26
27     private final SimpleInputRegister wrapped;
28
29     /**
30      * Constructs a new instance for bytes
31      *
32      * @param b1 the first (hi) byte of the word.
33      * @param b2 the second (low) byte of the word.
34      */
35     public ModbusRegister(byte b1, byte b2) {
36         wrapped = new SimpleInputRegister(b1, b2);
37     }
38
39     /**
40      * Construct register for at
41      *
42      * @param val value representing register data. The <code>int</code> will be downcasted to <code>short</code>.
43      */
44     public ModbusRegister(int val) {
45         wrapped = new SimpleInputRegister(val);
46     }
47
48     /**
49      * Get raw data represented by this register. Since register is 16 bits, array of length 2 will be returned.
50      *
51      * @return byte array of length 2, high byte first.
52      */
53     public byte[] getBytes() {
54         return wrapped.toBytes();
55     }
56
57     /**
58      * Returns the value of this register as integer representing 16 bit data parsed as signed integer.
59      *
60      * @return the register content as unsigned integer
61      */
62     public int getValue() {
63         return wrapped.getValue();
64     }
65
66     /**
67      * Returns the value of this register as integer representing 16 bit data parsed as unsigned integer.
68      *
69      * @return the register content as unsigned integer
70      */
71     public int toUnsignedShort() {
72         return wrapped.toUnsignedShort();
73     }
74
75     @Override
76     public String toString() {
77         StringBuffer buffer = new StringBuffer("ModbusRegisterImpl(");
78         buffer.append("uint16=").append(toUnsignedShort()).append(", hex=");
79         return appendHexString(buffer).append(')').toString();
80     }
81
82     /**
83      * Returns the register value as hex string
84      *
85      * For example, 12 34
86      *
87      * @return string representing the register data
88      */
89     public String toHexString() {
90         StringBuffer buffer = new StringBuffer(5);
91         return appendHexString(buffer).toString();
92     }
93
94     /**
95      * Appends the register value as hex string to the given StringBuffer
96      *
97      */
98     public StringBuffer appendHexString(StringBuffer buffer) {
99         byte[] bytes = getBytes();
100         for (int i = 0; i < 2; i++) {
101             byte b = bytes[i];
102             String byteHex = Long.toHexString(b & 0xff);
103             if ((b & 0xff) < 0x10) {
104                 buffer.append('0');
105             }
106             buffer.append(byteHex);
107             if (i == 0) {
108                 buffer.append(' ');
109             }
110         }
111         return buffer;
112     }
113 }