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;
	}
}