24-ESP8266 SDK开发基础入门篇--Android TCP客户端.控制 Wi-Fi输出PWM的占空比,调节LED亮度
<p><iframe name="ifd" src="https://mnif.cn/Learn8266SDKDevelop/directory.html" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p><p><span style="font-size: 18pt">https://www.cnblogs.com/yangfengwu/p/11204436.html</span></p>
<p><span style="font-size: 24px">刚才有人说需要点鸡汤....</span></p>
<p><span style="font-size: 24px">我想想哈;我还没问关于哪方面的鸡汤呢!!!</span></p>
<p><span style="font-size: 24px">我所一直走的路线</span></p>
<p><span style="font-size: 18pt">第一:能够帮到人</span><br><span style="font-size: 18pt">第二:能够获得好的人气</span><br><span style="font-size: 18pt">第三:获取利益,养活自己</span></p>
<p><span style="font-size: 24px">其实第一和第二只要你有哪一方面的优点,又肯吃苦,那么就很好走到.</span></p>
<p><span style="font-size: 24px">但是至于第三点,既要全心全意的帮助别人又要从别人那里获取利益养活自己.....其实不容易</span></p>
<p><span style="font-size: 24px">很多人只能走到第一,第二,然后走到沾一点第三的边.</span></p>
<p><span style="font-size: 24px">这个时候的人们就开始浮躁了,大部分人都会走极端,要么舍弃不干了,要么突然把自己的东西开始全部利益化......</span></p>
<p><span style="font-size: 24px">怎么说呢!走到这个时候,你应该静下心来好好的想想自己的初心,很多时候人们都在说:不忘初心!!!</span></p>
<p><span style="font-size: 24px">注意:事情总会有解决的办法,可能在那个时候你没有想出来很好的办法,就走了极端,你是否想过,你再坚持坚持自己的初心,无论何时都</span></p>
<p><span style="font-size: 24px">不能动摇自己的初心.你要知道很多时候都是在人们感觉一点希望都没有的时候,才会想到办法,才看到了希望!所以才会有了那句:天道酬勤</span></p>
<p><span style="font-size: 24px">其实我本身就是经历过这种日子,我曾经也动摇过初心,但是怎么说呢!我动摇过后不到3天就会立马改过来,因为我不敢欺骗自己的心.会让我难受!</span></p>
<p><span style="font-size: 24px">那个马爸爸说过一句话:迷茫和彷徨过后,知道自己应该干什么才是最重要的!</span></p>
<p><span style="font-size: 24px">关键还是放平自己的心态,现在遍地都是浮躁的人,一口吃不了胖子!</span></p>
<p><span style="font-size: 24px">虽然说是金子总会发光,,但是我的理解是:谁一开始都不是金子,是经过了炼金的过程最终把自己炼成了金子.</span></p>
<p> </p>
<p><span style="font-size: 24px">增加一个SeekBar</span></p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190808220519298-551299035.png" alt=""></p>
<p> </p>
<p><span style="font-size: 18pt"> 一个 Switch</span></p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190808220540724-1912672474.png" alt=""></p>
<p> </p>
<p> <span style="font-size: 18pt">协议:</span></p>
<p> <span style="font-size: 18pt">00 01 70 C0 控制LED点亮 70 C0 为CRC高位和低位<br>00 00 B0 01 控制LED熄灭 B0 01 为CRC高位和低位</span></p>
<p> </p>
<p><span style="font-size: 24px">PWM数据</span></p>
<p><span style="font-size: 24px">01固定 后四位为PWM数据(高位在前低位在后,按照IEEE754规约) 最后两位为CRC16校验位,高位在前,低位在后</span></p>
<p><span style="font-size: 24px">列如:举几个例子,PWM数据是0-1000</span></p>
<p><span style="font-size: 18pt">01 00 00 03 E8 7E 19 注:00 00 03 E8 为1000 7E 19 为CRC检验数据高位在前,低位在后</span></p>
<p><span style="font-size: 18pt">01 00 00 01 F4 D7 19 500</span></p>
<p><span style="font-size: 18pt">01 00 00 00 64 2B 18 100</span></p>
<p><span style="font-size: 18pt">01 00 00 00 00 C0 19 0</span></p>
<p> </p>
<p><span style="font-size: 24px">大家可以用此工具生成CRC,用于验证咱写的CRC程序. </span></p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809041824674-1926235451.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt"> 注:01 00 00 03 E8 7E 19 其实可以省掉两个0 01 03 E8 7E 19</span></p>
<p><span style="font-size: 24px">但是我所用过的大部分的仪器仪表都是4位的所以咱就还是用四位</span></p>
<p> </p>
<p><span style="font-size: 24px">现在看Android 端怎么写</span></p>
<p><span style="font-size: 24px"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190808225411580-1079054061.png" alt=""></span></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190808225427654-1371746787.png" alt=""></p>
<p> </p>
<p> </p>
<p> <span style="font-size: 18pt">现在呢给大家计算CRC的程序,判断数据CRC是不是正确的程序</span></p>
<p> </p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* CRC检验值
* @param modbusdata
* @param length
* @return CRC检验值
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">int</span> crc16_modbus(<span style="color: rgba(0, 0, 255, 1)">byte</span>[] modbusdata, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> length)
{
</span><span style="color: rgba(0, 0, 255, 1)">int</span> i=<span style="color: rgba(128, 0, 128, 1)">0</span>, j=<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> crc = <span style="color: rgba(128, 0, 128, 1)">0xffff</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有的用0,有的用0xff</span>
<span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
{
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < length; i++<span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意这里要&0xff,因为byte是-128~127,&0xff 就是0x0000 0000 0000 00000000 0000 1111 1111
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">参见:</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://blog.csdn.net/ido1ok/article/details/85235955</span>
crc ^= (modbusdata&(<span style="color: rgba(128, 0, 128, 1)">0xff</span><span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (j = <span style="color: rgba(128, 0, 128, 1)">0</span>; j < <span style="color: rgba(128, 0, 128, 1)">8</span>; j++<span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(0, 0, 255, 1)">if</span> ((crc & <span style="color: rgba(128, 0, 128, 1)">0x01</span>) == <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">)
{
crc </span>= (crc >> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">) ;
crc </span>= crc ^ <span style="color: rgba(128, 0, 128, 1)">0xa001</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
{
crc </span>>>= <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
}
}
</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e)
{
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> crc;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* CRC校验正确标志
* @param modbusdata
* @param length
* @return 0-failed 1-success
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">int</span> crc16_flage(<span style="color: rgba(0, 0, 255, 1)">byte</span>[] modbusdata, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> length)
{
</span><span style="color: rgba(0, 0, 255, 1)">int</span> Receive_CRC = <span style="color: rgba(128, 0, 128, 1)">0</span>, calculation = <span style="color: rgba(128, 0, 128, 1)">0</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">接收到的CRC,计算的CRC</span>
<span style="color: rgba(0, 0, 0, 1)">
Receive_CRC </span>=<span style="color: rgba(0, 0, 0, 1)"> crc16_modbus(modbusdata, length);
calculation </span>= modbusdata;
calculation </span><<= <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">;
calculation </span>+=<span style="color: rgba(0, 0, 0, 1)"> modbusdata;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (calculation !=<span style="color: rgba(0, 0, 0, 1)"> Receive_CRC)
{
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt">先去烧壶水,今天需要熬夜写文章,测试东西....</span></p>
<p><span style="font-size: 24px">下面看使用</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809022755488-2064171141.png" alt=""></span></p>
<p> </p>
<p> <span style="font-size: 18pt">我先和我的电脑的网络调试助手测试一下</span></p>
<p> </p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809022914088-1981131878.png" alt=""></span></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809023024056-2081857354.png" alt=""> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809023042265-451097769.png" alt=""></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809023106529-1957882629.png" alt=""> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809023120784-1603689972.png" alt=""></p>
<p> </p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt"> 好,现在写WIFI的程序</span></p>
<p><span style="font-size: 24px">弄个crc.c和crc.h</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809023756263-1133534068.png" alt=""></span></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809023838255-926105358.png" alt=""></p>
<p> </p>
<p><span style="font-size: 18pt">官方的都是把h文件放到这里,咱也放到这里</span></p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809023902553-197300277.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt">C语言的和java的其实差不多,只不过有些细节不一样,类如java用byte java的byte需要&0xff</span></p>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809031446466-321611100.png" alt=""></p>
<p> </p>
<div class="cnblogs_code">
<pre>#include <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">esp_common.h</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @brief计算CRC
* @param*modbusdata:数据指针
* @paramlength:需要计算的CRC的数据长度
* @param
* @retval 计算的CRC
* @example
*</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">int</span> crc16_modbus(u8 *modbusdata, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> length)
{
</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> i, j;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> crc = <span style="color: rgba(128, 0, 128, 1)">0xffff</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有的用0有的用0xffff</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < length; i++<span style="color: rgba(0, 0, 0, 1)">)
{
crc </span>^=<span style="color: rgba(0, 0, 0, 1)"> modbusdata;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (j = <span style="color: rgba(128, 0, 128, 1)">0</span>; j < <span style="color: rgba(128, 0, 128, 1)">8</span>; j++<span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(0, 0, 255, 1)">if</span> ((crc & <span style="color: rgba(128, 0, 128, 1)">0x01</span>) == <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">)
{
crc </span>= (crc >> <span style="color: rgba(128, 0, 128, 1)">1</span>) ^ <span style="color: rgba(128, 0, 128, 1)">0xa001</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
{
crc </span>>>= <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> crc;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @brief判断CRC的校验是否正确
* @param*modbusdata:要判断的数据指针
* @paramlength:需要计算的CRC的数据长度
* @param
* @retval 1 OK·0 err
* @example
*</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">int</span> crc16_flage(u8 *modbusdata, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> length)
{
</span><span style="color: rgba(0, 0, 255, 1)">int</span> Receive_CRC=<span style="color: rgba(128, 0, 128, 1)">0</span>,calculation=<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
Receive_CRC </span>= crc16_modbus(modbusdata, length);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用自带的函数计算数据的CRC</span>
calculation = modbusdata;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得接收的CRC的高位</span>
calculation <<= <span style="color: rgba(128, 0, 128, 1)">8</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得接收的CRC的低位</span>
calculation += modbusdata;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">高低位组合</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(calculation != Receive_CRC)<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">看看CRC是否相等</span>
<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt">声明一下</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809024829133-155086540.png" alt=""></span></p>
<p> </p>
<p> </p>
<p> <span style="font-size: 18pt">然后</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809031920616-31528651.png" alt=""></span></p>
<p> </p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809032017150-1581924070.png" alt=""></p>
<p> </p>
<p><span style="font-size: 18pt"> 下载测试</span></p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809032033859-303508520.png" alt=""></p>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809032215099-258297921.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt">开发板看上去有点旧........因为我把我使用的测试的那一块板子都卖了,现在还欠着别人的板子呢.....我使用的是以前自己焊接的一块..断货了....</span></p>
<p><span style="font-size: 18pt">最近由于一直忙,这块板子的下一批贴片可能要等大约2个星期.....</span></p>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809032431432-698761058.png" alt=""> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809031714422-1364093416.png" alt=""> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809032512535-1434882135.png" alt=""></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809032538337-1764603241.png" alt=""> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809031745226-1392938722.png" alt=""> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809032607297-1979546638.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt">接着做PWM部分</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809034830548-1809500331.png" alt=""></span></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809035519332-548397007.png" alt=""></p>
<p> </p>
<p> <span style="font-size: 18pt">和当时串口的时候一样,咱呢不能直接把发送放到里面,咱的TCP服务器设置的是5ms延时</span></p>
<p><span style="font-size: 18pt">所以咱呢,就10ms发送一次数据.</span></p>
<p><span style="font-size: 18pt">判断数据变化了,就把数据发送出去...</span></p>
<p><span style="font-size: 18pt">我测试了,出现了问题</span></p>
<p><span style="font-size: 18pt">WIFI的程序有问题,会粘包....</span></p>
<p><span style="font-size: 18pt">先说一下我的Android 的程序</span></p>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809145649160-781072803.png" alt=""></p>
<p> </p>
<p> </p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809145706825-1887359740.png" alt=""></p>
<p> </p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TCP客户端发送数据</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> startTimer(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (timer == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
timer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Timer();
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (timerTask == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
timerTask </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TimerTask() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (seekBarValueCopy != seekBarValue){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">数据改变</span>
seekBarValueCopy =<span style="color: rgba(0, 0, 0, 1)"> seekBarValue;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (socket!=<span style="color: rgba(0, 0, 255, 1)">null</span> && socket.isConnected()){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果TCP是正常连接的</span>
<span style="color: rgba(0, 0, 255, 1)">int</span> crc = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
TcpSendData[</span><span style="color: rgba(128, 0, 128, 1)">0</span>] =(<span style="color: rgba(0, 0, 255, 1)">byte</span>)<span style="color: rgba(128, 0, 128, 1)">0x01</span><span style="color: rgba(0, 0, 0, 1)">;
TcpSendData[</span><span style="color: rgba(128, 0, 128, 1)">1</span>] =(<span style="color: rgba(0, 0, 255, 1)">byte</span>)((seekBarValue>><span style="color: rgba(128, 0, 128, 1)">24</span>)&<span style="color: rgba(128, 0, 128, 1)">0xff</span><span style="color: rgba(0, 0, 0, 1)">);
TcpSendData[</span><span style="color: rgba(128, 0, 128, 1)">2</span>] =(<span style="color: rgba(0, 0, 255, 1)">byte</span>)((seekBarValue>><span style="color: rgba(128, 0, 128, 1)">16</span>)&<span style="color: rgba(128, 0, 128, 1)">0xff</span><span style="color: rgba(0, 0, 0, 1)">);
TcpSendData[</span><span style="color: rgba(128, 0, 128, 1)">3</span>] =(<span style="color: rgba(0, 0, 255, 1)">byte</span>)((seekBarValue>><span style="color: rgba(128, 0, 128, 1)">8</span>)&<span style="color: rgba(128, 0, 128, 1)">0xff</span><span style="color: rgba(0, 0, 0, 1)">);
TcpSendData[</span><span style="color: rgba(128, 0, 128, 1)">4</span>] =(<span style="color: rgba(0, 0, 255, 1)">byte</span>)(seekBarValue&<span style="color: rgba(128, 0, 128, 1)">0xff</span><span style="color: rgba(0, 0, 0, 1)">);
crc </span>= crc16_modbus(TcpSendData, <span style="color: rgba(128, 0, 128, 1)">5</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">计算CRC</span>
TcpSendData[<span style="color: rgba(128, 0, 128, 1)">5</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)((crc >> <span style="color: rgba(128, 0, 128, 1)">8</span>) & <span style="color: rgba(128, 0, 128, 1)">0xff</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">CRC高位</span>
TcpSendData[<span style="color: rgba(128, 0, 128, 1)">6</span>] = (<span style="color: rgba(0, 0, 255, 1)">byte</span>)(crc & <span style="color: rgba(128, 0, 128, 1)">0xff</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">CRC低位</span>
outputStream.write(TcpSendData,<span style="color: rgba(128, 0, 128, 1)">0</span>,<span style="color: rgba(128, 0, 128, 1)">7</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发送数据</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}
}</span><span style="color: rgba(0, 0, 255, 1)">catch</span> (Exception e){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">接收数据有错误</span>
Message msg = myHandler.obtainMessage();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从消息队列拉取个消息变量</span>
msg.what = <span style="color: rgba(128, 0, 128, 1)">9</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置消息变量</span>
myHandler.sendMessage(msg);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">插入消息队列</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}
};
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(timer != <span style="color: rgba(0, 0, 255, 1)">null</span> && timerTask != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)"> )
timer.schedule(timerTask, </span><span style="color: rgba(128, 0, 128, 1)">10</span>, <span style="color: rgba(128, 0, 128, 1)">100</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">启动定时器,100ms进一次</span>
<span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">停止定时器</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> stopTimer(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (timer != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
timer.cancel();
timer </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (timerTask != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
timerTask.cancel();
timerTask </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
}</span></pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809150104010-1533678893.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt"> 然后我用电脑的网络调试助手测试的</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809150211359-323219021.png" alt=""></span></p>
<p> </p>
<p> <span style="font-size: 18pt">滑动进度条,大约100ms发送一次数据,如果这次数据和上次数据不一样的话</span></p>
<p> </p>
<p><span style="font-size: 18pt">而用8266测试的是</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809150407886-1719870225.png" alt=""></span></p>
<p> </p>
<p> <span style="font-size: 18pt">就是说粘包挺严重的了,本来是7个数据......</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809150500086-214945013.png" alt=""></span></p>
<p> </p>
<p> </p>
<p> </p>
<p> <span style="font-size: 18pt">这个问题我有时间用ENC28J60测试下,当然源码都是公开的,大家发现有什么问题可以告知,感谢!</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809151952944-709286136.png" alt=""></span></p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809152154373-1186221715.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt">咱再接着优化一个问题</span></p>
<p><span style="font-size: 18pt">由于打开了PWM,所以这个无效了</span></p>
<p><span style="font-size: 18pt"><img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809150912231-719345675.png" alt=""></span></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt"> 所以</span></p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809151148019-35018792.png" alt=""></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/819239/201908/819239-20190809151328042-839707332.png" alt=""></p>
<p> </p>
<p> <span style="font-size: 18pt">然后大家自己去测试哈,,,由于粘包问题,就是滑动进度条的时候不怎么好使,这个我再找找WIFI程序的问题</span></p>
<p><span style="font-size: 18pt">https://www.cnblogs.com/yangfengwu/p/11427504.html</span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/yangfengwu/p/11324411.html
頁:
[1]