24-ESP8266 SDK development Basics piece --Android TCP client. Wi-Fi control output PWM duty cycle to adjust the brightness of LED

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

Someone just said I need chicken soup ....

I think Ha; I did not ask about the chicken soup which aspect of it !!!

 

 

Add a SeekBar

 

 A Switch

 

 

 protocol:

 00 01 70 C0 70 C0 controls the LED lighting is high and low CRC
00 00 B0 01 B0 01 LED to go out of upper and lower bits CRC

 

PWM data

After 01 fixed to four PWM data (MSB first low after, according to the statute IEEE754) CRC16 last two bits of the parity bit, high front and low at the

The columns: a few examples, PWM data is 0-1000

01 03 E8 00 00 B0 99 Note: 03 E8 00 00 to 1000 B0 99 CRC test data for the high front and low in the post

1:01 F4 0:00 EC 59 500

01 64 00 00 00 F0 06100

01 00 00 00 00 C0 19            0

 

We can use this tool to generate the CRC, the CRC is used to verify we write the program. 

 

 Note: 01 03 E8 00 00 B0 99 can actually save two 0 01 03 E8 B0 99

But I have used most of the instruments are 4 So let's still with four

 

Now look at how to write Android end

 

 

 

 

 Now it's a program for everyone CRC calculation, CRC data judgment is not the right program

 

    /**
     * CRC检验值
     * @param modbusdata
     * @param length
     * @return CRC检验值
     */
    protected int crc16_modbus(byte[] modbusdata, int length)
    {
        int i=0, j=0;
        int crc = 0xffff;//有的用0,有的用0xff
        try
        {
            for (i = 0; i < length; i++)
            {
                //注意这里要&0xff,因为byte是-128~127,&0xff 就是0x0000 0000 0000 0000  0000 0000 1111 1111
                //参见:https://blog.csdn.net/ido1ok/article/details/85235955
                crc ^= (modbusdata[i]&(0xff));
                for (j = 0; j < 8; j++)
                {
                    if ((crc & 0x01) == 1)
                    {
                        crc = (crc >> 1) ;
                        crc = crc ^ 0xa001;
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }
        }
        catch (Exception e)
        {

        }

        return crc;
    }

    /**
     * CRC校验正确标志
     * @param modbusdata
     * @param length
     * @return 0-failed 1-success
     */
    protected 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;
    }

 

 

先去烧壶水,今天需要熬夜写文章,测试东西....

Guess you like

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