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

Dependencies:   mbed

main.cpp

Committer:
Foxnec
Date:
2015-03-05
Revision:
2:bdc32e32f6f1
Parent:
1:a149bb0c3d05
Child:
3:d470d455b369

File content as of revision 2:bdc32e32f6f1:

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

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

Serial pc(SERIAL_TX, SERIAL_RX);

DigitalOut myled(LED1);

int main()
{
    int i;
    char text[0xFF];
    
    
    pc.baud(115200);
    i=1;
    pc.printf("Hello World !\n-----------------\nYou can tell me anything and I will repeat it...\n");
    
    while(1) {
        
        wait(1);
        
        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') && (i<0xFF))
        {
            if (pc.readable())
                text[i++] = getc(pc);
                        
        }
        
        pc.printf("Received data: %s", text);
        memset(&text, 0, i);
        //flushSerialPort(pc);
        
    }
}

void flushSerialPort()
{

    while(pc.readable()) 
        pc.getc();
    
    return;
}