10 years, 10 months ago.

Serial pins echo program

I was able to successfully echo using the USB serial communications (USBTX, USBRX). I'm now trying to do the same using the serial pins. You'd think it would be the same, but I'm getting gibberish echoed back. I've tried both 9600 baud and 115200 baud.

I've made sure to change the baud rate in device manager, my code, and in putty, but still no luck. Here is my code:

#include "mbed.h"

DigitalOut myled(LED1);
Serial uart(p9, p10);

char c;

int main() {
    uart.baud(9600);
    while(1) {
        if(uart.readable()) {
            c = uart.getc();
            uart.putc(c);
        }
    }
}

I should add that I am using an RS232 to USB adapter for the communications. I've got drivers installed and was able to communicate with other devices no problem with it, so I don't think that is the problem.

3 Answers

Tony Otis
poster
10 years, 10 months ago.

Thanks for the help, I figured it out. RS232 used inverted logic. I used a MAX3232 to convert from RS232 to TTL. It also flips the logic.

Accepted Answer
10 years, 10 months ago.

You could also send it to the usbtx, usbrx serial to see what you are getting at the input.

Your adapter is 3.3V compatible? (they generally will be). Didn't mix up TX and RX?

Yeah I also tried sending to usbtx/rx. So when I send a character through p9,p10, my code tells it to echo back through p9,p10, as well as usbtx/rx. Here's the updated code:

#include "mbed.h"

DigitalOut myled(LED1);
Serial uart(p9, p10);
Serial pc(USBTX, USBRX);

char c;

int main() {
    uart.baud(9600);
    pc.baud(9600);
    while(1) {
        if(uart.readable()) {
            c = uart.getc();
            uart.putc(c);
            pc.putc(c);
        }
        if(pc.readable()) {
            c = pc.getc();
            uart.putc(c);
            pc.putc(c);
        }
    }
}

So when I send a character through the serial pins, for example char 'a', I get an 'X' back through the serial pins and an 'O' through USBTX/RX.

On the other hand, when I send an 'a' through the USBTX/RX, I get the 'a' correctly echoed back through USBTX/RX but an 'O' through p9/p10.

Does that make any sense to you? I've been staring at this for a couple hours and I am stumped. Quadruple checked the TX/RX pins.

posted by Tony Otis 19 Jun 2013

No that doesn't make sense to me :P

Also start/stop/parity bits are correct? It really looks like an error in either the timing or the formatting.

So Serial -> USB results in a->O. And USB -> Serial also results in a->O. The same result despite that it is the opposite operation. If you send an X and O through USB, what does it then return on serial?

And do you happen to have a second USB<>RS232 converter (or anything that can act like one, such as a second mbed).

posted by Erik - 19 Jun 2013
10 years, 10 months ago.

You say you change the baud rate in the device manager and in putty. Usually changing baudrate in the terminal application should be sufficient.

Did you also connect GND as well as RXD, TXD between the mbed and the USB-RS232 adapter.

Is the USB-RS232 adapter echoing when you short RXD and TXD (after disconnecting from mbed).

Thanks for the responses. Yep GND is connected. The adapter is echoing when I short RX and TX.

posted by Tony Otis 19 Jun 2013