1 package net.sf.mbus4j;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class MBusUtils {
35
36 public final static byte UNCONFIGURED_PRIMARY_ADDRESS = (byte) 0x00;
37 public final static byte FIRST_REGULAR_PRIMARY_ADDRESS = (byte) 0x01;
38 public final static byte LAST_REGULAR_PRIMARY_ADDRESS = (byte) 0xFA;
39 public final static byte SLAVE_SELECT_PRIMARY_ADDRESS = (byte) 0xFD;
40 public final static byte BROADCAST_WITH_ANSWER_PRIMARY_ADDRESS = (byte) 0xFE;
41 public final static byte BROADCAST_NO_ANSWER_PRIMARY_ADDRESS = (byte) 0xFF;
42
43 public static String short2Man(short value) {
44 if (value == -1) {
45 return null;
46 }
47
48 StringBuilder sb = new StringBuilder();
49 sb.append((char) ((value / 1024) + 64));
50 value %= 1024;
51 sb.append((char) ((value / (32)) + 64));
52 value %= 32;
53 sb.append((char) (value + 64));
54
55 return sb.toString();
56 }
57
58
59
60
61
62
63
64 public static short man2Short(String man) {
65 if (man == null) {
66 return -1;
67 } else {
68 byte[] bytes = man.getBytes();
69 return (short) (bytes[2] - 64 + (bytes[1] - 64) * 32 + (bytes[0] - 64) * 1024);
70 }
71 }
72
73 public static long String2Bcd(String value) {
74 long result = 0;
75 for (int i = 0; i < value.length(); i++) {
76 result <<= 4;
77 result |= (byte) (Short.parseShort(value.substring(i, i + 1), 16) & 0x0F);
78 }
79 return result;
80 }
81
82 public static long long2Bcd(long l) {
83 long result = 0;
84 for (int i = 0; i < 16; i++) {
85 result |= (l % 10) << (i * 4);
86 l /= 10;
87 }
88 return result;
89 }
90
91 public static long bcd2Long(long l) {
92 long result = 0;
93 for (int i = 0; i < 16; i++) {
94 result += (long) ((l % 16) * Math.pow(10, i));
95 l >>= 4;
96 }
97 return result;
98 }
99
100 public static int int2Bcd(int i) {
101 int result = 0;
102 for (int idx = 0; idx < 8; idx++) {
103 result |= (i % 10) << (idx * 4);
104 i /= 10;
105 }
106 return result;
107 }
108
109 public static int bcd2Int(int i) {
110 int result = 0;
111 for (int idx = 0; idx < 8; idx++) {
112 result += (int) ((i % 16) * Math.pow(10, idx));
113 i >>= 4;
114 }
115 return result;
116 }
117
118 public static short short2Bcd(short s) {
119 short result = 0;
120 for (int i = 0; i < 4; i++) {
121 result |= (s % 10) << (i * 4);
122 s /= 10;
123 }
124 return result;
125 }
126
127 public static short bcd2Short(short s) {
128 short result = 0;
129 for (int i = 0; i < 4; i++) {
130 result += (short) ((s % 16) * Math.pow(10, i));
131 s >>= 4;
132 }
133 return result;
134 }
135
136 public static byte byte2Bcd(byte b) {
137 byte result = 0;
138 for (int i = 0; i < 2; i++) {
139 result |= (b % 10) << (i * 4);
140 b /= 10;
141 }
142 return result;
143 }
144
145 public static byte bcd2Byte(byte b) {
146 byte result = 0;
147 for (int i = 0; i < 16; i++) {
148 result += (long) ((b % 16) * Math.pow(10, i));
149 b >>= 4;
150 }
151 return result;
152 }
153
154 }