RS232 communication doesn't work

07 Feb 2012

I'm trying to use mbed to control a piece of equipment. As a test, I hooked up p28 and p27 of mbed (Serial Tx/Rx respectively) to the usb port of a computer through an RS232/usb adapter. On the mbed, I had the talk-back code running

#include "mbed.h"
Serial pc(p28,p27); // tx, rx
int main() {
    pc.printf("Hello");
    while(1) {
        pc.putc(pc.getc());
    }
}

I then connected to mbed using the serial cable (not USB), reset the mbed and received the following response instead of "Hello"

o5''!<NUL>

or in binary

10101011 00111010 00111010 00001010 00000010 

Clearly, the code doesn't work. I then tested it with another program

#include "mbed.h"
Serial device(p28, p27);  // tx, rx
int main() {
    while (1) {
        device.putc('8');
        wait(0.2);
    }
}

By changing from '8' to various letters, I see that the signal as read by RS232 port monitoring program doesn't agree with the input of the program. Here're some of the examples (in binary form)

Input...Output

  • 01100001...01001111
  • 01100010...00100111
  • 00111000...00001100

Connecting mbed directly to an actual com port of a linux box (not using the adapter anymore) results in the same wrong outputs.

Any ideas on this issue?

Any help would be greatly appreciated. I've been staring at these confusing outputs for a while now.

07 Feb 2012

Possible problems:

  • I assume that you did not connect mbed directly to an RS232 port. The standard RS232 has voltage levels that mbed can not tolerate. The levels are also of the wrong polarity. You should always use something like a max3232 between RS232 and mbed.
  • Make sure that mbed and the RS232 device share a common ground
  • Make sure you have both devices set to the same baudrate, databits, parity and stopbits. Default for mbed serial ports is 9600 8N1.
13 Feb 2012

The reverse polarity bit of information solved the problem. Thank you.