liurunyu
2024-12-02 7fd3ff6715939467e3850763206d37fac8564e41
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
package com.dy.common.mw.protocol.p206V202404.parse.global;
 
import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
import com.dy.common.mw.protocol.p206V2.ProtocolConstantV206V2;
import com.dy.common.mw.protocol.p206V202404.CommonV202404;
import com.dy.common.mw.protocol.p206V202404.ProtocolConstantV206V202404;
import com.dy.common.util.*;
 
public class GlCreate {
    /**
     * 6字节BCD码(12位行政区划) + 2字节HEX
     * @param rtuAddr 控制器地址
     * @param commandCode 功能码
     * @throws Exception
     */
    public static byte[] createStart2Cd(String rtuAddr, String commandCode) throws Exception {
        CommonV202404 commonV202404 = new CommonV202404() ;
        byte[] bsHead = new byte[ProtocolConstantV206V202404.lenHead2Code] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV206V202404.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = 0 ;//帧长度
 
        index++ ;
        bsHead[index] = ProtocolConstantV206V202404.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = commonV202404.createCtrl((byte)0, (byte)0) ;
 
        index++ ;
        GlCreate.createRtuAddr(rtuAddr, bsHead, index);
        index += 8 ;
 
        ByteUtil.hex2Bytes(commandCode, bsHead, index) ;
        return bsHead ;
    }
 
    /**
     * 生成控制器地址
     * 6字节BCD码(12位行政区划) + 2字节HEX
     * @param rtuAddr
     * @param bs
     * @param index
     * @throws Exception
     */
    public static void createRtuAddr(String rtuAddr, byte[] bs, int index) throws Exception {
        String rtuAddr1 = rtuAddr.substring(0, 12) ;
        String rtuAddr2 = rtuAddr.substring(12) ;
 
        ByteUtil.string2BCD_BE(bs, rtuAddr1, index) ;
        index += 6 ;
 
        Integer rtuAddr2Int = Integer.parseInt(rtuAddr2) ;
        ByteUtilUnsigned.short2Bytes_LE(bs, rtuAddr2Int.shortValue(), index);
    }
    /**
     * 为升级协议
     * @param rtuAddr
     * @param bs
     * @param index
     * @throws Exception
     */
    public static void createRtuAddr4PUg(String rtuAddr, byte[] bs, int index) throws Exception {
        ByteUtil.string2BCD_BE(bs, rtuAddr, index) ;
    }
 
    /**
     * 生成IC卡地址
     * @param icCardAddr
     * @param bs
     * @param index
     * @throws Exception
     */
    public static void createIcCardAddr(String icCardAddr, byte[] bs, int index) throws Exception {
        String s1 = icCardAddr.substring(0, 2) ;
        String s2 = icCardAddr.substring(2, 4) ;
        String s3 = icCardAddr.substring(4, 6) ;
        String s4 = icCardAddr.substring(6, 8) ;
        bs[index++] = (byte) (Integer.parseInt(s1, 16)) ;
        bs[index++] = (byte) (Integer.parseInt(s2, 16)) ;
        bs[index++] = (byte) (Integer.parseInt(s3, 16)) ;
        bs[index++] = (byte) (Integer.parseInt(s4, 16)) ;
    }
 
    /**
     * 生成IC卡编码
     * 6字节BCD码(12位行政区划) + 2字节HEX
     * @param icCardNo
     * @param bs
     * @param index
     * @throws Exception
     */
    public static void createIcCardNo(String icCardNo, byte[] bs, int index) throws Exception {
        String icCardNo1 = icCardNo.substring(0, 12) ;
        String icCardNo2 = icCardNo.substring(12) ;
 
        ByteUtil.string2BCD_BE(bs, icCardNo1, index) ;
        index += 6 ;
 
        Integer icCardNo2Int = Integer.parseInt(icCardNo2) ;
        ByteUtilUnsigned.short2Bytes_LE(bs, icCardNo2Int.shortValue(), index);
    }
 
    public static void createDt(byte[] bs, int index) throws Exception {
        ByteUtil.string2BCD_BE(bs, DateTime.ss(), index) ; ;
        ByteUtil.string2BCD_BE(bs, DateTime.mm(), index + 1) ;
        ByteUtil.string2BCD_BE(bs, DateTime.HH(), index + 2) ;
        ByteUtil.string2BCD_BE(bs, DateTime.dd(), index + 3) ;
        ByteUtil.string2BCD_BE(bs, DateTime.MM(), index + 4) ;
        ByteUtil.string2BCD_BE(bs, DateTime.yy(), index + 5) ;
    }
 
 
    public static void createPw(byte[] bs, int index) throws Exception {
        bs[index] = 0;
        bs[index + 1] = 0;
    }
 
    public static void createTp(byte[] bs, int index) throws Exception {
        ByteUtil.string2BCD_BE(bs, DateTime.ss(), index) ; ;
        ByteUtil.string2BCD_BE(bs, DateTime.mm(), index + 1) ;
        ByteUtil.string2BCD_BE(bs, DateTime.HH(), index + 2) ;
        ByteUtil.string2BCD_BE(bs, DateTime.dd(), index + 3) ;
 
        byte week = DateTime.week_Int().byteValue() ;
        byte month = Byte.parseByte(DateTime.MM()) ;
        if(month < 10){
            bs[index + 4] = (byte)((week << 5) | month) ;
        }else{
            bs[index + 4] = (byte)((week << 5) | 0x10 | (month - 10)) ;
        }
 
        ByteUtil.string2BCD_BE(bs, DateTime.yy(), index + 5) ;
    }
 
    public static void createS2D(byte[] bs, int index) throws Exception {
        ByteUtil.string2BCD_BE(bs, DateTime.ss(), index) ; ;
        ByteUtil.string2BCD_BE(bs, DateTime.mm(), index + 1) ;
        ByteUtil.string2BCD_BE(bs, DateTime.HH(), index + 2) ;
        ByteUtil.string2BCD_BE(bs, DateTime.dd(), index + 3) ;
    }
 
    public static void createLen(byte[] bsNoTail) throws Exception {
        int len = bsNoTail.length - ProtocolConstantV206V1.lenHead2ctrl;
        bsNoTail[ProtocolConstantV206V1.dataLenIndex] = (byte)len ;
    }
 
    public static void createLen4PUg(byte[] bytes) throws Exception {
        int len = bytes.length - 4;
        byte[] bs = ByteUtil.int2BCD_LE(len) ;
        if(bs != null && bs.length == 1){
            bytes[1] = bs[0] ;
        }else if(bs != null && bs.length > 1){
            bytes[1] = bs[0] ;
            bytes[2] = bs[1] ;
        }
    }
 
    public static byte[] createCrcTail(byte[] bsNoTail) throws Exception {
        int crc = new CRC8_for_2_0().CRC8(bsNoTail, ProtocolConstantV206V1.ctrlIndex, bsNoTail.length -1) ;
        byte[] bytes = new byte[ProtocolConstantV206V1.lenTail] ;
        bytes[0] = (byte)crc ;
        bytes[1] = ProtocolConstantV206V1.P_Tail_Byte ;
        return bytes ;
    }
 
    public static byte[] createCrcTail4Ug(byte[] bsNoTail) throws Exception {
        int crc = new CRC16().CRC(bsNoTail, 0, bsNoTail.length -1) ;
        byte[] bytes = new byte[ProtocolConstantV206V2.UG_lenTail] ;
        ByteUtilUnsigned.short2Bytes_BE(bytes, crc, 0);
        bytes[2] = ProtocolConstantV206V2.P_Tail_Byte ;
        return bytes ;
    }
 
    public static void main(String[] args)throws Exception{
        byte[] bs = new byte[6] ;
        createTp(bs, 0) ;
        String hex = ByteUtil.bytes2Hex(bs,true, 0, 6) ;
        System.out.println(hex);
    }
}