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:
Thu Nov 24 23:20:44 2011 +0000
Child:
1:c007775d6d21
Commit message:

Changed in this revision

MODDMA.lib Show annotated file Show diff for this revision Revisions of this file
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
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODDMA.lib	Thu Nov 24 23:20:44 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODDMA/#cb10aec6feb1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Thu Nov 24 23:20:44 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#af2af4c61c2f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleSerialProtocol.lib	Thu Nov 24 23:20:44 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/p3p/code/SimpleSerialProtocol/#bfb164b5d829
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 24 23:20:44 2011 +0000
@@ -0,0 +1,60 @@
+#include "mbed.h"
+#include "Protocol.h"
+
+//class will receive a packet and echo it back out
+class TestProtocol : public SimpleSerialProtocol::Protocol {
+public:
+    TestProtocol() : Protocol(p9, p10, NC) { //LED1 to 4 for a status led, NC to disable
+        _dma_port = 1; //set the dma port, must be unique per class 0 - 9
+    }
+    virtual ~TestProtocol() {};
+
+#pragma pack(push, 1) //must pack the structure to byte boundary for raw recast to work reliably
+    struct PacketInterface {
+        PacketInterface() {
+            type = 1; // initialise the type
+        }
+        uint8_t type;
+        uint8_t data;
+        uint16_t datashort;
+        uint32_t dataint;
+        float datafloat;
+    };
+#pragma pack(pop)
+
+    virtual void decode() {
+        switch (_packet._type) {
+            case 1:
+                if (_packet._size == sizeof( PacketInterface)) { //check that the packet in the correct size to avoid memory corruption
+                    PacketInterface* message;
+                    message = reinterpret_cast<PacketInterface*>(_packet._data); //cast the raw data to the packet struct
+                    
+                    //use the data through the struct interface
+                    uint8_t temp = message->data;
+                    short temp1 = message->datashort;
+                    int temp2 = message->dataint;
+                    float temp3 = message->datafloat;
+                    
+                    PacketInterface sendMessage; // initialise a packet to send
+                    sendMessage.data = temp;
+                    sendMessage.datashort = temp1;
+                    sendMessage.dataint = temp2;
+                    sendMessage.datafloat = temp3;
+                    sendPacket<PacketInterface>(&sendMessage); //send the packet (async)
+                }
+                break;
+            default:
+                break;
+        }
+    }
+};
+
+TestProtocol testProtocol;
+
+//the main loop
+int main() {
+    testProtocol.initialise();
+    while (1) {
+        testProtocol.update();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 24 23:20:44 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912