View Javadoc
1   package net.sf.mbus4j;
2   
3   /*
4    * #%L
5    * mbus4j-core
6    * %%
7    * Copyright (C) 2009 - 2014 MBus4J
8    * %%
9    * mbus4j - Drivers for the M-Bus protocol - http://mbus4j.sourceforge.net/
10   * Copyright (C) 2009-2014, mbus4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
29   */
30  /**
31   *
32   * @author aploese
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;// 250
39      public final static byte SLAVE_SELECT_PRIMARY_ADDRESS = (byte) 0xFD; // 253
40      public final static byte BROADCAST_WITH_ANSWER_PRIMARY_ADDRESS = (byte) 0xFE;// 254
41      public final static byte BROADCAST_NO_ANSWER_PRIMARY_ADDRESS = (byte) 0xFF;//255
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; // 32*32 = 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       * convert the man string to 2 byte binary representation
60       *
61       * @param man
62       * @return
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 }