transmit what you receive in both directions
Pass serial data to/from a one uart to another. Useful for accessing the serial interface from a connected module such as WiFi or Cellular.
main.cpp@12:c922ba772e30, 2019-09-13 (annotated)
- Committer:
- maclobdell
- Date:
- Fri Sep 13 15:37:35 2019 +0000
- Revision:
- 12:c922ba772e30
- Parent:
- 8:bb09890333fe
update to RawSerial to allow send/receive form interrupt context; update Mbed OS version to latest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
Jonathan Austin |
0:2757d7abb7d9 | 2 | |
maclobdell | 12:c922ba772e30 | 3 | void recieve_this(void); |
maclobdell | 12:c922ba772e30 | 4 | void recieve_that(void); |
maclobdell | 12:c922ba772e30 | 5 | |
Jonathan Austin |
0:2757d7abb7d9 | 6 | DigitalOut led1(LED1); |
maclobdell | 12:c922ba772e30 | 7 | DigitalOut led2(LED2); |
Jonathan Austin |
0:2757d7abb7d9 | 8 | |
maclobdell | 12:c922ba772e30 | 9 | RawSerial this_way(USBTX,USBRX); |
maclobdell | 12:c922ba772e30 | 10 | RawSerial that_way(D1,D0); |
maclobdell | 12:c922ba772e30 | 11 | |
Jonathan Austin |
1:846c97078558 | 12 | // main() runs in its own thread in the OS |
Jonathan Austin |
1:846c97078558 | 13 | // (note the calls to Thread::wait below for delays) |
Jonathan Austin |
0:2757d7abb7d9 | 14 | int main() { |
maclobdell | 12:c922ba772e30 | 15 | |
maclobdell | 12:c922ba772e30 | 16 | |
maclobdell | 12:c922ba772e30 | 17 | this_way.baud(115200); |
maclobdell | 12:c922ba772e30 | 18 | /** attach the callback function to be called whenever a character is received */ |
maclobdell | 12:c922ba772e30 | 19 | this_way.attach(&recieve_this, Serial::RxIrq); |
maclobdell | 12:c922ba772e30 | 20 | ///set the baud rate |
maclobdell | 12:c922ba772e30 | 21 | that_way.baud(115200); |
maclobdell | 12:c922ba772e30 | 22 | /** attach the callback function to be called whenever a character is received */ |
maclobdell | 12:c922ba772e30 | 23 | that_way.attach(&recieve_that, Serial::RxIrq); |
maclobdell | 12:c922ba772e30 | 24 | |
maclobdell | 12:c922ba772e30 | 25 | |
Jonathan Austin |
0:2757d7abb7d9 | 26 | while (true) { |
maclobdell | 12:c922ba772e30 | 27 | Thread::wait(1000); |
Jonathan Austin |
0:2757d7abb7d9 | 28 | } |
Jonathan Austin |
0:2757d7abb7d9 | 29 | } |
Jonathan Austin |
1:846c97078558 | 30 | |
maclobdell | 12:c922ba772e30 | 31 | |
maclobdell | 12:c922ba772e30 | 32 | void recieve_this(void) |
maclobdell | 12:c922ba772e30 | 33 | { |
maclobdell | 12:c922ba772e30 | 34 | ///blink an led for fun |
maclobdell | 12:c922ba772e30 | 35 | led1 = !led1; |
maclobdell | 12:c922ba772e30 | 36 | /** get a character, then put it back on the other interface */ |
maclobdell | 12:c922ba772e30 | 37 | that_way.putc(this_way.getc()); |
maclobdell | 12:c922ba772e30 | 38 | |
maclobdell | 12:c922ba772e30 | 39 | } |
maclobdell | 12:c922ba772e30 | 40 | /** get incoming data from second serial interface and put to other |
maclobdell | 12:c922ba772e30 | 41 | * @param none |
maclobdell | 12:c922ba772e30 | 42 | * @returns none |
maclobdell | 12:c922ba772e30 | 43 | */ |
maclobdell | 12:c922ba772e30 | 44 | void recieve_that(void) |
maclobdell | 12:c922ba772e30 | 45 | { |
maclobdell | 12:c922ba772e30 | 46 | ///blink an led for fun |
maclobdell | 12:c922ba772e30 | 47 | led2 = !led2; |
maclobdell | 12:c922ba772e30 | 48 | /** get a character, then put it back on the other interface */ |
maclobdell | 12:c922ba772e30 | 49 | this_way.putc(that_way.getc()); |
maclobdell | 12:c922ba772e30 | 50 | } |