zhubaomin
2024-11-22 44180ee779c038ad628fcd847a0b8e2ca9b6f4ee
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
package com.dy.simRtu.tcpClient.upData;
 
import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate;
import com.dy.common.util.ByteUtil;
import org.apache.mina.core.session.IoSession;
 
/**
 * @Author: liurunyu
 * @Date: 2024/7/15 14:20
 * @Description
 */
public class UpData {
 
    protected static IoSession session ;
 
    private static Object synObj = new Object() ;
 
    public static void setSession(IoSession session){
        UpData.session = session ;
    }
 
    protected static void upSend(byte[] bs) throws Exception{
        synchronized (synObj){
            session.write(bs) ;
        }
    }
 
    protected static byte[] creatHead(String rtuAddr, String code, byte ctrl)throws Exception {
        byte[] bsHead = new byte[ProtocolConstantV206V1.lenHead2Code] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV206V1.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = 0 ;//帧长度
 
        index++ ;
        bsHead[index] = ProtocolConstantV206V1.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = ctrl; //控制域功能码
 
        index++ ;
        GlCreate.createRtuAddr4P206(rtuAddr, bsHead, index);
        index += 5 ;
 
        ByteUtil.hex2Bytes(code, bsHead, index) ;
 
        return bsHead ;
    }
 
    protected static byte[] creatHead4Upgrade(String rtuAddr, String preCode, String sufCode)throws Exception {
        byte[] bsHead = new byte[12] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV206V1.UG_P_Head_Byte ;
 
        index++ ;
        bsHead[index] = 0 ;//帧长度
        index++ ;
        bsHead[index] = 0 ;//帧长度
 
        index++ ;
        bsHead[index] = ProtocolConstantV206V1.UG_P_Head_Byte ;
 
        index++ ;
        bsHead[index] = ByteUtil.hex2Bytes(preCode)[0]; //功能码域
 
        index++ ;
        bsHead[index] = ByteUtil.hex2Bytes(sufCode)[0]; //功能码域
 
        index++ ;
        GlCreate.createRtuAddr4P206(rtuAddr, bsHead, index);
 
        return bsHead ;
    }
}