8 years, 5 months ago.

Using multiple Serial instances does not work!

Hi, I have come across a problem where declaring a second instance of the Serial class seems to override the first declaration. Following is a copy of my test program. If I comment out uart1 definition and usage then pc uart part works and I can see output on terminal connection, otherwise it does not. It seems that declaration of uart1 overrides the pins used for pc!! or am I missing something here? (I also used the echo example program provide on mbed Serial page and got the same problem) I am using the latest mbed libraries on a Nordic nrf51-DK board

Thanks, Farshad

  1. include "mbed.h"

-------- Testing of multiple serial connections. The last definition of Serial instance overwrites the previous definitions! ==> if uart1 is defined, pc.printf() statement does not work. Maybe its TX and RX definitions are overriden by uart1 TX and RX pin definitions if pc is defined after uart1 then pc works and in fact printf statement of uart1 also goes to the pc!! --------

Serial pc(USBTX, USBRX); Serial uart1( p13, NC );

DigitalOut led1(LED1);

int main() { int i = 1; led1 = 0;

pc.baud(115200); pc.printf("Starting....\r\n");

while(1) { pc.printf("PC: %d \r\n", i); uart1.printf("uart1: %d \r\n", i);

led1 = !led1; i++; wait(1); }

the following code is from the example provided on mbed Serial page. It shows the same problem... #include "mbed.h" Serial pc(USBTX, USBRX); tx, rx Serial device(p9, p10); tx, rx int main() { pc.baud(115200); while(1) { if(pc.readable()) { char c = pc.getc(); pc.putc(c); device.putc(c); } if(device.readable()) { pc.putc(device.getc()); } } }

Question relating to:

Which device are you using? Some only have hardware for a single UART.

posted by Erik - 03 Dec 2015

I think Nordic DK uses nRF51822. And you are right, this chip only has one UART. I didn't realize that Serial was using hardware UART. I guess for the secondary serial I will have to find a Soft serial. I have just come across a SoftSerial class which I will have a go at and see how it goes. Thanks for the pointer. Farshad

posted by Farshad N 04 Dec 2015
Be the first to answer this question.