Go Down

Topic: Serial working for me, not Serial1, 2, or 3 (Read 934 times) previous topic - next topic

I'm trying to send serial data from a Mega 2560 to an Uno. When I send data from the Mega to the Uno via the Mega's RX0 and TX0, the message gets across, but when I use RX/TX1, RX/TX2, RX/TX3, or SoftwareSerial, it fails.

Receiver (Uno): Waits for an incoming byte, displays it on an array of 8 LEDs.
Code: [Select]
#include "mj_bitwise.h" // byteToLEDs(int byte, int startingPin)

void setup () {
  Serial.begin(9600);
}

void loop () {
  if (Serial.available() > 0){
    char data = Serial.read();
    byteToLEDs((int) data, 2); // function from mj_bitwise
  }
}


Sender (Mega): I include calls to three different serials, even mixing up the order, and somehow, only Serial goes through.
Code: [Select]
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
}

void loop() {
  Serial1.write(7); // TX1
  Serial.write(8);  // TX0
  Serial2.write(9); // TX2
}


My pins: When trying an alternative serial connection, e.g. the Mega's RX1 and TX1, I connect Uno:RX to Mega:TX1 and Mega:RX1 to Uno:TX. With the foregoing code running, I can switch the pins from one serial to the next, resetting the boards or not, and no matter what, only the TX0-transmission succeeds.

TMI: My desire is to send data to the Mega from a computer on RX0, then send that data to the Uno through TX1, for debugging purposes.

Oddly, the byte displayed on the Uno's LEDs is correct only about half the time. The other half the time, it's consistently the same, wrong number. E.g. sending 144 gives me either 144 (10010000) or 6 (00000110); sending 129 gives me either 129 (10000001) or 96 (01100000). Any idea why this happens?

James C4S

What if you try to send data from one of the Mega's serial ports to another?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

That much, at least, seems to work. With the following code, an 'A' repeatedly prints to the Serial monitor:

Code: [Select]
/* Serial1 is connected to Serial2 */

void setup() {
  Serial.begin(9600) // diag
  Serial1.begin(9600) // send
  Serial2.begin(9600) // listen
}

void loop() {
  Serial1.write(65);
  if (Serial2.available() > 0) {
    char c = Serial2.read();
    Serial.println(c);
  }
}

Nick Gammon

I can't reproduce your problem. On my Mega:

Code: [Select]
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
}

void loop() {
  Serial1.write(7); // TX1
  Serial.write(8);  // TX0
  Serial2.write(9); // TX2
  delay (100);
}


On my Uno:

Code: [Select]
void setup () {
  Serial.begin(9600);
}

void loop () {
  if (Serial.available() > 0){
    char data = Serial.read();
    Serial.println (data, HEX);
  }
}


Moving the wire from port to port I see 7, 8 or 9 appear as appropriate.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Wish I could say the same. I copied your code verbatim and had output only on the TX0 pin.

Other complications:

  • Sometimes the output to the Uno's Serial monitor was 6, occasionally 7, often FFFFFF, FFFFFE, or another number following the pattern FFFFXX.

  • Connecting TX0 to RX isn't enough for any output; I must also have RX0 connected to TX. Unless both connections are made, I get no output at all.



Can you offer me any instruction to explain these unexpected results.


Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
the message gets across, but when I use RX/TX1, RX/TX2, RX/TX3, or SoftwareSerial, it fails.

Have you looked at the source code for SoftwareSerial? It is based on NewSoftSerial, which was never designed to use on the Mega. And, why should it have been? You have 4 hardware serial ports. If you really need to attach more than 3 serial devices to an Arduino, even the Mega is probably not the right choice.

Thanks for the help, Nick.

Connecting the two grounds together made the difference. (I can't figure out why, though. Why should the ground on the listening arduino be linked to the ground on the transmitting arduino?)

James C4S

Voltages are relative. If two systems don't have a common point, there is nothing relative.  We call this common: "Ground."
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up