
A trivial example of a UART comm. Works on all Nucleos.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 2:bdc32e32f6f1
- Parent:
- 1:a149bb0c3d05
- Child:
- 3:d470d455b369
--- a/main.cpp Thu Mar 05 21:56:11 2015 +0000 +++ b/main.cpp Thu Mar 05 22:09:54 2015 +0000 @@ -5,7 +5,7 @@ // Hyperterminal configuration // 115200 bauds, 8-bit data, no parity //------------------------------------ -void flushSerialPort(Serial* port); +void flushSerialPort(); Serial pc(SERIAL_TX, SERIAL_RX); @@ -14,43 +14,42 @@ int main() { int i; - char text[255]; + char text[0xFF]; pc.baud(115200); i=1; - pc.printf("Hello World !\n"); - + pc.printf("Hello World !\n-----------------\nYou can tell me anything and I will repeat it...\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()){ + 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); + //flushSerialPort(pc); } } -void flushSerialPort(Serial* port) +void flushSerialPort() { - while((*port).readable()) { - (*port).getc(); - } - + while(pc.readable()) + pc.getc(); + return; }