Andreas Hatzl
/
test_150615
asdf
main.cpp@0:24f914e903b4, 2015-06-10 (annotated)
- Committer:
- andreashatzl
- Date:
- Wed Jun 10 15:36:18 2015 +0000
- Revision:
- 0:24f914e903b4
- Child:
- 1:756c9df9881f
GruppeA
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andreashatzl | 0:24f914e903b4 | 1 | /* |
andreashatzl | 0:24f914e903b4 | 2 | Serial Communication with a PC |
andreashatzl | 0:24f914e903b4 | 3 | The mbed Microcontroller can communicate with a host PC through a "USB Virtual Serial Port" |
andreashatzl | 0:24f914e903b4 | 4 | over the same USB cable that is used for programming. |
andreashatzl | 0:24f914e903b4 | 5 | This enables you to: |
andreashatzl | 0:24f914e903b4 | 6 | a) Print out messages to a host PC terminal (useful for debugging!) |
andreashatzl | 0:24f914e903b4 | 7 | b) Read input from the host PC keyboard |
andreashatzl | 0:24f914e903b4 | 8 | c) Communicate with applications and programming languages running on the host PC |
andreashatzl | 0:24f914e903b4 | 9 | that can communicate with a serial port, e.g. perl, python, java, c# and so on. |
andreashatzl | 0:24f914e903b4 | 10 | .. see http://mbed.org/handbook/SerialPC |
andreashatzl | 0:24f914e903b4 | 11 | |
andreashatzl | 0:24f914e903b4 | 12 | The C stdin, stdout and stderr file handles are also defaulted to the PC serial connection |
andreashatzl | 0:24f914e903b4 | 13 | standard calls are: printf, scanf, getc, putc, .. |
andreashatzl | 0:24f914e903b4 | 14 | */ |
andreashatzl | 0:24f914e903b4 | 15 | |
andreashatzl | 0:24f914e903b4 | 16 | #include "mbed.h" |
andreashatzl | 0:24f914e903b4 | 17 | // global vars and objects |
andreashatzl | 0:24f914e903b4 | 18 | BusOut doLeds(LED1,LED2,LED3,LED4); |
andreashatzl | 0:24f914e903b4 | 19 | DigitalOut led2(LED2); |
andreashatzl | 0:24f914e903b4 | 20 | Serial pc(USBTX, USBRX); // tx, rx ; is default !!! (9600, 8N1) |
andreashatzl | 0:24f914e903b4 | 21 | char recChar=0; |
andreashatzl | 0:24f914e903b4 | 22 | bool recFlag=false; |
andreashatzl | 0:24f914e903b4 | 23 | char recArr[20]; |
andreashatzl | 0:24f914e903b4 | 24 | int index=0; |
andreashatzl | 0:24f914e903b4 | 25 | |
andreashatzl | 0:24f914e903b4 | 26 | // functions |
andreashatzl | 0:24f914e903b4 | 27 | void flushSerialBuffer() { |
andreashatzl | 0:24f914e903b4 | 28 | while (pc.readable()) { |
andreashatzl | 0:24f914e903b4 | 29 | pc.getc(); |
andreashatzl | 0:24f914e903b4 | 30 | } |
andreashatzl | 0:24f914e903b4 | 31 | } |
andreashatzl | 0:24f914e903b4 | 32 | |
andreashatzl | 0:24f914e903b4 | 33 | void readData() { |
andreashatzl | 0:24f914e903b4 | 34 | recChar = pc.getc(); |
andreashatzl | 0:24f914e903b4 | 35 | recArr[index] = recChar; |
andreashatzl | 0:24f914e903b4 | 36 | index++; |
andreashatzl | 0:24f914e903b4 | 37 | |
andreashatzl | 0:24f914e903b4 | 38 | recFlag = true; |
andreashatzl | 0:24f914e903b4 | 39 | recArr[index] = 0; |
andreashatzl | 0:24f914e903b4 | 40 | index = 0; |
andreashatzl | 0:24f914e903b4 | 41 | pc.printf(" - That's the input: %s\r\n", recArr); |
andreashatzl | 0:24f914e903b4 | 42 | |
andreashatzl | 0:24f914e903b4 | 43 | if(recArr[0]=='a') |
andreashatzl | 0:24f914e903b4 | 44 | { |
andreashatzl | 0:24f914e903b4 | 45 | doLeds = 0; |
andreashatzl | 0:24f914e903b4 | 46 | }; |
andreashatzl | 0:24f914e903b4 | 47 | if(recArr[0]=='b') |
andreashatzl | 0:24f914e903b4 | 48 | { |
andreashatzl | 0:24f914e903b4 | 49 | doLeds = 16; |
andreashatzl | 0:24f914e903b4 | 50 | }; |
andreashatzl | 0:24f914e903b4 | 51 | |
andreashatzl | 0:24f914e903b4 | 52 | flushSerialBuffer(); |
andreashatzl | 0:24f914e903b4 | 53 | |
andreashatzl | 0:24f914e903b4 | 54 | |
andreashatzl | 0:24f914e903b4 | 55 | } |
andreashatzl | 0:24f914e903b4 | 56 | |
andreashatzl | 0:24f914e903b4 | 57 | int main() { |
andreashatzl | 0:24f914e903b4 | 58 | // pc.baud(115200); |
andreashatzl | 0:24f914e903b4 | 59 | pc.baud(38400); |
andreashatzl | 0:24f914e903b4 | 60 | pc.format(8, SerialBase::Odd, 2); |
andreashatzl | 0:24f914e903b4 | 61 | led2 = 1; |
andreashatzl | 0:24f914e903b4 | 62 | |
andreashatzl | 0:24f914e903b4 | 63 | flushSerialBuffer(); |
andreashatzl | 0:24f914e903b4 | 64 | pc.printf("Hatzl Andreas: Gruppe A\r\n Bitte geben Sie einen Befehl ein:\r\n"); |
andreashatzl | 0:24f914e903b4 | 65 | pc.attach(&readData); |
andreashatzl | 0:24f914e903b4 | 66 | |
andreashatzl | 0:24f914e903b4 | 67 | while(1) { |
andreashatzl | 0:24f914e903b4 | 68 | if (recFlag) { |
andreashatzl | 0:24f914e903b4 | 69 | flushSerialBuffer(); |
andreashatzl | 0:24f914e903b4 | 70 | // pc.printf(" - That's the input: %s\r\n", recArr); // non reantrant function |
andreashatzl | 0:24f914e903b4 | 71 | recFlag = false; |
andreashatzl | 0:24f914e903b4 | 72 | led2 = !led2; |
andreashatzl | 0:24f914e903b4 | 73 | } |
andreashatzl | 0:24f914e903b4 | 74 | } |
andreashatzl | 0:24f914e903b4 | 75 | } |