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

main.cpp

Committer:
p3p
Date:
2011-11-24
Revision:
0:109a0b974600
Child:
2:8799090c0fe4

File content as of revision 0:109a0b974600:

#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();
    }
}