zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package tbDataModel_Dell;
import JNADell.DellJAN;
import Method.HexConvert;
import tbDataModel_Dell.ModbusUtil;
 
@SuppressWarnings("unused")
public class Tools {
        
    
    
    /**
     * ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×é
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStrToBinaryStr(String hexString) {
        
 
        if (hexString==null) {
            return null;
        }
 
        hexString = hexString.replaceAll(" ", "");
 
        int len = hexString.length();
        int index = 0;
 
 
        byte[] bytes = new byte[len / 2];
 
        while (index < len) {
 
            String sub = hexString.substring(index, index + 2);
            bytes[index/2] = (byte)Integer.parseInt(sub,16);
            index += 2;
        }
 
 
        return bytes;
    }
    
    /**
     * ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×鲻ȥ³ý¿Õ¸ñ
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStrToBinaryStr2(String hexString) {
         
        if (hexString==null) {
            return null;
        } 
        
        int len = hexString.length();
        
        int index = 0;
 
 
        byte[] bytes = new byte[len / 2];
 
        while (index < len) {
            String sub = hexString.substring(index, index + 2);
            bytes[index/2] = (byte)Integer.parseInt(sub,16);
            index += 2;
        }
 
 
        return bytes;
    }
    
    
    
     public static final int registersToInt(byte[] bytes) {
            return (
                ((bytes[0] & 0xff) << 24) |
                ((bytes[1] & 0xff) << 16) |
                ((bytes[2] & 0xff) << 8) |
                (bytes[3] & 0xff)
                );
          }//registersToInt
     
     
    /**½«×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®*/ 
     public static String Bytes2HexString(byte[] b) { 
           String ret = ""; 
           for (int i = 0; i < b.length; i++) { 
                 String hex = Integer.toHexString(b[i] & 0xFF); 
                 if (hex.length() == 1) { 
                   hex = '0' + hex; 
                     } 
                 ret += hex.toUpperCase(); 
               }
        return ret;
       }
     
     //assiccת16½øÖÆ
     public static byte[] assictohex(byte[] bytes){
         byte by[]=new byte[4];
         by[0]=0;
         by[1]=0;
         by[2]=(byte) Integer.parseInt(Tools.convertHexToString(Tools.Bytes2HexString(bytes)).substring(0, 2), 16);
         by[3]=(byte) Integer.parseInt(Tools.convertHexToString(Tools.Bytes2HexString(bytes)).substring(2, 4), 16);
         
        return by;
         
     }
 
 
     
     public static String convertHexToString(String hex){
         
          StringBuilder sb = new StringBuilder();
          StringBuilder temp = new StringBuilder();
     
          for( int i=0; i<hex.length()-1; i+=2 ){
     
              //grab the hex in pairs
              String output = hex.substring(i, (i + 2));
              //convert hex to decimal
              int decimal = Integer.parseInt(output, 16);
              //convert the decimal to character
              sb.append((char)decimal);
     
              temp.append(decimal);
          }
     
          return sb.toString();
          }
 
     public static final float registersToFloat(byte[] bytes) {
            return Float.intBitsToFloat((
                ((bytes[0] & 0xff) << 24) |
                ((bytes[1] & 0xff) << 16) |
                ((bytes[2] & 0xff) << 8) |
                (bytes[3] & 0xff)
                ));
          }//
     @SuppressWarnings("unused")
    private static String intToHex(int n) {
            StringBuffer s = new StringBuffer();
            String a;
            char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
            while(n != 0){
                s = s.append(b[n%16]);
                n = n/16;            
            }
            a = s.reverse().toString();
            return a;
        }
     
     //½«·µ»ØµÄÊý¾Ýת»¯Îª10½øÖÆÊý¾Ý
     public static int ormntoint(byte[] bytes){
         int aa=0;
         aa=bytes[0]-48 +(bytes[1]-48)*10+(bytes[2]-48)*100+(bytes[3]-48)*1000;
         return aa;
     }
     
     
     /**¶Á´®¿ÚÇëÇó¶ÁÊý¾Ý°ü
      * @param ref ÆôʼµØÖ·
      * @param lenth Êý¾Ý³¤¶È*/
     public static byte[] ReadData(byte ref ,byte lenth) {
            byte[] buf =new byte[10];
            byte[] buf1 =new byte[6];
            //°üÍ·
            buf[0]=(byte) 0x55;
            //°üÍ·
            buf[1]=(byte) 0xAA;
            
            //Ö¸ÁîÀàÐÍ(²ÎÊý¶Áдģʽ)
            buf1[0]=(byte) 0x03;
            //Êý¾Ý³¤¶È
            buf1[1]=(byte) 0x06;
            
            //¶Áģʽ(¶Áģʽ)
            buf1[2]=(byte) 0x01;
            //ÆðʼµØÖ·
            buf1[3]=(byte) ref; 
            //Êý¾Ý³¤¶È
            buf1[4]=(byte) lenth;
            
            //¹Ì¶¨Öµ(¶ÁģʽÏÂΪ0x00)
            buf1[5]=(byte) 0x00;
            
            for(int i=0;i<buf1.length;i++) {
                buf[i+2]=buf1[i];
            }
            //УÑéÂë
            buf[8]=Jiaoyan.check(buf1)[1];
            buf[9]=Jiaoyan.check(buf1)[0];
            return buf; 
       } 
     
     
     //д´®¿ÚÇëÇó¶ÁÊý¾Ý°ü
     public static byte[] WriteData(byte ref ,byte lenth,int value) {
            byte[] buf =new byte[9+lenth];
            byte[] buf1 =new byte[5+lenth];
            //°üÍ·
            buf[0]=(byte) 0x55;
            //°üÍ·
            buf[1]=(byte) 0xAA;
            //Ö¸ÁîÀàÐÍ
            buf1[0]=(byte) 0x03;
            //Êý¾Ý³¤¶È
            buf1[1]=(byte) ((byte) 5+lenth);
            //¶Áģʽ
            buf1[2]=(byte) 0x02;
            //ÆðʼµØÖ·
            buf1[3]=(byte) ref; 
            //Êý¾Ý³¤¶È
            buf1[4]=(byte) lenth;
            
            //дÈëÖµ
            if(lenth==2) {
                 buf1[5]=ModbusUtil.intToRegisters(value)[2];  
                 buf1[6]=ModbusUtil.intToRegisters(value)[3]; 
            }
           
            
            for(int i=0;i<buf1.length;i++) {
                buf[i+2]=buf1[i];
            }
            //УÑéÂë
            buf[buf.length-2]=Jiaoyan.check(buf1)[1];
            buf[buf.length-1]=Jiaoyan.check(buf1)[0];
            return buf; 
       }
 
       //»ñÈ¡Ëã·¨°æ±¾
    public static int suanfaVersion(){
        String suanfaversion = DellJAN.getSuanfaversion();
        String[] suanfa = suanfaversion.split("\\.");
        return Integer.parseInt(suanfa[2]) ;
    }
     
     
     
     
     
}