9度通讯程序适用于9度多组设备
Administrator
2023-12-27 c736aaea5522e295b42676f094d313e7f6a4afb4
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
package com.dev.fbs9009;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Calendar;
import java.util.Date;
 
import com.base.ComBase;
import com.base.Crc16;
import com.dev.fbs9100.ComFn;
 
public class BaseData {
    public final int BYTE_LEN_03 = 8;
    public final int BYTE_LEN_10 = 9;
    
    private int dev_addr=255;                //É豸µØÖ·
    private int ByteCount;                //¶ÁÈ¡¼Ä´æÆ÷ÊýÁ¿
    public int CMD;                        //ÃüÁî
    public int RES_Index;                //Êý¾ÝÆðʼµØÖ· 
    
    
    public LD_param ld_param;
    
    public ConcentratorParam concentratorparam;        //»ã¼¯Æ÷²ÎÊý
    private int CRC;            //CRCУÑéÂë
    
    public void makeCmd(int res_index, int cmd, int bytecount)
    {
        //this.dev_addr = dev_addr;
        this.ByteCount = bytecount;
        this.CMD = cmd;
        this.RES_Index = res_index;
    }
    
    //¶ÁÈ¡»ñÈ¡µÄÊý¾Ý
    public boolean putByteBuffer(final ByteBuffer bf)
    {
        ByteBuffer tmpbuf = bf;
        int crc0 = tmpbuf.getShort(bf.limit() - 2)& 0xFFFF;
        int crc1 = Crc16.CalCRC16(tmpbuf, bf.limit()-2)& 0xFFFF;
        
        if(crc0 != crc1) {
            //System.err.println("crc0:"+crc0+"\t crc1:"+crc1);
            return false;
        }
        tmpbuf.position(0);    
        int dev_real_addr = ComBase.changeByteToInt(tmpbuf.get());            //É豸µÄʵ¼ÊµØÖ·
        this.CMD = ComBase.changeByteToInt(tmpbuf.get());                    //ÃüÁî¶ÔÓ¦µÄ¶ÁдÃüÁî
        this.RES_Index = ComBase.changeShortToInt(tmpbuf.getShort());        //»ñÈ¡¼Ä´æÆ÷µÄÆðʼλÖÃ
        this.ByteCount = ComBase.changeByteToInt(tmpbuf.get());                //Êý¾ÝÇø×Ö½ÚÊý
        //tmpbuf.position(0);    
        //this.dev_addr = FBS9100_ComBase.changeShortToInt(tmpbuf.get());
        //CMD = FBS9100_ComBase.changeByteToInt(tmpbuf.get());
        
        //CRC = FBS9100_ComBase.changeShortToInt(tmpbuf.getShort(bf.limit()-2));        
        //tmpbuf.compact();
        //tmpbuf.flip();        
        return true;
    }
    
    //½«¶ÁÈ¡µÄÊý¾Ýת³ÉByteBufferÊý¾Ý
    public ByteBuffer getByteBuffer()
    {   ByteBuffer bytebuffer = ByteBuffer.allocate(0);;
        if(CMD == MyModBusCom.CMD_READ_MODBUS){
            bytebuffer=getByteBuffer_03();
        }else if(CMD == MyModBusCom.CMD_MULTI_WRITE_MODBUS){
            bytebuffer=getByteBuffer_10();
        }
        return bytebuffer;
    }
    //½«¶ÁÈ¡µÄÊý¾Ýת³ÉByteBufferÊý¾Ý£¨03ÃüÁ
    public ByteBuffer getByteBuffer_03()
    {
        ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN_03);
 
        bytebuffer.put(ComBase.changeIntToByte(this.dev_addr));
        bytebuffer.put(ComBase.changeIntToByte(CMD));
        bytebuffer.putShort(ComBase.changeIntToShort(RES_Index));
        bytebuffer.putShort(ComBase.changeIntToShort(this.ByteCount));
        
        CRC = Crc16.CalCRC16(bytebuffer, bytebuffer.position());
        bytebuffer.putShort((short)CRC);
        bytebuffer.flip();
        
        return bytebuffer;
    }
    //½«¶ÁÈ¡µÄÊý¾Ýת³ÉByteBufferÊý¾Ý(10ÃüÁî)
    public ByteBuffer getByteBuffer_10()
    {   
        ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN_10+ByteCount*2);
        bytebuffer.put(ComBase.changeIntToByte(this.dev_addr));
        bytebuffer.put(ComBase.changeIntToByte(this.CMD));
        bytebuffer.putShort(ComBase.changeIntToShort(this.RES_Index));
        
        bytebuffer.putShort(ComBase.changeIntToShort(this.ByteCount));        
        bytebuffer.put(ComBase.changeIntToByte(this.ByteCount*2));
        if(RES_Index == MyModBusCom.LD_PARM_ADDR){
            System.out.println(this.ld_param);
            //ÉèÖÃÉ豸µÄ·Åµç²ÎÊýÖµ
            bytebuffer.putShort(ComBase.changeIntToShort(ld_param.getParam_teststart_batt_num()));
            bytebuffer.putShort(ComBase.changeIntToShort(ld_param.getParam_cap()));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_discharge_curr()*1000)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_mon_curr_low()*1000)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_charge_curr()*1000)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_mon_curr_high()*1000)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_online_vol()*100)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_charge_curr_low()*1000)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_charge_cap_low()));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_online_vol_recover()*100)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_module_address()));
            bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_junheng_model()));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_cap_low()*10)));
            bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_datasource()));
            bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_res_args()));
            bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_tmp_high()));
            bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_res_high()*10)));
            System.out.println((int)(ld_param.getParam_cap_low()*10));
        }else if(RES_Index==MyModBusCom.LD_CMD_START_TEST_ADDR||RES_Index==MyModBusCom.LD_CMD_STOP_TEST_ADDR 
                || RES_Index==MyModBusCom.LD9_CMD_STARTRESTEST_ARRR || RES_Index==MyModBusCom.LD9_CMD_STOPRESTEST_ADDR
                || RES_Index==MyModBusCom.LD9_CMD_RESTART_ADDR){
            System.out.println("Æô¶¯»òÍ£Ö¹²âÊÔ");
            //ÉèÖÃÉ豸Æô¶¯·ÅµçºÍÍ£Ö¹·Åµç|ÖØÆôϵͳ
            bytebuffer.putShort(ComBase.changeIntToShort(MyModBusCom.LD_CMD_STARTSTOP_VALUE));
        }else if(RES_Index == MyModBusCom.LD_DATE_TIME_ADDR){
            Calendar now = Calendar.getInstance();  
            bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.YEAR)%100));
            bytebuffer.put(ComBase.changeIntToByte((now.get(Calendar.MONTH) + 1)));
            bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.DAY_OF_MONTH)));
            bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.HOUR_OF_DAY)));
            bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.MINUTE)));
            bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.SECOND)));
        }else if(RES_Index == MyModBusCom.LD_BATT_COLLECTIO_PARAM_ADDR) {
            concentratorparam.putByteBuffer(bytebuffer);
        }
        CRC = Crc16.CalCRC16(bytebuffer, bytebuffer.position()) & 0xFFFF;
        bytebuffer.putShort((short)CRC);
        bytebuffer.flip();
        System.out.println(ComFn.bytesToHexString(bytebuffer.array(), bytebuffer.array().length));
        return bytebuffer;
    }
    
    public static short toBigEndian(int n){
        return (short) ((n<<8) + (n>>8));
    }
    
    
    public LD_param getLd_param() {
        return ld_param;
    }
 
    public void setLd_param(LD_param ld_param) {
        this.ld_param = ld_param;
    }
 
    
    
    public int getByteCount() {
        return ByteCount;
    }
 
    public void setByteCount(int byteCount) {
        ByteCount = byteCount;
    }
 
    
    
    public int getRES_Index() {
        return RES_Index;
    }
 
    public void setRES_Index(int rES_Index) {
        RES_Index = rES_Index;
    }
 
    
 
    public ConcentratorParam getConcentratorparam() {
        return concentratorparam;
    }
 
    public void setConcentratorparam(ConcentratorParam concentratorparam) {
        this.concentratorparam = concentratorparam;
    }
 
    @Override
    public String toString() {
        return "BaseData [BYTE_LEN_03=" + BYTE_LEN_03 + ", BYTE_LEN_10=" + BYTE_LEN_10 + ", dev_addr=" + dev_addr
                + ", ByteCount=" + ByteCount + ", CMD=" + CMD + ", RES_Index=" + RES_Index  + ", ld_param=" 
                + ld_param + ", CRC=" + CRC + "]";
    }
 
    public static void main(String[] args) {
        //System.out.println(toBigEndian(0x201B));
        
        //BaseData data = new BaseData();
        //data.makeCmd(0x201B, 0x03,0x05);
        //System.out.println(ComFn.bytesToHexString(data.getByteBuffer().array(), data.getByteBuffer().array().length));
    
        //System.out.println(ComFn.bytesToHexString(src, len));
    }
    
}