6873浏览
查看: 6873|回复: 4

无线通讯通用库(PlainProtocol 简介 Ver 1.1)

[复制链接]
本帖最后由 Angelo 于 2014-11-3 10:15 编辑

做项目的时候,一旦牵涉到通信,就需要建立一套自己的协议,这不仅会花很长时间,而且在协议的解析过程中也很容易出现各种各样的问题。如果其他人需要使用这个协议,必须用很长的时间来看懂协议。这个过程也会非常痛苦。
为什么这种协议不能变得简单而易于使用呢?通过借鉴JavaScript Object Notation(JSON),我们完成这个基于明文协议的Arduino库。
它使用起来非常方便,继承了Arduino的函数调用习惯,更加方便,易懂。
并且可以在基于串口的任何一种通信设备上使用,比如Zigbee,Wi-Fi, 蓝牙等等。

下载PlainProtocol库

如何安装Arduino库

发送端的样例代码:
  1. #include <PlainProtocol.h>
  2. PlainProtocol mytest(Serial);
  3. void setup() {
  4.   mytest.begin(57600);
  5. }
  6. void loop() {
  7.   mytest.write("speed",100);          //set the speed to 100
  8.   mytest.write("destination", 23, 56); //set the destination to (23,56)
  9.   mytest.write("display","Hello World!",10,35);//show the string on display at (10,35)
  10.   delay(2000);
  11. }
复制代码

上传完这个代码以后,你就可以打开Arduino的串口监视器中看到如下的字符串,这个字符串就是编码过之后的数据。
  1. <speed>100;
  2. <destination>23,56;
  3. <displayHello World!>10,35;
复制代码



接收端的样例代码:


这是一个接收端的DEMO,你可以打开串口监视器,发送以下几条数据。然后就可以看到,PlainProtocol正确解析了发送出来的数据并且打印了出来。


  1. <speed>100;
  2. <destination>23,56;
  3. <displayHello World!>10,35;
复制代码




  1. #include <PlainProtocol.h>
  2. PlainProtocol mytest(Serial);
  3. int motorSpeed;
  4. int motorDestination[2];
  5. String displayString="";
  6. int displayPosition[2];
  7. void setup() {
  8.   mytest.begin(57600);
  9. }
  10. /*
  11. This is the receiving demo code for plainprotocol.
  12. You can send the following frame in Serial monitor to test whether the PlainProtocol can phrase the frame correctly:
  13. <speed>100;
  14. <destination>23,56;
  15. <displayHello World!>10,35;
  16. */
  17. void loop() {
  18.   if (mytest.available()) {
  19.     if (mytest.equals("speed")) {    //send "<speed>100;" in Serial monitor
  20.       //the "speed" process
  21.       motorSpeed=mytest.read();
  22.       Serial.print("speed:");
  23.       Serial.println(motorSpeed);
  24.     }
  25.     else if (mytest.equals("destination")){   //send "<destination>23,56;" in Serial monitor
  26.       //the "destination" process
  27.       motorDestination[0]=mytest.read();
  28.       motorDestination[1]=mytest.read();
  29.       Serial.println("destination:");
  30.       Serial.print(" X:");
  31.       Serial.print(motorDestination[0]);
  32.       Serial.print(" Y:");
  33.       Serial.println(motorDestination[1]);
  34.     }
  35.     else if (mytest.equals("display")){    //send "<displayHello World!>10,35;" in Serial monitor
  36.       //the "destination" process
  37.       displayString=mytest.readString();
  38.       displayPosition[0]=mytest.read();
  39.       displayPosition[1]=mytest.read();
  40.       Serial.println("display:");
  41.       Serial.print("displayString:");
  42.       Serial.println(displayString);
  43.       Serial.print(" X:");
  44.       Serial.print(displayPosition[0]);
  45.       Serial.print(" Y:");
  46.       Serial.println(displayPosition[1]);
  47.     }
  48.     else{
  49.       //no matching command
  50.     }
  51.   }
  52. }
复制代码


无线通讯通用库(PlainProtocol 简介 Ver 1.1)图1

英文版参考链接

[/table]
[table]
tutorials

Grey  中级技匠

发表于 2014-4-16 14:39:17

这个太赞了,通信协议实在是太头痛了
回复

使用道具 举报

齐天大妖孽  中级技师

发表于 2014-4-16 16:06:41

终于不用自己去解析了,这个确实不错,很容易与高级设备通信。
回复

使用道具 举报

wanglei830205  初级技师

发表于 2014-10-5 18:17:05

这个很值得借鉴使用,下载一个先
回复

使用道具 举报

wanglei830205  初级技师

发表于 2014-11-2 20:13:58

这个协议目前都是基于串口的,比如现在两个UNO之间通过其它的硬件(如射频硬件)发生无线通讯,如何利用这个协议来进行解析传输的数据呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail