Just a small test for frames and serial (RS232) communication, with ideas from http://eli.thegreenplace.net/2009/08/20/frames-and-protocols-for-the-serial-port-in-python/

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "parser.h"
00003 #include "string"
00004 #include "list"
00005 
00006 Serial pc(USBTX, USBRX);    // initialize RS232 via USB
00007 
00008 DigitalOut prg_activity_led(LED1);     // some LED to show activity
00009 DigitalOut rx_activity_led(LED2);
00010 DigitalOut tx_activity_led(LED3);
00011 DigitalOut message_led(LED4);
00012 
00013 // we need some storage for the messages we want to send and receive
00014 string singleSendMessageBuffer;
00015 string singleReceiveMessageBuffer;
00016 // and a buffer for those messages, until they are processed
00017 list<string> sendMessagesBuffer;
00018 list<string> receiveMessagesBuffer;
00019 
00020 
00021 // Interupt Routine to read in data from serial port
00022 void Rx_interrupt() {
00023     rx_activity_led=!rx_activity_led;
00024     // Loop just in case more than one character is in UART's receive FIFO buffer
00025     while (pc.readable()) {
00026         char c = pc.getc();
00027         unwrap_protocol(&c, &singleReceiveMessageBuffer, &receiveMessagesBuffer);
00028         // pc.putc(c); // echo
00029     }
00030     return;
00031 }
00032 
00033 
00034 // Interupt Routine to write out data to serial port
00035 void Tx_interrupt() {
00036     char c;
00037     int finished = WRAP_SEND;
00038     int messageAvailable = !sendMessagesBuffer.empty();
00039 
00040     tx_activity_led=!tx_activity_led;
00041 
00042     // Loop to fill more than one character in UART's transmit FIFO buffer
00043     while (  pc.writeable() && messageAvailable && (finished == WRAP_SEND)) {
00044         finished = wrap_protocol( &c, &singleSendMessageBuffer, &sendMessagesBuffer );
00045         if (finished != WRAP_ABORT)
00046             pc.putc(c);
00047     }
00048 
00049     //tx_activity_led=0;
00050     return;
00051 }
00052 
00053 
00054 int main() {
00055 
00056     string sendString;
00057 
00058     pc.baud(115200);        // other high speed: 921600
00059 
00060     // Setup a serial interrupt function to receive data
00061     pc.attach(&Rx_interrupt, Serial::RxIrq);
00062     // Setup a serial interrupt function to transmit data
00063     pc.attach(&Tx_interrupt, Serial::TxIrq);
00064     // enable interrupt
00065     NVIC_EnableIRQ(UART1_IRQn);
00066 
00067     set_time(0);            // needed to initialize rtc
00068 
00069     while (1) {
00070         prg_activity_led = !prg_activity_led;       // toggle LED
00071         time_t seconds = time(NULL);    // get current time
00072 
00073         //printf("Time as a string = %s\r\n", ctime(&seconds));
00074 
00075         if (!receiveMessagesBuffer.empty()) {
00076             //pc.printf("\r\nMessage: %s\r\n", receiveMessagesBuffer.front().c_str());
00077 
00078             sendString = "Message '";
00079             sendString += receiveMessagesBuffer.front().c_str();
00080             sendString += "' received at ";
00081             sendString += ctime(&seconds);
00082             sendString += "\r";
00083 
00084             sendMessagesBuffer.push_back(sendString);
00085             receiveMessagesBuffer.pop_front();
00086 
00087         } else {
00088             //sendString = "Hallo Welt!\r\n";
00089             sendString = ctime(&seconds);
00090             sendString += "\r";
00091             sendMessagesBuffer.push_back(sendString);
00092         }
00093         
00094         
00095         // trigger sending interrupt
00096         char c;
00097         int finished = wrap_protocol( &c, &singleSendMessageBuffer, &sendMessagesBuffer );
00098         if (finished != WRAP_ABORT)
00099             pc.putc(c);
00100 
00101         wait(1); // wait and don't flood the RS232
00102     }
00103 }