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

Committer:
p3p
Date:
Thu Nov 24 23:20:44 2011 +0000
Revision:
0:109a0b974600
Child:
2:8799090c0fe4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p3p 0:109a0b974600 1 #include "mbed.h"
p3p 0:109a0b974600 2 #include "Protocol.h"
p3p 0:109a0b974600 3
p3p 0:109a0b974600 4 //class will receive a packet and echo it back out
p3p 0:109a0b974600 5 class TestProtocol : public SimpleSerialProtocol::Protocol {
p3p 0:109a0b974600 6 public:
p3p 0:109a0b974600 7 TestProtocol() : Protocol(p9, p10, NC) { //LED1 to 4 for a status led, NC to disable
p3p 0:109a0b974600 8 _dma_port = 1; //set the dma port, must be unique per class 0 - 9
p3p 0:109a0b974600 9 }
p3p 0:109a0b974600 10 virtual ~TestProtocol() {};
p3p 0:109a0b974600 11
p3p 0:109a0b974600 12 #pragma pack(push, 1) //must pack the structure to byte boundary for raw recast to work reliably
p3p 0:109a0b974600 13 struct PacketInterface {
p3p 0:109a0b974600 14 PacketInterface() {
p3p 0:109a0b974600 15 type = 1; // initialise the type
p3p 0:109a0b974600 16 }
p3p 0:109a0b974600 17 uint8_t type;
p3p 0:109a0b974600 18 uint8_t data;
p3p 0:109a0b974600 19 uint16_t datashort;
p3p 0:109a0b974600 20 uint32_t dataint;
p3p 0:109a0b974600 21 float datafloat;
p3p 0:109a0b974600 22 };
p3p 0:109a0b974600 23 #pragma pack(pop)
p3p 0:109a0b974600 24
p3p 0:109a0b974600 25 virtual void decode() {
p3p 0:109a0b974600 26 switch (_packet._type) {
p3p 0:109a0b974600 27 case 1:
p3p 0:109a0b974600 28 if (_packet._size == sizeof( PacketInterface)) { //check that the packet in the correct size to avoid memory corruption
p3p 0:109a0b974600 29 PacketInterface* message;
p3p 0:109a0b974600 30 message = reinterpret_cast<PacketInterface*>(_packet._data); //cast the raw data to the packet struct
p3p 0:109a0b974600 31
p3p 0:109a0b974600 32 //use the data through the struct interface
p3p 0:109a0b974600 33 uint8_t temp = message->data;
p3p 0:109a0b974600 34 short temp1 = message->datashort;
p3p 0:109a0b974600 35 int temp2 = message->dataint;
p3p 0:109a0b974600 36 float temp3 = message->datafloat;
p3p 0:109a0b974600 37
p3p 0:109a0b974600 38 PacketInterface sendMessage; // initialise a packet to send
p3p 0:109a0b974600 39 sendMessage.data = temp;
p3p 0:109a0b974600 40 sendMessage.datashort = temp1;
p3p 0:109a0b974600 41 sendMessage.dataint = temp2;
p3p 0:109a0b974600 42 sendMessage.datafloat = temp3;
p3p 0:109a0b974600 43 sendPacket<PacketInterface>(&sendMessage); //send the packet (async)
p3p 0:109a0b974600 44 }
p3p 0:109a0b974600 45 break;
p3p 0:109a0b974600 46 default:
p3p 0:109a0b974600 47 break;
p3p 0:109a0b974600 48 }
p3p 0:109a0b974600 49 }
p3p 0:109a0b974600 50 };
p3p 0:109a0b974600 51
p3p 0:109a0b974600 52 TestProtocol testProtocol;
p3p 0:109a0b974600 53
p3p 0:109a0b974600 54 //the main loop
p3p 0:109a0b974600 55 int main() {
p3p 0:109a0b974600 56 testProtocol.initialise();
p3p 0:109a0b974600 57 while (1) {
p3p 0:109a0b974600 58 testProtocol.update();
p3p 0:109a0b974600 59 }
p3p 0:109a0b974600 60 }