yxfyx
Dependencies: mbed
main.cpp@0:a01301f7a406, 2015-06-09 (annotated)
- Committer:
- AndreasSchoegler
- Date:
- Tue Jun 09 17:44:38 2015 +0000
- Revision:
- 0:a01301f7a406
fh
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AndreasSchoegler | 0:a01301f7a406 | 1 | #include "mbed.h" |
AndreasSchoegler | 0:a01301f7a406 | 2 | // global vars and objects |
AndreasSchoegler | 0:a01301f7a406 | 3 | DigitalOut led2(LED2); |
AndreasSchoegler | 0:a01301f7a406 | 4 | Serial pc(USBTX, USBRX); // tx, rx ; is default !!! (9600, 8N1) |
AndreasSchoegler | 0:a01301f7a406 | 5 | char recChar=0; |
AndreasSchoegler | 0:a01301f7a406 | 6 | bool recFlag=false; |
AndreasSchoegler | 0:a01301f7a406 | 7 | char recArr[20]; |
AndreasSchoegler | 0:a01301f7a406 | 8 | int index=0; |
AndreasSchoegler | 0:a01301f7a406 | 9 | |
AndreasSchoegler | 0:a01301f7a406 | 10 | // functions |
AndreasSchoegler | 0:a01301f7a406 | 11 | void flushSerialBuffer() { |
AndreasSchoegler | 0:a01301f7a406 | 12 | while (pc.readable()) { |
AndreasSchoegler | 0:a01301f7a406 | 13 | pc.getc(); |
AndreasSchoegler | 0:a01301f7a406 | 14 | } |
AndreasSchoegler | 0:a01301f7a406 | 15 | } |
AndreasSchoegler | 0:a01301f7a406 | 16 | |
AndreasSchoegler | 0:a01301f7a406 | 17 | void readData() { |
AndreasSchoegler | 0:a01301f7a406 | 18 | recChar = pc.getc(); |
AndreasSchoegler | 0:a01301f7a406 | 19 | recArr[index] = recChar; |
AndreasSchoegler | 0:a01301f7a406 | 20 | index++; |
AndreasSchoegler | 0:a01301f7a406 | 21 | if ((recChar == '\r') || (index>=19)) { |
AndreasSchoegler | 0:a01301f7a406 | 22 | recFlag = true; |
AndreasSchoegler | 0:a01301f7a406 | 23 | recArr[index] = 0; |
AndreasSchoegler | 0:a01301f7a406 | 24 | index = 0; |
AndreasSchoegler | 0:a01301f7a406 | 25 | pc.printf(" - That's the input: %s\r\n", recArr); |
AndreasSchoegler | 0:a01301f7a406 | 26 | flushSerialBuffer(); |
AndreasSchoegler | 0:a01301f7a406 | 27 | } |
AndreasSchoegler | 0:a01301f7a406 | 28 | } |
AndreasSchoegler | 0:a01301f7a406 | 29 | |
AndreasSchoegler | 0:a01301f7a406 | 30 | int main() { |
AndreasSchoegler | 0:a01301f7a406 | 31 | // pc.baud(115200); |
AndreasSchoegler | 0:a01301f7a406 | 32 | pc.baud(38400); |
AndreasSchoegler | 0:a01301f7a406 | 33 | pc.format(8, SerialBase::Odd, 2); |
AndreasSchoegler | 0:a01301f7a406 | 34 | led2 = 1; |
AndreasSchoegler | 0:a01301f7a406 | 35 | |
AndreasSchoegler | 0:a01301f7a406 | 36 | flushSerialBuffer(); |
AndreasSchoegler | 0:a01301f7a406 | 37 | pc.printf("Hello RS232-USB Virtual Serial Port -World!\r\n"); |
AndreasSchoegler | 0:a01301f7a406 | 38 | pc.attach(&readData); |
AndreasSchoegler | 0:a01301f7a406 | 39 | |
AndreasSchoegler | 0:a01301f7a406 | 40 | while(1) { |
AndreasSchoegler | 0:a01301f7a406 | 41 | if (recFlag) { |
AndreasSchoegler | 0:a01301f7a406 | 42 | flushSerialBuffer(); |
AndreasSchoegler | 0:a01301f7a406 | 43 | // pc.printf(" - That's the input: %s\r\n", recArr); // non reantrant function |
AndreasSchoegler | 0:a01301f7a406 | 44 | recFlag = false; |
AndreasSchoegler | 0:a01301f7a406 | 45 | led2 = !led2; |
AndreasSchoegler | 0:a01301f7a406 | 46 | } |
AndreasSchoegler | 0:a01301f7a406 | 47 | } |
AndreasSchoegler | 0:a01301f7a406 | 48 | } |