Go Down

Topic: RS 232 communication with Serial1 on MEGA 2560 (Read 8857 times) previous topic - next topic

jpkami

Hello everybody,
I'm having problem communicating with the MEGA 2560 through Serial 1,2 and 3 :
the connection through USB via Serial 0 works perfectly but when I try to send data with Serial3 (pins 14 and 15) the computer recieve weird datas.
It is as if  the bytes are messed up.

here is the code I'm using :

Code: [Select]
void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial3.begin(9600);
  Serial.println("Hello World");
  Serial3.println("Hello World");
}

void loop() { 
  if (Serial3.available()) {
    int inByte = Serial3.read();
    Serial.print(inByte, BYTE);
  }
 
    if (Serial.available()) {
    int inByte = Serial.read();
    Serial3.print(inByte, BYTE);
  }
 
}


for example, there is what I receive instead of Hello World (in Hexa) :
00
AB 3A 3A 0A FA 8B 85 1B 27 37 E5 EB 00


I think it may be a silly mistake but I don't have clues.

I already tried to check the communication with a cross wired DB9 cable between 2 PC to see if the data was sent correctly and it works.
Also the Com port configuration is : 9600 bps - 8 bits - No Parity - 1 Bit Stop - No Control

Do you have any ideas ?

arbarnhart

#1
Jan 27, 2011, 05:04 pm Last Edit: Jan 27, 2011, 05:09 pm by arbarnhart Reason: 1
The Arduino serial ports are 5v TTL. RS-232 is -12v. You need a level shifter. Sparkfun has one I have used with great success.
http://www.sparkfun.com/products/8780
scroll down below it to related products for one with a connector and/or a kit.

bullethole

Are you trying to drive the DB9 directly from the MEGA pins?   If so, you are trying to use TTL to talk RS232, which can't be done.  I believe you need something like a MAX232 to convert TTL to RS232.

Try this  connect TX2-RX3 and RX2 to TX3 and see what you get on Serial2.  

jpkami

Ok thanks for the quick replies, I wasn't using a level shifter I'll try with that.

BTW I tried connecting Rx 2 to Tx 3 and Rx3 to Tx2 and it was working perfectly.

Thanks again for the advices

Graynomad

Transmitting 5v -> 232 often does work, but often doesn't work also. It depends on the receiving hardware.

Receiving 232 into the Arduino is a bad thing to do, +-12v into the Arduino pin is not recomended. :)

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Go Up