Pollutant online automatic monitoring (monitoring) system data transmission standard (HJ212-2017)-CRC data verification (C/Java source code)

Reprinted from: https://www.cnblogs.com/LiuSiyuan/p/8110192.html Pollutant Online Automatic Monitoring (Monitoring) System Data Transmission Standard (HJ212-2017)-CRC Data Verification

After the 2017 edition of the pollutant online monitoring (monitoring) system data transmission standard was released, the 2005 edition became invalid.

Due to the company's business needs, this standard is studied as part of the underlying data analysis of the company's IoT monitoring cloud platform. (The company uses a simplified version of the 212 protocol, which is really too simple)

CRC check part:

C code

unsigned int CRC16_Checkout ( unsigned char *puchMsg, unsigned int usDataLen ) 
{ 
unsigned int i,j,crc_reg,check; 
crc_reg = 0xFFFF; 
for(i=0;i<usDataLen;i++) 
{
crc_reg = (crc_reg>>8) ^ puchMsg[i]; 
 for(j=0;j<8;j++) 
{
 check = crc_reg & 0x0001; 
 crc_reg >>= 1; 
 if(check==0x0001) 
{
 crc_reg ^= 0xA001; 
 } 
 }
} 
return crc_reg; 
}

Java code implementation:

public static int getCRC(String data212) {
        int CRC = 0xFFFF;
        int num = 0xA001;
        int inum = 0;
        byte[] sb = data212.getBytes();
        for(int j = 0; j < sb.length; j ++) {
            inum = sb[j];
            CRC = (CRC >> 8) & 0x00FF;
            CRC ^= inum;
            for(int k = 0; k < 8; k++) {
                int flag = CRC % 2;
                CRC = CRC >> 1;
            
                if(flag == 1) {
                    CRC = CRC ^ num;
                }
            }
        }
        return CRC;
    }

Finally, don't forget, what is needed is the hexadecimal result, Integer.toHexString()

Check section:

Example:
##0101QN=20160801085857223;ST=32;CN=1062;PW=100000;MN=010000A8900016F000169DC0;Flag=5 ;CP=&&RtdInterval=30
&&1C80\r\n, where 1C80 is CRC16 check code, is for the data segment QN=20160801085857223;
ST=32;CN=1062;PW=100000;MN=010000A8900016F000169DC0;Flag=5;CP=&&RtdInterval=30&& The check
code obtained from the CRC16 check (extracted from the 2017 version of the protocol document, here is a error here, corrected here)

 ‘======================================================

ps: The error in the original text is as follows:

 Actually: 1C80

Guess you like

Origin blog.csdn.net/jessezappy/article/details/125092027