/* Check if the CRC of a MODBUS message is correct. */
int32_t modbus_check_crc(uint8_t *msg, uint8_t msg_length)
/* Check if the CRC of a MODBUS message is correct. */
int32_t modbus_check_crc(uint8_t *msg, uint8_t msg_length)
{
	int32_t ret = 0;
	uint16_t crc_calculated, crc_received;
	uint8_t crc_hi, crc_lo;
	if(msg_length < 2)
		return -1;
	modbus_crc16(msg, msg_length - 2, &crc_hi, &crc_lo);
	crc_calculated = ((uint16_t)crc_hi << 8) | (uint16_t)crc_lo;
	crc_received   = ((uint16_t)msg[msg_length - 1] << 8) |
			 (uint16_t)msg[msg_length - 2];
	if(crc_calculated == crc_received)
		ret = msg_length;
	else
		ret = -1;
	return ret;
}