Matthias Planinsec
/
rs232_rxInt_reantrantProblem
2
Revision 0:ad5f7c242f1b, committed 2015-06-06
- Comitter:
- Planinsec
- Date:
- Sat Jun 06 10:38:27 2015 +0000
- Commit message:
- 2
Changed in this revision
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
rs232_rxInt_reantrantProblem.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r ad5f7c242f1b mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Jun 06 10:38:27 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/dbbf35b96557 \ No newline at end of file
diff -r 000000000000 -r ad5f7c242f1b rs232_rxInt_reantrantProblem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rs232_rxInt_reantrantProblem.cpp Sat Jun 06 10:38:27 2015 +0000 @@ -0,0 +1,63 @@ +/* +Serial Communication with a PC +The mbed Microcontroller can communicate with a host PC through a "USB Virtual Serial Port" +over the same USB cable that is used for programming. +This enables you to: +a) Print out messages to a host PC terminal (useful for debugging!) +b) Read input from the host PC keyboard +c) Communicate with applications and programming languages running on the host PC + that can communicate with a serial port, e.g. perl, python, java, c# and so on. +.. see http://mbed.org/handbook/SerialPC + +The C stdin, stdout and stderr file handles are also defaulted to the PC serial connection +standard calls are: printf, scanf, getc, putc, .. +*/ + +#include "mbed.h" +// global vars and objects +DigitalOut led2(LED2); +Serial pc(USBTX, USBRX); // tx, rx ; is default !!! (9600, 8N1) +char recChar=0; +bool recFlag=false; +char recArr[20]; +int index=0; + +// functions +void flushSerialBuffer() { + while (pc.readable()) { + pc.getc(); + } +} + +void readData() { + recChar = pc.getc(); + recArr[index] = recChar; + index++; + if ((recChar == '\r') || (index>=19)) { + recFlag = true; + recArr[index] = 0; + index = 0; + pc.printf(" - That's the input: %s\r\n", recArr); + flushSerialBuffer(); + } +} + +int main() { +// pc.baud(115200); + pc.baud(38400); + pc.format(8, SerialBase::Odd, 2); + led2 = 1; + + flushSerialBuffer(); + pc.printf("Hello RS232-USB Virtual Serial Port -World!\r\n"); + pc.attach(&readData); + + while(1) { + if (recFlag) { + flushSerialBuffer(); +// pc.printf(" - That's the input: %s\r\n", recArr); // non reantrant function + recFlag = false; + led2 = !led2; + } + } +}