liurunyu
2023-11-27 c475f9ad3290c2593897736144758b54e2b2f407
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
package com.dy.testClient.httpCl;
 
import com.dy.common.mw.protocol.pMeterV1_0_1.ProtocolConstantV1_0_1;
import com.dy.common.util.ByteUtil;
import com.dy.common.util.ByteUtilUnsigned;
import com.dy.common.util.CreateRadom;
import com.dy.common.util.DateTime;
 
public class RtuData4MeterV1_0 {
    public byte[] create(String imei) throws Exception{
        /*
         * 68
         * 0051
         * 01
         * 0300000000000001
         * 12233445566778
         * 01
         * 221128152107
         * 0001E240
         * 000004D2
         * 24
         * 0129
         * 11
         * 002D
         * 0035
         * 003D
         * 006E
         * 0038
         * 00C3
         * 00AC
         * 004C
         * 0028
         * 0081
         * 001B
         * 005E
         * 003D
         * 0063
         * 00AB
         * 0071
         * 00BB
         * 0046
         * 004A
         * 009B
         * 0087
         * 00BE
         * 0022
         * 00B0
         * DA
         * 16
         */
        int len = 89 ;
        byte[] bs = new byte[len] ;
        bs[0] = ProtocolConstantV1_0_1.P_Head_Byte ;
        ByteUtilUnsigned.short2Bytes_BE(bs, len, 1);//长度
        bs[3] = (byte)101 ;//协议版本号
        while(true){
            if(imei.length() >= 16){
                break ;
            }else{
                imei = "0" + imei ;
            }
        }
        ByteUtil.string2BCD_BE(bs, imei, 4) ;//IMEI号
        
        String meterNo = "" + (Long.valueOf(imei) + 1);
        while(true){
            if(meterNo.length() >= 16){
                break ;
            }else{
                meterNo = "0" + meterNo ;
            }
        }
        ByteUtil.string2BCD_BE(bs, meterNo, 12) ;//水表号
        
        bs[20] = 1 ;//功能码
        
        ByteUtil.string2BCD_BE(bs, DateTime.yyMMddhhmmss(), 21) ;//时间
        
        ByteUtilUnsigned.int2Bytes_BE(bs, 123456, 27);//实时累计水量
        
        ByteUtilUnsigned.int2Bytes_BE(bs, 54321, 31);//日累计水量
        
        bs[35] = (byte)36;//电池电压
        
        ByteUtilUnsigned.short2Bytes_BE(bs, 297, 36);//信号强度
        
        byte sta = 0 ;
        int num = CreateRadom.radom_4() ;
        if(num%2 == 0){
            sta += 1 ;//阀门状态
        }
        num = CreateRadom.radom_4() ;
        if(num%2 == 0){
            sta += 4 ;//阀门故障
        }
        num = CreateRadom.radom_4() ;
        if(num%2 == 0){
            sta += 8 ;//强磁
        }
        num = CreateRadom.radom_4() ;
        if(num%2 == 0){
            sta += 16 ;//电池欠压
        }
        bs[38] = sta;//表状态
        
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 39);//1点小时用水量
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 41);//2点小时用水量
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 43);//3
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 45);//4 
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 47);//5
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 49);//6
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 51);//7
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 53);//8
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 55);//9
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 57);//10
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 59);//11
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 61);//12
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 63);//13
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 65);//14
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 67);//15
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 69);//16
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 71);//17
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 73);//18
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 75);//19
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 77);//20
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 79);//21
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 81);//22
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 83);//23
        num = CreateRadom.radom(200, 10) ;
        ByteUtilUnsigned.short2Bytes_BE(bs, num, 85);//24
        
        byte he = 0 ;
        for(byte i = 0 ; i <= 86; i++){
            he = (byte)(he + bs[i]) ;
        }
        ByteUtilUnsigned.byte2Byte(bs, he, 87) ;
        bs[88] = ProtocolConstantV1_0_1.P_Tail_Byte ;
        
        return bs ;
    }
 
}