An example Program for the SimpleSerialProtocol Library, This program will receive a packet, then echo it back to the client

Dependencies:   mbed SimpleSerialProtocol MODSERIAL

A simple example program that receives a packet over serial and echos it back.

I include this java program to show an example client application, all this program does is send packets as fast as it can without filling up its output buffer, the mbed will echo these packets back.

This is a good benchmark of the serial connection, and should show about 11KB/s at 115200baud

/media/uploads/p3p/serialecho.zip

example command: java -jar SerialEcho.jar com3 115200

Files at this revision

API Documentation at this revision

Comitter:
p3p
Date:
Wed Aug 27 18:05:51 2014 +0000
Parent:
10:f6862abba2d5
Child:
12:1d1d8425c79c
Commit message:
Simplified

Changed in this revision

MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
SimpleSerialProtocol.lib Show annotated file Show diff for this revision Revisions of this file
TestProtocol.cpp Show annotated file Show diff for this revision Revisions of this file
TestProtocol.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/MODSERIAL.lib	Sun Jul 29 19:46:09 2012 +0000
+++ b/MODSERIAL.lib	Wed Aug 27 18:05:51 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/AjK/code/MODSERIAL/#5c45c21f36b7
+http://mbed.org/users/AjK/code/MODSERIAL/#ae0408ebdd68
--- a/SimpleSerialProtocol.lib	Sun Jul 29 19:46:09 2012 +0000
+++ b/SimpleSerialProtocol.lib	Wed Aug 27 18:05:51 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/p3p/code/SimpleSerialProtocol/#1639507580d5
+http://mbed.org/users/p3p/code/SimpleSerialProtocol/#98ad30934a71
--- a/TestProtocol.cpp	Sun Jul 29 19:46:09 2012 +0000
+++ b/TestProtocol.cpp	Wed Aug 27 18:05:51 2014 +0000
@@ -1,6 +1,6 @@
 #include "TestProtocol.h"
 
-void TestProtocol::onEchoPacket(SimpleSerialProtocol::Packet* packet){
+void TestProtocol::onEchoPacket(SimpleSerialProtocol::Protocol* comms, SimpleSerialProtocol::Packet* packet){
     if(!packet)return;
     if (packet->_valid) {
         EchoPacket::Interface* interface = packet->interpretData<EchoPacket::Interface>();
@@ -9,18 +9,16 @@
             temp1 = interface->datashort;
             temp2 = interface->dataint;
             temp3 = interface->datafloat;
-            reply();
+            
+            //we can use the Protocol pointer to send a response from the callback
+            EchoPacket echoMessage; // initialise a packet to send
+            echoMessage.interface.data = temp;
+            echoMessage.interface.datashort = temp1;
+            echoMessage.interface.dataint = temp2;
+            echoMessage.interface.datafloat = temp3;
+            echoMessage.buildData<EchoPacket::Interface>(&echoMessage.interface);
+            comms->sendPacket(&echoMessage); //send the packet (async)
         }
     }
     return;
-}
-
-void TestProtocol::reply() {
-    EchoPacket echoMessage; // initialise a packet to send
-    echoMessage.interface.data = temp;
-    echoMessage.interface.datashort = temp1;
-    echoMessage.interface.dataint = temp2;
-    echoMessage.interface.datafloat = temp3;
-    echoMessage.buildData<EchoPacket::Interface>(&echoMessage.interface);
-    sendPacket(&echoMessage); //send the packet (async)
 }
\ No newline at end of file
--- a/TestProtocol.h	Sun Jul 29 19:46:09 2012 +0000
+++ b/TestProtocol.h	Wed Aug 27 18:05:51 2014 +0000
@@ -5,14 +5,16 @@
 #include <SimpleSerialProtocol/Protocol.h>
 
 //class will receive a packet and echo it back out
-class TestProtocol : public SimpleSerialProtocol::Protocol {
+class TestProtocol {
 public:
-    TestProtocol() : Protocol(USBTX, USBRX, LED1) { //LED1 to 4 for a status led, NC to disable
-        receiveCallback(1, this, &TestProtocol::onEchoPacket);
+    TestProtocol() {
+        temp = 0;
+        temp1 = 0;
+        temp2 = 0;
+        temp3 = 0.0f;
     }
     virtual ~TestProtocol() {};
-    void onEchoPacket(SimpleSerialProtocol::Packet* packet);
-    void reply();
+    void onEchoPacket(SimpleSerialProtocol::Protocol* comms, SimpleSerialProtocol::Packet* packet);
 
     class EchoPacket : public SimpleSerialProtocol::Packet {
     public:
@@ -20,6 +22,7 @@
         virtual ~EchoPacket() {}
         
 #pragma pack(push, 1) //must pack the structure to byte boundary for raw recast to work reliably
+                      //this is used in Packet::interpretData and used to validate the packet
         struct Interface {
             Interface() {
                 type = 1; // initialise the type
@@ -35,8 +38,8 @@
     };
 
     uint8_t temp;
-    short temp1;
-    int temp2;
+    uint16_t temp1;
+    uint32_t temp2;
     float temp3;
 };
 
--- a/main.cpp	Sun Jul 29 19:46:09 2012 +0000
+++ b/main.cpp	Wed Aug 27 18:05:51 2014 +0000
@@ -17,27 +17,15 @@
 //    }
 //    return tmp_checksum;
 
-Serial debug(p28, p27);
-
 TestProtocol testProtocol;
+SimpleSerialProtocol::Protocol comms(USBTX, USBRX, LED1);
 
 //the main loop
 int main() {
-    testProtocol.initialise();
-    
-    debug.baud(115200);
-    debug.printf("Debug Console:\r\n");
-
-    testProtocol.printf("Hello printf\r\n");
-    testProtocol.printf("Hello printf %s\r\n", "booyaa");
-    testProtocol.puts("Hello puts\r\n");
-
-    testProtocol.putc('H');
-    testProtocol.puts("\r\n");
-
-    testProtocol.puts("SimpleSerialProtocol Serial io overrides tested\r\n");
-
+    comms.initialise(115200);
+    comms.receiveCallback(1, &testProtocol, &TestProtocol::onEchoPacket);
+        
     while (1) {
-        testProtocol.update();
+        comms.update();
     }
 }
--- a/mbed.bld	Sun Jul 29 19:46:09 2012 +0000
+++ b/mbed.bld	Wed Aug 27 18:05:51 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/10b9abbe79a6
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013
\ No newline at end of file