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:
Mon Jan 16 22:46:45 2012 +0000
Parent:
1:c007775d6d21
Child:
3:8ac7e37d0e0e
Commit message:
Modified to reflect changes in the SimpleSerialProtocol Library

Changed in this revision

MODDMA.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/MODDMA.lib	Thu Nov 24 23:21:44 2011 +0000
+++ b/MODDMA.lib	Mon Jan 16 22:46:45 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/AjK/code/MODDMA/#cb10aec6feb1
+http://mbed.org/users/p3p/code/MODDMA/#558d6dacfb89
--- a/SimpleSerialProtocol.lib	Thu Nov 24 23:21:44 2011 +0000
+++ b/SimpleSerialProtocol.lib	Mon Jan 16 22:46:45 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/p3p/code/SimpleSerialProtocol/#363a3b7ca868
+http://mbed.org/users/p3p/code/SimpleSerialProtocol/#86435f2828f8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestProtocol.cpp	Mon Jan 16 22:46:45 2012 +0000
@@ -0,0 +1,26 @@
+#include "TestProtocol.h"
+
+uint32_t TestProtocol::onEchoPacket(uint32_t packet_ptr_value) {
+    EchoPacket *packet = (EchoPacket*)packet_ptr_value;
+    if (packet->_valid) {
+        EchoPacket::Interface* interface = packet->interpretData<EchoPacket::Interface>();
+        if (interface) {
+            temp = interface->data;
+            temp1 = interface->datashort;
+            temp2 = interface->dataint;
+            temp3 = interface->datafloat;
+            reply();
+        }
+    }
+    return 0;
+}
+
+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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestProtocol.h	Mon Jan 16 22:46:45 2012 +0000
@@ -0,0 +1,44 @@
+#ifndef _TEST_PROTOCOL_H
+#define _TEST_PROTOCOL_H
+
+#include <mbed.h>
+#include <SimpleSerialProtocol/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 = 0; //set the dma port, must be unique per class 0 - 9
+        packetCallback(1, this, &TestProtocol::onEchoPacket);
+    }
+    virtual ~TestProtocol() {};
+    uint32_t onEchoPacket(uint32_t packet_ptr_value);
+    void reply();
+
+    class EchoPacket : public SimpleSerialProtocol::Packet {
+    public:
+        EchoPacket() {}
+        virtual ~EchoPacket() {}
+        
+#pragma pack(push, 1) //must pack the structure to byte boundary for raw recast to work reliably
+        struct Interface {
+            Interface() {
+                type = 1; // initialise the type
+            }
+            uint8_t type;
+            uint8_t data;
+            uint16_t datashort;
+            uint32_t dataint;
+            float datafloat;
+        } interface;
+#pragma pack(pop)
+
+    };
+
+    uint8_t temp;
+    short temp1;
+    int temp2;
+    float temp3;
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Thu Nov 24 23:21:44 2011 +0000
+++ b/main.cpp	Mon Jan 16 22:46:45 2012 +0000
@@ -1,60 +1,28 @@
-#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();
-    }
-}
+#include "mbed.h"
+#include "Protocol.h"
+#include "TestProtocol.h"
+
+//valid test packet bytes
+//
+// 255 127 // packet start 2 bytes 
+// 12 // payload size 1 byte
+// 0 0 0 0 0 0 0 0 0 0 0 0 // payload data
+// 16 // checksum 1 byte
+
+//checksum calculation
+//
+//    uint8_t tmp_checksum = 16;
+//    for (int i = 0; i < packet_size; i++) {
+//        tmp_checksum ^= packet[i];
+//    }
+//    return tmp_checksum;
+
+TestProtocol testProtocol;
+
+//the main loop
+int main() {
+    testProtocol.initialise();    
+    while (1) {
+        testProtocol.update();
+    }
+}
--- a/mbed.bld	Thu Nov 24 23:21:44 2011 +0000
+++ b/mbed.bld	Mon Jan 16 22:46:45 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
+http://mbed.org/users/mbed_official/code/mbed/builds/7110ebee3484