15-ESP8266 SDK Development Foundation Beginners - PC serial port controls the duty cycle of the PWM output Wi-Fi, the preparation of PC program

https://www.cnblogs.com/yangfengwu/p/11104167.html

Let me talk about the overall Silu Ha ..

When we slide

Will enter this, then you do not write directly from the inside

Because in that case too soon ,, we deal with the task of SCM

Let's do a timer, real-time detection of changes in the value of a progress bar of 10ms, if this value is not the same as last time value is sent

 

 

 

 

 

 

 

 

 Definition of a variable for storing the previous state

 

 

 

 

Now test, sent another serial port, and then calculate the next

   

 

 

 

 

 

 Well now control lights

    

 

    

 

 

 

     

 

Themselves to test it, to expand their control servo, motor speed control can be

 

 

Then everyone CRC code CRC and the CRC ahead microcontroller calculated CRC consistent, you can add CRC data in the last two

        /// <CRC  效验  >
        /// 
        /// </summary>
        /// <param name="modbusdata"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private int crc16_modbus(byte[] modbusdata, int length)
        {
            int i, j;

            int crc = 0xffff;

            try
            {
                for (i = 0; i < length; i++)
                {
                    crc ^= modbusdata[i];
                    for (j = 0; j < 8; j++)
                    {
                        if ((crc & 0x01) == 1)
                        {
                            crc = (crc >> 1) ^ 0xa001;
                        }
                        else
                        {
                            crc >>= 1;
                        }
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
            return crc;
        }

        /*
          modbusdata   要校验的数据
          length       数据长度
          返回值 1 正确  0 错误
        */
        private int crc16_flage(byte[] modbusdata, int length)
        {
            int Receive_CRC = 0, calculation = 0;//接收到的CRC,计算的CRC

            Receive_CRC = crc16_modbus(modbusdata, length);
            calculation = modbusdata[length + 1];
            calculation <<= 8;
            calculation += modbusdata[length];
            if (calculation != Receive_CRC)
            {
                return 0;
            }
            return 1;
        }

 

 

提示:CRC使用

 

 

 后面咱会使用起来CRC  现在大家先自己摸索把,或者百度

 

Guess you like

Origin www.cnblogs.com/yangfengwu/p/11105466.html