Cortex Challenge Team / Mbed 2 deprecated Nucleo-UART

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************************************
00002 * @file    main.cpp
00003 * @author  Petr Dousa, Daniel Toms
00004 * @version V0.1
00005 * @date    05-March-2015
00006 * @brief   Nucleo UART example - sends and receives data over serial
00007 ***********************************************************************************/
00008 
00009 /* Includes ----------------------------------------------------------------------*/
00010 #include "mbed.h"
00011 #include <string>
00012 
00013 #define INPUT_BUFFER_SIZE 256
00014 
00015 void flushSerialPort();
00016 
00017 Serial pc(SERIAL_TX, SERIAL_RX);
00018 
00019 DigitalOut myled(LED1);
00020 
00021 int main()
00022 {
00023     int i;
00024     char text[INPUT_BUFFER_SIZE];       //input buffer
00025     
00026     //------------------------------------
00027     // Hyperterminal configuration
00028     // 9600 bauds, 8-bit data, no parity
00029     // --------------------------------
00030     // Communication settings:
00031     // pc.format(bits,parity,stop_bits)
00032     //      bits: 5-8
00033     //      parity: SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0
00034     //      stop_bits: 1 or 2
00035     // pc.baudrate(baud)
00036     //      baud: The desired baudrate (speed)
00037     // --------------------------------
00038     
00039     pc.format(8,SerialBase::None,1);    // This is the default setting
00040     pc.baud(9600);                      // This is the default setting
00041     
00042     //Sends a welcome message over UART (Serial port)
00043     //Checks if the port is writeable first
00044     if (pc.writeable())
00045         pc.printf("Hello World !\n-----------------\nWrite something...\n");
00046     
00047     while(1) {
00048         
00049         wait(1);
00050         
00051         myled = !myled;                             //blinking led indicates activity
00052                 
00053         //NOTE: the data must be terminated with CR(Carriage return) '\r'
00054         //      You can however choose a different terminating character.
00055         
00056         i=0;
00057         while ((text[i-1] != '\r') && (i<0xFF))
00058         {
00059             if (pc.readable())                      //Checks if there are data to be received and stores them into the input buffer
00060             {    
00061                 text[i++] = getc(pc);               //Note: you could use pc.scanf() instead of getc.
00062                                                     //pc.scanf() has the same syntax like in regular C apps.
00063             }           
00064         }
00065         
00066         if (pc.writeable()) 
00067         {
00068             pc.printf("Received data: %s", text);   //Sends the data back for confirmation.
00069             memset(&text, 0, i);
00070         }
00071         
00072         //flushSerialPort(pc);                      //Erases all the data waiting to be received
00073         
00074     }
00075 }
00076 
00077 //Erases all the data waiting to be received
00078 void flushSerialPort()
00079 {
00080 
00081     while(pc.readable()) 
00082         pc.getc();
00083     
00084     return;
00085 }