80,349
社区成员
发帖
与我相关
我的任务
分享
public byte[] getCRC(byte[] input, int input_length){
int reg_crc=0xFFFF;
//input_length--;
for (int i=1;i<input_length;i++){
Toast.makeText(Commviewer_ControlActivity.this, i+", "+String.valueOf((int)input[i]), Toast.LENGTH_SHORT).show();
reg_crc ^= input[i];
for (int j=0;j<8;j++){
//reg_crc=reg_crc>>1;
if((reg_crc & 0x01)==1){
reg_crc=(reg_crc>>1)^0xA001;
}else{
reg_crc=reg_crc>>1;
}
}
}
byte[] crcByte = new byte[2];
crcByte[0] = (byte)((reg_crc >> 8) & 0xFF);
crcByte[1] = (byte) (reg_crc & 0xFF);
// 将新生成的byte数组添加到原数据结尾并返回
return concatAll(input, crcByte);
}
public byte[] concatAll(byte[] input_array, byte[] crcB){
byte[] result = new byte[input_array.length + 3];
System.arraycopy(input_array,0,result,0,input_array.length);
result[input_array.length]=crcB[1];
result[input_array.length+1]=crcB[0];
result[input_array.length+2]=0x18;
return result;
}
byte[] WriteBytes = new byte[17];
WriteBytes[0]=0x66;
WriteBytes[1]=0x00;
WriteBytes[2]=0x0B;
WriteBytes[3]=0x00;
WriteBytes[4]=0x10;
WriteBytes[5]=0x00;
WriteBytes[6]=0x10;
WriteBytes[7]=0x11;
WriteBytes[8]=0x22;
WriteBytes[9]=0x33;
WriteBytes[10]=0x44;
WriteBytes[11]=0x55;
WriteBytes[12]=0x66;
WriteBytes[13]=0x77;
WriteBytes[14]=(byte)0x88;
WriteBytes[15]=(byte)0x99;
WriteBytes[16]=(byte)0xFF;
public byte[] getCRC(byte[] input, int input_length){
int reg_crc=0xFFFF;
//input_length--;
for (int i=1;i<input_length;i++){
//Toast.makeText(Commviewer_ControlActivity.this, i+", "+String.valueOf((int)input[i]), Toast.LENGTH_SHORT).show();
reg_crc ^= (int)(input[i]&0xFF);
for (int j=0;j<8;j++){
if((int)(reg_crc & 0x01)==1){
reg_crc=(reg_crc>>1)^0xA001;
}else{
reg_crc=reg_crc>>1;
}
}
}