Calculating Modbus RTU CRC 16

前端 未结 4 419
日久生厌
日久生厌 2020-12-16 01:37

I\'m implementing a software where I read and write data in Modbus RTU protocolo via serial. For that, I need to calculate the two CRC byte at the end of the string of bytes

4条回答
  •  离开以前
    2020-12-16 02:03

    Here are my two cents. First thing you cant return two values to a function so

    1. Append two values to the array(remember to remove const)
    2. Return a struct containing these two values.
    3. Proccess return value as follow

      WORD n = CRC16(nData,wLength);
      WORD x = (0xFF00&n)>>8,y=0x00FF&n;
      printf("0x%04x\n", n); // to check original value
      printf("0x%02x\t0x%02x\n",x,y); // to check separated values
      

    Try this and let me know.

提交回复
热议问题