A trivial example of a UART comm. Works on all Nucleos.

Dependencies:   mbed

main.cpp

Committer:
Foxnec
Date:
2015-03-05
Revision:
1:a149bb0c3d05
Parent:
0:389db7a2ed7f
Child:
2:bdc32e32f6f1

File content as of revision 1:a149bb0c3d05:

#include "mbed.h"
#include <string>

//------------------------------------
// Hyperterminal configuration
// 115200 bauds, 8-bit data, no parity
//------------------------------------
void flushSerialPort(Serial* port);

Serial pc(SERIAL_TX, SERIAL_RX);

DigitalOut myled(LED1);

int main()
{
    int i;
    char text[255];
    
    
    pc.baud(115200);
    i=1;
    pc.printf("Hello World !\n");
    
    
    while(1) {
        
        wait(1);
        pc.printf("You can tell me anything and I will repeat it...\n");
        myled = !myled;
        
        
        //NOTE: the data must be terminated with CR(Carriage return)
        //NOTE: I had no luck using scanf() - it just scans an empty string.
        i=0;
        while (text[i-1] != '\r'){
            if (pc.readable()){
                text[i++] = getc(pc);
            }            
        }
        
        pc.printf("Received data: %s", text);
        memset(&text, 0, i);
        //flushSerialPort(&pc);
        
    }
}

void flushSerialPort(Serial* port)
{

    while((*port).readable()) {
        (*port).getc();
    }

    return;
}