JLChen
2021-03-16 3617e6eecac94965554487afc50d39b0584324fb
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
package sendData;
 
 
public class SendDatas { 
 
    //SubnetID
    public int DesSubnetID;
    //DeviceID
    public int DesDeviceID;
    //Send additional data
    public byte[] AddBytes =new byte[]{};
 
    //Source subnet addresses 0-254    
    public static int LocalSubnetID = 250;
 
    //source device address 0-254
    public static int LocalDeviceID = 251;
 
    // Device type
    public static int DeviceType = 424;
    
    //opcode
    public int Command;
        
    public byte []GetSendBytes(){
 
        //Data and to compute the CRC of CRC two bytes
        byte []crcBytes = new byte[9+this.AddBytes.length+2];
 
        //Send out the data
        byte []sendBytes = new byte[2+crcBytes.length];
 
        //Boot code
        sendBytes[0]=(byte)0xAA;
        sendBytes[1]=(byte)0xAA;
 
        //16    The length of the packet   11-78
        crcBytes[0]=(byte)crcBytes.length;
        //17    Source subnet addresses  0-254
        crcBytes[1]=(byte)LocalSubnetID;
        //18    source device address   0-254
        crcBytes[2]=(byte)LocalDeviceID;
        //19    High source device type
        crcBytes[3]=(byte)(DeviceType/256);
        //20    The source device type low
        crcBytes[4]=(byte)(DeviceType%256);
        //21    Opcode high
        crcBytes[5]=(byte)(this.Command/256);
        //22    Opcode low
        crcBytes[6]=(byte)(this.Command%256);
        //23    The target subnet addresses  0-254
        crcBytes[7]=(byte)this.DesSubnetID;
        //24    The destination address  0-254
        crcBytes[8]=(byte)this.DesDeviceID;
        //25~n  Additional data
        System.arraycopy(this.AddBytes, 0, crcBytes, 9, this.AddBytes.length);
        //Check CRC
        CRC.ConCRC(crcBytes, crcBytes.length-2);
 
        //Copy the CRC data to send data
        System.arraycopy(crcBytes, 0,sendBytes, 2, crcBytes.length);
        
        return sendBytes;        
        
    }
    
    //send data
    public static byte []AddSendData(int command,int desSubnetID,int desDeviceID,byte []addBytes){
        SendDatas sendDatas = new SendDatas();
        sendDatas.Command=command;    
        sendDatas.DesSubnetID=desSubnetID;
        sendDatas.DesDeviceID=desDeviceID;
        sendDatas.AddBytes=addBytes;       
        
        byte []sendBytes = sendDatas.GetSendBytes();
        
        return sendBytes;
    }
}