Indeed the first release of the PlainProtocol deals with the process of the frame generating and phasing. However the code itself is not as graceful as we except. Thus we deeply deliberate on the Arduino basic library and highly inspired by its design philosophy. In bid to make the code more "Plain" as well, The PlainProtocol Ver 1.1 is released.
Ver 1.1 is the superset of Ver1.0. All the old functions are compatible. Just download and update the library here.
This example set the speed to 100, set the destination to (23,56), and show the string on display at (10,35). You can directly monitor the raw frame communicating in the Serial port.#include <PlainProtocol.h> PlainProtocol mytest(Serial); void setup() { mytest.begin(57600); } void loop() { mytest.write("speed",100); //set the speed to 100 mytest.write("destination", 23, 56); //set the destination to (23,56) mytest.write("display","Hello World!",10,35);//show the string on display at (10,35) delay(2000); }
This example receive the data from device and change the value of motorSpeed , motorDestination. displayString and etc. Just as the comments suggest, send the following frames in Serial monitor:<speed>100; <destination>23,56; <displayHello World!>10,35; Thus you can get the parsed data from the frame.#include <PlainProtocol.h> PlainProtocol mytest(Serial); int motorSpeed; int motorDestination[2]; String displayString=""; int displayPosition[2]; void setup() { mytest.begin(57600); } /* This is the receiving demo code for plainprotocol. You can send the following frame in Serial monitor to test whether the PlainProtocol can phrase the frame correctly: <speed>100; <destination>23,56; <displayHello World!>10,35; */ void loop() { if (mytest.available()) { if (mytest.equals("speed")) { //send "<speed>100;" in Serial monitor //the "speed" process motorSpeed=mytest.read(); Serial.print("speed:"); Serial.println(motorSpeed); } else if (mytest.equals("destination")){ //send "<destination>23,56;" in Serial monitor //the "destination" process motorDestination[0]=mytest.read(); motorDestination[1]=mytest.read(); Serial.println("destination:"); Serial.print(" X:"); Serial.print(motorDestination[0]); Serial.print(" Y:"); Serial.println(motorDestination[1]); } else if (mytest.equals("display")){ //send "<displayHello World!>10,35;" in Serial monitor //the "destination" process displayString=mytest.readString(); displayPosition[0]=mytest.read(); displayPosition[1]=mytest.read(); Serial.println("display:"); Serial.print("displayString:"); Serial.println(displayString); Serial.print(" X:"); Serial.print(displayPosition[0]); Serial.print(" Y:"); Serial.println(displayPosition[1]); } else{ //no matching command } } }
Author:Angelo