whyclj
2020-07-21 1fe34bd9cd779c67beece43b4016e2a0192157c9
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
package com.modbus.util;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
import com.base.ComBase;
import com.base.ComFn;
 
 
public class BaseData {
    public static final int ReadByteCount = 12;
    
    public int transsend;                //·¢ËÍÊÂÎñ±êʶ·û
    public int transreceive;            //½ÓÊÕÊÂÎñ±êʶ·û
    public int protocolident;            //ЭÒé±êʶ·û
    public int datacount;                //Êý¾Ý³¤¶È[±¾×Ö½ÚÏÂÒ»¸öµ½×îºó]
    public int unitident = 0x02;        //µ¥Ôª±êʶ·û[µ±Ç°µ¥Ôª¹Ì¶¨Îª2]
    public int funcode;                    //¹¦ÄÜÂë
    public int addrstart;                //¼Ä´æÆ÷ÆðʼµØÖ·
    public int regcount;                //¼Ä´æÆ÷ÊýÁ¿
    public int bytecount;                //Êý¾ÝÇø×Ö½ÚÊýÁ¿
    public byte[] data;                    //Êý¾ÝÇøÊý¾Ý
    
    public BaseData() {
        
    }
    
    public BaseData(int cmd_code,int regstart,int regcount) {
        this.funcode = cmd_code;
        this.addrstart = regstart;
        this.regcount = regcount;
    }
    
    /**
     * ¹¹Ôì·¢ËÍÊý¾Ý
     * @param cmd_code        ÃüÁîÂë
     * @param addrstart        ¼Ä´æÆ÷ÆðʼµØÖ·
     * @param regcount        ¼Ä´æÆ÷ÊýÁ¿
     * @return
     */
    public byte[] createReadData() {
        ByteBuffer bf = ByteBuffer.allocate(ReadByteCount);
        bf.order(ByteOrder.BIG_ENDIAN);
        bf.putShort(ComBase.changeIntToShort(addrstart));        //½ÓÊÕ±êʶ          ½«¼Ä´æÆ÷ÆðʼµØÖ··ÅÔÚ±êʶÖÐ[2¸ö×Ö½Ú]
        bf.putShort(ComBase.changeIntToShort(0x0000));            //ЭÒé±êʶ·û
        bf.putShort(ComBase.changeIntToShort(0x0006));            //ÏÂÃæ×Ö½Ú³¤¶È
        bf.put(ComBase.changeIntToByte(unitident));                //µ¥Ôª±êʶ·û(É豸ID)
        bf.put(ComBase.changeIntToByte(funcode));                //¹¦ÄÜÂë
        bf.putShort(ComBase.changeIntToShort(addrstart));        //¼Ä´æÆ÷ÆðʼµØÖ·
        bf.putShort(ComBase.changeIntToShort(regcount));
        bf.flip();
        byte[] databyte = new byte[ReadByteCount];
        bf.get(databyte);
        return databyte;
    }
    
    /**
     *     ¹¹Ôìµ¥µãдÈëÊý¾Ý
     * @param value
     * @return
     */
    public byte[] createSingleCollWiriteData(byte[] data) {
        ByteBuffer bf = ByteBuffer.allocate(ReadByteCount+regcount*2+1);
        bf.order(ByteOrder.BIG_ENDIAN);
        bf.putShort(ComBase.changeIntToShort(addrstart));        //½ÓÊÕ±êʶ          ½«¼Ä´æÆ÷ÆðʼµØÖ··ÅÔÚ±êʶÖÐ[2¸ö×Ö½Ú]
        bf.putShort(ComBase.changeIntToShort(0x0000));            //ЭÒé±êʶ·û
        bf.putShort(ComBase.changeIntToShort(7+regcount*2));    //ÏÂÃæ×Ö½Ú³¤¶È
        bf.put(ComBase.changeIntToByte(unitident));                //µ¥Ôª±êʶ·û(É豸ID)
        bf.put(ComBase.changeIntToByte(funcode));                //¹¦ÄÜÂë
        bf.putShort(ComBase.changeIntToShort(addrstart));        //¼Ä´æÆ÷ÆðʼµØÖ·
        bf.put(data);                                            //Ä¿±êÖµ
        bf.flip();
        byte[] databyte = new byte[bf.limit()];
        bf.get(databyte);
        return databyte;
    }
    
    
    /**
     *     ¹¹Ôì¶àµãдÈëÊý¾Ý
     * @param value
     * @return
     */
    public byte[] createWiriteData(byte[] data) {
        ByteBuffer bf = ByteBuffer.allocate(ReadByteCount+regcount*2+1);
        bf.order(ByteOrder.BIG_ENDIAN);
        bf.putShort(ComBase.changeIntToShort(addrstart));        //½ÓÊÕ±êʶ          ½«¼Ä´æÆ÷ÆðʼµØÖ··ÅÔÚ±êʶÖÐ[2¸ö×Ö½Ú]
        bf.putShort(ComBase.changeIntToShort(0x0000));            //ЭÒé±êʶ·û
        bf.putShort(ComBase.changeIntToShort(7+regcount*2));    //ÏÂÃæ×Ö½Ú³¤¶È
        bf.put(ComBase.changeIntToByte(unitident));                //µ¥Ôª±êʶ·û(É豸ID)
        bf.put(ComBase.changeIntToByte(funcode));                //¹¦ÄÜÂë
        bf.putShort(ComBase.changeIntToShort(addrstart));        //¼Ä´æÆ÷ÆðʼµØÖ·
        bf.putShort(ComBase.changeIntToShort(regcount));         //¼Ä´æÆ÷ÊýÁ¿
        bf.put(ComBase.changeIntToByte(regcount*2));            //д×Ö½ÚÊýÁ¿
        bf.put(data);                                            //Ä¿±êÖµ
        bf.flip();
        byte[] databyte = new byte[bf.limit()];
        bf.get(databyte);
        return databyte;
    }
    
    /**
     * ¹¹Ôì°´Å¥¿ØÖÆÊý¾Ý
     * @return
     */
    public static byte[] createButtonByte(int value) {
        ByteBuffer bf = ByteBuffer.allocate(2);
        bf.order(ByteOrder.BIG_ENDIAN);
        bf.putShort(ComBase.changeIntToShort(value));
        bf.flip();
        byte[] b = new byte[2];
        bf.get(b);
        return b;
    }
    
    /**
     * ¹¹ÔìдÈë¶à¸ö¼Ä´æÆ÷Êý¾Ý
     * @param value
     * @return
     */
    public static byte[] createFloatByte(float value) {
        ByteBuffer bf = ByteBuffer.allocate(4);
        bf.order(ByteOrder.BIG_ENDIAN);
        bf.putFloat(value);
        bf.flip();
        byte[] b = new byte[4];
        bf.get(b);
        return b;
    }
    
    /**
     * 
     * @return
     */
    public boolean putByteBuffer(ByteBuffer bf) {
        ByteBuffer tmpbuf = bf;
        if(tmpbuf.limit() < 8) {
            return false;
        }
        tmpbuf.position(0);
        transsend = ComBase.changeShortToInt(bf.getShort());
        protocolident = ComBase.changeShortToInt(bf.getShort());
        datacount = ComBase.changeShortToInt(bf.getShort());
        unitident = ComBase.changeByteToInt(bf.get());
        addrstart = transsend;
        if(unitident != 0x02) {
            return false;
        }
        funcode = ComBase.changeByteToInt(bf.get());
        return true;
    }
    
    
}