15832144755
2021-08-27 3516f02277035cdc7ff137422709b16efe97ca4a
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
package com.hxzkoa.udp;
 
@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; 
       }
}