Serial (HW ports) read and write are not correct

Hi there,

We set up Mega 2560 Serial1 (HW ports) to communicate with an extern serial device. The device sends string ASCII string "0,10,10,2,0$". Mega reads "76 EB 9D A7 9D 9D A7 9B A7 9F B7 0".

We tried to debug this by connecting Serial1 to my computer using a serial cable. From computer, use a terminal program to send/receive data. Mega reads the data and send it back to terminal program.

Strangely, when terminal sends ‘a’ (0x61, 0B01100001) to Serial 1, Terminal reads back (0x86, 0B10000110).

Is it known that MSB/LSB bits for the data are swapped in sending/receiving?

Thanks,
Liang

Are you connecting to an RS232 line? If so, what type of RS232 converter are you using?

The desktop computer has a serial port. We used standard 9 pin RS-232 cable, connect Tx/Rx pins to Serial 1 Tx /Rx on Mega 2560. We did not use an RS232 converter.

Serial port output is a voltage between 0 and 5 volts. RS-232 is a bi-polar voltage between something greater than +3 volts and something greater than -3 volts. The two are not compatible.

The signal on RS232 is inverted, with a negative voltage representing a 1, and a positive voltage a 0. You need an interface between an RS232 signal and the 5 volt logic of the arduino.

For connecting an Arduino to an old style desktop computer RS232 serial port, this adapter works very well: Pololu 23201a Serial Adapter Fully Assembled

The direct connection that you tried can destroy an Arduino, due to the mismatched voltage levels.

You are right. Found the wiki RS-232 - Wikipedia. Thanks.

Set aside voltage differences, are there any libraries to take are of the bit shift?

What bit shift?

Use Serial.read() to get characters from the Arduino serial port, and Serial.write() or .print() to send them.

I meant to ask was, since the other side is RS-232 and RS-232 has inverted logic, is there a function to convert return from Serial.read() to ASCII.

The adapter I linked takes care of the inverted logic and voltage levels.

Typically, human readable ASCII data are sent over the serial link.

Binary data require special handling, where sender and receiver have to agree on some well defined data transmission protocol.