This program turns the mbed device into a serial passthrough. This is useful for imitating a FTDI chip. Any commands sent from the PC to the mbed board will be forwarded on to the serial device attached, and any commands coming from the serial device will be forwarded to the PC. Make sure to change the speeds to match your serial device.
Diff: main.cpp
- Revision:
- 5:96cb82af9996
- Parent:
- 4:ba9100d52e48
--- a/main.cpp Mon Apr 27 21:32:55 2015 +0000 +++ b/main.cpp Thu Apr 30 22:48:21 2015 +0000 @@ -1,37 +1,35 @@ #include "mbed.h" -// Program to use the mbed as a serial bridge -// -RawSerial pc(USBTX, USBRX); // tx, rx -RawSerial dev(D1, D0); // tx, rx +RawSerial pc(USBTX, USBRX); +RawSerial dev(D1, D0); +DigitalOut led1(LED1); +DigitalOut led4(LED4); -void send() +void dev_recv() { - while (pc.readable()) { + led1 = !led1; + while(dev.readable()) { + pc.putc(dev.getc()); + } +} + +void pc_recv() +{ + led4 = !led4; + while(pc.readable()) { dev.putc(pc.getc()); } } -void recv() -{ - pc.putc(dev.getc()); -} - int main() { - char c; - - // PC serial pc.baud(9600); - pc.attach(send, Serial::RxIrq); - - // Device serial dev.baud(9600); - wait(0.1); - dev.printf("AT+RST\r\n"); // Reset esp device + pc.attach(&pc_recv, Serial::RxIrq); + dev.attach(&dev_recv, Serial::RxIrq); + while(1) { - c = dev.getc(); - pc.putc(c); + sleep(); } }