4 years, 5 months ago.

Why does this not work? - Serial Question..

From the pc i send serial data to sINPUT and expect it to send that data out of sOUTPUT

Serial cant send getc

#include "mbed.h"

Serial sOUTPUT(p9, p10);  // tx, rx
Serial sINPUT(USBTX, USBRX); // tx, rx

int main() {
    sOutput.baud(921600);
    sINPUT.baud(921600);
    while (1)
    {
        sOutput.printf(sINPUT.getc());
    }
}

3 Answers

4 years, 5 months ago.

If you are using a windows pc, lower the baudrate to 460800.

The baud is fine, It's this line that's the problem

sOutput.printf(sINPUT.getc());

posted by Matthew Savage 23 Jun 2015
4 years, 5 months ago.

The first parameter to printf should be a string. You are giving it an int.

You could try sOutput.printf("%c", sINPUT.getc()), but it might be more efficient to use putc and getc instead. There is an example of doing this in the Handbook Serial page.

4 years, 5 months ago.

Use putc rather than printf. printf expects a char* and you're passing it a char.

The alternative would be sOutput.printf("%c",sINPUT.getc());

but that would be about 20 times slower than putc for no benefit