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

2014-04-099830
AI 快速预览详细 收起
PlainProtocol是一个基于明文协议的Arduino库,旨在简化通信协议的设计与解析。它支持多种串口通信设备,如Zigbee、Wi-Fi和蓝牙。通过示例代码,用户可以轻松发送和接收数据,无需复杂的协议设计。例如,发送端代码展示了如何设置速度、目的地和显示字符串,而接收端代码则展示了如何解析这些数据并打印出来。


做项目的时候,一旦牵涉到通信,就需要建立一套自己的协议,这不仅会花很长时间,而且在协议的解析过程中也很容易出现各种各样的问题。如果其他人需要使用这个协议,必须用很长的时间来看懂协议。这个过程也会非常痛苦。
为什么这种协议不能变得简单而易于使用呢?通过借鉴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

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(4)
wanglei830205
wanglei830205
这个协议目前都是基于串口的,比如现在两个UNO之间通过其它的硬件(如射频硬件)发生无线通讯,如何利用这个协议来进行解析传输的数据呢?
wanglei830205
wanglei830205
这个很值得借鉴使用,下载一个先
齐天大妖孽
齐天大妖孽
终于不用自己去解析了,这个确实不错,很容易与高级设备通信。
Grey
Grey
这个太赞了,通信协议实在是太头痛了
- 没有更多了 -