![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demonstration of using serial port as console/terminal
Demonstration of using serial port with a dumb terminal. Such as teraterm. This example uses serial port at 57600bps.
purpose
provide quick/easy user interface.
features
- backspace key stops at start of line
- <enter> on empty line repeats last command
- Ctrl-C can be used to abort something
teraterm console
reset > ? . print status led toggle LED da<%d> get/set my_data > . LED1:0 my_data:0 > led > . LED1:1 my_data:0 > da data:0 > da1337 data:1337 > . LED1:1 my_data:1337 > led > > > >
main.cpp@0:9a142eb22e83, 2014-11-06 (annotated)
- Committer:
- dudmuck
- Date:
- Thu Nov 06 02:58:50 2014 +0000
- Revision:
- 0:9a142eb22e83
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudmuck | 0:9a142eb22e83 | 1 | #include "mbed.h" |
dudmuck | 0:9a142eb22e83 | 2 | |
dudmuck | 0:9a142eb22e83 | 3 | Serial pc(USBTX, USBRX); |
dudmuck | 0:9a142eb22e83 | 4 | char pcbuf[64]; |
dudmuck | 0:9a142eb22e83 | 5 | |
dudmuck | 0:9a142eb22e83 | 6 | DigitalOut myled(LED1); |
dudmuck | 0:9a142eb22e83 | 7 | |
dudmuck | 0:9a142eb22e83 | 8 | uint16_t my_data; |
dudmuck | 0:9a142eb22e83 | 9 | |
dudmuck | 0:9a142eb22e83 | 10 | void service_something() |
dudmuck | 0:9a142eb22e83 | 11 | { |
dudmuck | 0:9a142eb22e83 | 12 | /* poll something while waiting for keyboard */ |
dudmuck | 0:9a142eb22e83 | 13 | } |
dudmuck | 0:9a142eb22e83 | 14 | |
dudmuck | 0:9a142eb22e83 | 15 | int get_kbd_str(char* buf, int size) |
dudmuck | 0:9a142eb22e83 | 16 | { |
dudmuck | 0:9a142eb22e83 | 17 | char c; |
dudmuck | 0:9a142eb22e83 | 18 | int i; |
dudmuck | 0:9a142eb22e83 | 19 | static int prev_len; |
dudmuck | 0:9a142eb22e83 | 20 | |
dudmuck | 0:9a142eb22e83 | 21 | for (i = 0;;) { |
dudmuck | 0:9a142eb22e83 | 22 | if (pc.readable()) { |
dudmuck | 0:9a142eb22e83 | 23 | c = pc.getc(); |
dudmuck | 0:9a142eb22e83 | 24 | if (c == 8) { // backspace |
dudmuck | 0:9a142eb22e83 | 25 | if (i > 0) { |
dudmuck | 0:9a142eb22e83 | 26 | pc.putc(8); |
dudmuck | 0:9a142eb22e83 | 27 | pc.putc(' '); |
dudmuck | 0:9a142eb22e83 | 28 | pc.putc(8); |
dudmuck | 0:9a142eb22e83 | 29 | i--; |
dudmuck | 0:9a142eb22e83 | 30 | } |
dudmuck | 0:9a142eb22e83 | 31 | } else if (c == '\r') { |
dudmuck | 0:9a142eb22e83 | 32 | if (i == 0) { |
dudmuck | 0:9a142eb22e83 | 33 | return prev_len; // repeat previous |
dudmuck | 0:9a142eb22e83 | 34 | } else { |
dudmuck | 0:9a142eb22e83 | 35 | buf[i] = 0; // null terminate |
dudmuck | 0:9a142eb22e83 | 36 | prev_len = i; |
dudmuck | 0:9a142eb22e83 | 37 | return i; |
dudmuck | 0:9a142eb22e83 | 38 | } |
dudmuck | 0:9a142eb22e83 | 39 | } else if (c == 3) { |
dudmuck | 0:9a142eb22e83 | 40 | // ctrl-C abort |
dudmuck | 0:9a142eb22e83 | 41 | return -1; |
dudmuck | 0:9a142eb22e83 | 42 | } else if (i < size) { |
dudmuck | 0:9a142eb22e83 | 43 | buf[i++] = c; |
dudmuck | 0:9a142eb22e83 | 44 | pc.putc(c); |
dudmuck | 0:9a142eb22e83 | 45 | } |
dudmuck | 0:9a142eb22e83 | 46 | } else { |
dudmuck | 0:9a142eb22e83 | 47 | service_something(); |
dudmuck | 0:9a142eb22e83 | 48 | } |
dudmuck | 0:9a142eb22e83 | 49 | } // ...for() |
dudmuck | 0:9a142eb22e83 | 50 | } |
dudmuck | 0:9a142eb22e83 | 51 | |
dudmuck | 0:9a142eb22e83 | 52 | void |
dudmuck | 0:9a142eb22e83 | 53 | print_status() |
dudmuck | 0:9a142eb22e83 | 54 | { |
dudmuck | 0:9a142eb22e83 | 55 | printf("LED1:%d\r\n", myled.read()); |
dudmuck | 0:9a142eb22e83 | 56 | printf("my_data:%d\n", my_data); |
dudmuck | 0:9a142eb22e83 | 57 | } |
dudmuck | 0:9a142eb22e83 | 58 | |
dudmuck | 0:9a142eb22e83 | 59 | void console() |
dudmuck | 0:9a142eb22e83 | 60 | { |
dudmuck | 0:9a142eb22e83 | 61 | int len, n; |
dudmuck | 0:9a142eb22e83 | 62 | |
dudmuck | 0:9a142eb22e83 | 63 | len = get_kbd_str(pcbuf, sizeof(pcbuf)); |
dudmuck | 0:9a142eb22e83 | 64 | if (len < 0) { |
dudmuck | 0:9a142eb22e83 | 65 | printf("abort\r\n"); |
dudmuck | 0:9a142eb22e83 | 66 | return; |
dudmuck | 0:9a142eb22e83 | 67 | } |
dudmuck | 0:9a142eb22e83 | 68 | |
dudmuck | 0:9a142eb22e83 | 69 | printf("\r\n"); |
dudmuck | 0:9a142eb22e83 | 70 | if (len == 1) { |
dudmuck | 0:9a142eb22e83 | 71 | /* single character handling */ |
dudmuck | 0:9a142eb22e83 | 72 | switch (pcbuf[0]) { |
dudmuck | 0:9a142eb22e83 | 73 | case '?': |
dudmuck | 0:9a142eb22e83 | 74 | printf(". print status\r\n"); |
dudmuck | 0:9a142eb22e83 | 75 | printf("led toggle LED\r\n"); |
dudmuck | 0:9a142eb22e83 | 76 | printf("da<%%d> get/set my_data\r\n"); |
dudmuck | 0:9a142eb22e83 | 77 | break; |
dudmuck | 0:9a142eb22e83 | 78 | case '.': |
dudmuck | 0:9a142eb22e83 | 79 | print_status(); |
dudmuck | 0:9a142eb22e83 | 80 | break; |
dudmuck | 0:9a142eb22e83 | 81 | } // ...switch (pcbuf[0]) |
dudmuck | 0:9a142eb22e83 | 82 | } else if (pcbuf[0] == 'l' && pcbuf[1] == 'e' && pcbuf[2] == 'd') { |
dudmuck | 0:9a142eb22e83 | 83 | if (myled.read()) // invert LED state |
dudmuck | 0:9a142eb22e83 | 84 | myled = 0; |
dudmuck | 0:9a142eb22e83 | 85 | else |
dudmuck | 0:9a142eb22e83 | 86 | myled = 1; |
dudmuck | 0:9a142eb22e83 | 87 | } else if (pcbuf[0] == 'd' && pcbuf[1] == 'a') { |
dudmuck | 0:9a142eb22e83 | 88 | if (pcbuf[2] >= '0' && pcbuf[2] <= '9') { |
dudmuck | 0:9a142eb22e83 | 89 | sscanf(pcbuf+2, "%d", &n); |
dudmuck | 0:9a142eb22e83 | 90 | my_data = n; // set user data |
dudmuck | 0:9a142eb22e83 | 91 | } |
dudmuck | 0:9a142eb22e83 | 92 | printf("data:%d\r\n", my_data); // show current value; |
dudmuck | 0:9a142eb22e83 | 93 | } |
dudmuck | 0:9a142eb22e83 | 94 | |
dudmuck | 0:9a142eb22e83 | 95 | printf("> "); |
dudmuck | 0:9a142eb22e83 | 96 | fflush(stdout); |
dudmuck | 0:9a142eb22e83 | 97 | } |
dudmuck | 0:9a142eb22e83 | 98 | |
dudmuck | 0:9a142eb22e83 | 99 | int main() |
dudmuck | 0:9a142eb22e83 | 100 | { |
dudmuck | 0:9a142eb22e83 | 101 | pc.baud(57600); |
dudmuck | 0:9a142eb22e83 | 102 | printf("\r\nreset\r\n"); |
dudmuck | 0:9a142eb22e83 | 103 | |
dudmuck | 0:9a142eb22e83 | 104 | printf("> "); |
dudmuck | 0:9a142eb22e83 | 105 | fflush(stdout); |
dudmuck | 0:9a142eb22e83 | 106 | |
dudmuck | 0:9a142eb22e83 | 107 | while (1) { |
dudmuck | 0:9a142eb22e83 | 108 | console(); |
dudmuck | 0:9a142eb22e83 | 109 | } |
dudmuck | 0:9a142eb22e83 | 110 | } |