Using mutiple Serial ports on Arduino Mega simultaneously

Hello friends, I am having trouble connecting two devices via serial port together in Mega.

This is the program for SIM900 GSM shield connected at Port 1

Serial1.begin(9600);
delay(5000);
Serial1.println("AT");
delay(1000);
Serial1.println("AT+CMGF=1");
delay(1000);
Serial1.println("AT+CMGS=\"1234567890\""); //CHANGE TO DESTINATION NUMBER
delay(1000);
Serial1.print("1234");
Serial1.write(26);

This is the program for Program for SM630 fingerprint scanner connected at Port 2

void searchFinger(){
// write the 8bytes array of the command code
Serial2.write(searchFingerPrint, sizeof(searchFingerPrint));
Serial.println(" Searching... ");
}

If i individually connected each of these devices to Serial 0 of Mega, everything works perfect! But when i connect to Serial 1 and Serial 2, everything fails! Even if i connect these two devices on same serial 0, that too fails! How to use Serial 1,2,3 apart from Serial 0 ? Please help me!!!

You have only posted snippets. Post the full code for the two devices and then we may be able to help.

It is quite likely that all those delay()s get in the way. Look at how to use millis() to manage timing as illustrated in several things at a time.

...R

The other serial ports should work just like Serial. You, of course, have to move the RX and TX wires to the pins for the port you are using.

You can't connect both devices to the same serial port.

I recommend you go back to the beginning. Start with a sketch that works for one device on Serial. Move the wires to Serial1 and change all the occurrences of Serial to Serial1 in the sketch. It should work just as well as before. If it does not then something is wrong with your hardware. Repeat with the other device but use Serial -> Serial2. Once you have those two sketches working you can start to put them together into one sketch.

johnwasser:
The other serial ports should work just like Serial. You, of course, have to move the RX and TX wires to the pins for the port you are using.

You can't connect both devices to the same serial port.

I recommend you go back to the beginning. Start with a sketch that works for one device on Serial. Move the wires to Serial1 and change all the occurrences of Serial to Serial1 in the sketch. It should work just as well as before. If it does not then something is wrong with your hardware. Repeat with the other device but use Serial -> Serial2. Once you have those two sketches working you can start to put them together into one sketch.

So how am I supposed to check whether serial 1,2,3 are working??? I already tried many things!

Robin2:
You have only posted snippets. Post the full code for the two devices and then we may be able to help.

It is quite likely that all those delay()s get in the way. Look at how to use millis() to manage timing as illustrated in several things at a time.

...R

those are the codes itself where I use one after the other! At first, I individually uploaded those and they worked fine with serial. But if I change as serial1/2/3 nothing happens! Only the default serial works!

irfanece:
So how am I supposed to check whether serial 1,2,3 are working??? I already tried many things!

Like I said: "go back to the beginning. Start with a sketch that works for one device on Serial. Move the wires to Serial1 and change all the occurrences of Serial to Serial1 in the sketch. It should work just as well as before. If it does not then something is wrong with your hardware." Do you want me to spell it out for you>?

  • Start with a sketch that works for one device on Serial.
  • Move the wires From Serial (Pins 0 and 1) to Serial1 (Pins 19 and 18).
  • Change all the occurrences of Serial to Serial1 in the sketch.
  • It should work just as well as before. If it does not then something is wrong with your hardware.
  • Repeat with the other device moving from Serial to Serial2 (Pins 17 and 16)

johnwasser:
Like I said: "go back to the beginning. Start with a sketch that works for one device on Serial. Move the wires to Serial1 and change all the occurrences of Serial to Serial1 in the sketch. It should work just as well as before. If it does not then something is wrong with your hardware." Do you want me to spell it out for you>?

  • Start with a sketch that works for one device on Serial.
  • Move the wires From Serial (Pins 0 and 1) to Serial1 (Pins 19 and 18).
  • Change all the occurrences of Serial to Serial1 in the sketch.
  • It should work just as well as before. If it does not then something is wrong with your hardware.
  • Repeat with the other device moving from Serial to Serial2 (Pins 17 and 16)

pardon me, that's what I've been trying to do! The below code is the one I am trying!!! But no change...

Serial1.begin(9600);
delay(5000);
Serial1.println("AT");
delay(1000);
Serial1.println("AT+CMGF=1");
delay(1000);
Serial1.println("AT+CMGS="1234567890""); //CHANGE TO DESTINATION NUMBER
delay(1000);
Serial1.print("1234");
Serial1.write(26);

In above program, if I replace serial with serial1 nothing happens!!!

This is probably a silly question but when you test the various Serial ports you are connecting to the appropriate Tx/Rx pins I hope ?

irfanece:
The below code is the one I am trying!!!

For the SECOND time of asking, post a complete program that compiles properly.

How else can anyone try it to see what you are experiencing?

...R

This is the code!!! :frowning: :blush: I changed Serial to Serial1 but nothing happens!

void setup()
{
Serial1.begin(9600);
delay(5000);
Serial1.println("AT");
delay(1000);
Serial1.println("AT+CMGF=1");
delay(1000);
Serial1.println("AT+CMGS=\"1234567890\""); //CHANGE TO DESTINATION NUMBER
delay(1000);
Serial1.print("hi");
Serial1.write(26);
delay(1000);
}
void loop()
{
}

UKHeliBob:
This is probably a silly question but when you test the various Serial ports you are connecting to the appropriate Tx/Rx pins I hope ?

Off course yes! I am following exactly as John said!

irfanece:
Hello friends, I am having trouble connecting two devices via serial port together in Mega.

If i individually connected each of these devices to Serial 0 of Mega, everything works perfect! But when i connect to Serial 1 and Serial 2, everything fails! Even if i connect these two devices on same serial 0, that too fails! How to use Serial 1,2,3 apart from Serial 0 ? Please help me!!!

I assume you have opened each serial port you need to use by typing:

Serial.begin (9600);
Serial1.begin (115200);
Serial2.begin (57600);

Something like that (of course, the baud rates YOU need go into each one).

Tried that too ::slight_smile:

byte searchFingerPrint[] = {0x4D, 0x58, 0x10, 0x05, 0x44, 0x00, 0x00, 0x00, 0x05, 0x03}; // 10 bytes
byte operation[7],operationFound[9],b,g;

void setup()
{
Serial.begin(9600); //GSM shield
Serial1.begin(57600);  //Fingerprint scanner
delay(5000);
Serial.println("AT");
delay(1000);
Serial.println("AT+CMGF=1");
delay(1000);
Serial.println("AT+CMGS=\"1234567890\""); //CHANGE TO DESTINATION NUMBER
delay(1000);
Serial.print("hi");
Serial.write(26);
delay(1000);
Serial1.write(searchFingerPrint, sizeof(searchFingerPrint));
  for(byte x=0; x<7; x++)
  {
    while(Serial1.available() == 0) {}; // Wait for a byte
    operation[x] = Serial1.read();
  }
    b = memcmp(operationSuccessSearch, operation, sizeof(operationSuccessSearch));
    else if (b==0) {
      for(byte y=0; y<9; y++)
      {
        while(Serial1.available() == 0) {}; // Wait for a byte
        operationFound[y] = Serial1.read();
      }
        
        g = memcmp(FingerPrint0, operationFound, sizeof(FingerPrint0));
   if (g==0) {
      Serial.println(" Match found ");
         }
}
}
void loop()
{
}

irfanece:
This is the code!!! :frowning: :blush: I changed Serial to Serial1 but nothing happens!

Have you a GND connection as well as Rx and Tx?

I think it would be a big help if you make two pencil drawings. The first should show the exact wiring connections for use with the code that does work. The second should show the connections that you use with the code in Reply #9. Post photos of the two drawings. Note that drawings are more informative than images of the hardware.

...R