Modbus CRC C語言

	crc_value=get_crc(z_tx_data,3);
	z_tx_data[3] = crc_value>>8;
	z_tx_data[4] = crc_value & 0x00FF;

/*Modbus crc16*/
unsigned int crc (unsigned int crc_val ,unsigned char R80) //for crc Xor operation
{
	
	unsigned char R81;
	
	crc_val = crc_val ^ R80;
	
		for (R81=0;R81<8;R81++)
		{
		
		crc_val = (crc_val >>1) ^ ((crc_val & 1) ? crc_CHKD : 0);
		
		}
		
	return crc_val;
	
}
unsigned int get_crc (unsigned char R70[],unsigned char R71)//calculate the value of crc
{
	
	unsigned char R72;
	unsigned int  R73 = 0xFFFF;
	unsigned int  R74;
	
		for (R72 = 0;R72 < R71;R72++)
		{
		
		R73 = crc(R73,R70[R72]);
		
		}
	//---------------------------------------
	// crc16 H-BYTE,L-BYTE CHANG
	//---------------------------------------
	R74 = R73;
	R74 <<= 8;
	R73 >>= 8;
	R73 |= R74;
	//---------------------------------------
	return R73;	
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料