Source: Android can use the Modebus CRC16 calculation, attached to the corresponding C language CRC16
"Source" is the original address, want to know more articles and ideas please go to "source". Turn around just for my interest in the article search convenience.
Body:
Recently wrote Android serial communication needs to use the Modebus of CRC16 communication, online find a lot of is a string, I need a byte, because Java has no unsigned number, conversion a little trouble, a test of C a
The code is as follows:
Java
intCrc16_check (bytePushdata[],intlength) { intReg_crc=0xffff; inttemp; inti,j; for(i = 0; i<length; i + +)) {Temp=Pushdata[i]; if(Temp < 0) Temp + + 256; Temp&= 0xFF; REG_CRC^=temp; for(j = 0; j<8; J + +) { if((REG_CRC & 0x0001) = = 0x0001) REG_CRC= (reg_crc>>1) ^0xa001; ElseREG_CRC>>=1; } } return(REG_CRC&0XFFFF); }
C
U16 Crc16_check (U8 *pushdata,u16 length) {U16 REG_CRC=0xFFFF; U8 Temp_reg=0x00; U16 I,j; for(i =0; i<length; i + +) {REG_CRC^= *pushdata++; for(j =0; j<8; J + +) { if(REG_CRC &0x0001) REG_CRC=reg_crc>>1^0xa001; ElseREG_CRC>>=1; } } return(REG_CRC); }
Android can use the Modebus CRC16 calculation, attached to the corresponding C language CRC16 (turn)