Android can use the Modebus CRC16 calculation, attached to the corresponding C language CRC16

Source: Internet
Author: User

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&AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.