Transfer data between Serials in arduino

Dear readers,

I am a newbie in Arduino.

I intend to send a character in Serial 0, read this character in serial 3, and then using the RX, TX communication to send this character from serial 3 to serial 1. Finally, read the character on the serial monitor. I used 2 wires to connect Rx Tx of Serial 1 to Serial 3.

The result in the serial monitor shows that data has been transferred to Serial 1 but there is nothing in Serial 3. Can you help me in this situation?

Thank you so much!
Lu Nguyen.

This is my code:

void setup() {
 // put your setup code here, to run once:
Serial.begin(115200);
Serial1.begin(115200);
Serial3.begin(115200);
Serial.println("Ready");
delay(1000);
}

void loop() {
 // put your main code here, to run repeatedly:
if (Serial.available()>0) {
 Serial.println("Hello!");
 delay(100);
 // Serial.println(Serial.read());
 char c = Serial.read();
 Serial3.print(c);
 delay(100);
 if (Serial1.available()> 0){
   // Serial.println(Serial.available());
   // Serial.println(Serial.read());
   Serial.println(Serial1.available());
   Serial.println(Serial1.read());
   Serial.println(Serial3.available());
   Serial.println(Serial3.read());
   Serial.println("Bye!");
   delay(100);
   }
 }
}

And this is the result when I sent "a":

Ready
Ready
Hello!
1
97
0
-1
Bye!
Hello!
1
10
0
-1
Bye!

If you ever expect this to do anything, you can't have delays which stop everything, then expect to see something that happened during the delay.

Paul

Dear Paul,

Thank you for your reply.

I have commented all the delays but I still have the same result.

Hello!
1
10
0
-1
Bye!
Hello!
1
97
0
-1
Bye!

I wonder why I send the character from serial 3 to serial 1. There is no character in serial 3 while the character has been sent to serial 1. I expect the same result in serial 1 and serial 3.

Can you tell us in plain words, what this program is intended to do?

I intend to send a character in Serial 0, read this character in serial 3, and then using the RX, TX communication to send this character from serial 3 to serial 1. Finally, read the character on the serial monitor. I used 2 wires to connect Rx Tx of Serial 1 to Serial 3.

seems like you want to do this

void loop () {
    // echo from 0 to 3
    if (Serial.available () >0) {
        Serial3.print (Serial.read ();
    }

    // echo from 3 to 1
    if (Serial3.available () >0) {
        Serial1.print (Serial3.read ();
    }

    // echo from 1 to 0
    if (Serial1.available () >0) {
        Serial.print (Serial1.read ();
    }
}


void setup () {
    // put your setup code here, to run once:
    Serial.begin (115200);
    Serial1.begin (115200);
    Serial3.begin (115200);
    Serial.println ("Ready");
}

aarg:
Can you tell us in plain words, what this program is intended to do?

Dear aarg,
Thank you for your reply.

I intend to study the uart communication in arduino.

The procedure is mentioned as follows:

  1. write a character in Serial monitor
  2. send it from serial 0 to serial 3.
  3. the character is then sent from serial 3 to serial 1 by 2 wires, Rx - Tx. (I have attached the picture about hardware connection)

When I send a character and print the value of serial 1 and serial 3 in the serial monitor, only serial 1 has the expected value.

Once again thank you for you time!
Lu Nguyen.

gcjr:
seems like you want to do this

void loop () {

// echo from 0 to 3
   if (Serial.available () >0) {
       Serial3.print (Serial.read ();
   }

// echo from 3 to 1
   if (Serial3.available () >0) {
       Serial1.print (Serial3.read ();
   }

// echo from 1 to 0
   if (Serial1.available () >0) {
       Serial.print (Serial1.read ();
   }
}

void setup () {
   // put your setup code here, to run once:
   Serial.begin (115200);
   Serial1.begin (115200);
   Serial3.begin (115200);
   Serial.println ("Ready");
}

Dear gcjr,
Thank you for your code.
This is exactly what I want to do. Unfortunately, I have tried your code but nothing happens. No character has been printed on the serial monitor when I send a character from serial 0.

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial3.begin(115200);
  Serial.println("Ready");
}
void loop() {
  // echo from 0 to 3
    if (Serial.available () >0) {
        Serial3.print (Serial.read ());
    }
    // echo from 3 to 1
    if (Serial3.available () >0) {
        Serial1.print (Serial3.read ());
}
}

Many thanks!
Lu Nguyen.

You don't print to serial monitor :wink: There is no Serial.println in your loop().

on some boards the tx and rx pins are shared by the usb out. I would guess, you can't use the first set of serial pins and the usb serial at the same time.

taterking:
on some boards the tx and rx pins are shared by the usb out. I would guess, you can't use the first set of serial pins and the usb serial at the same time.

@lu_nguyen is using Serial1 and Serial3 :wink:

sterretje:
You don't print to serial monitor :wink: There is no Serial.println in your loop().

Dear sterretje,
Thank you for your remind.
I have added the Serial.println() functions in the loop(). However, the result does not match my expectation. The code and the output when I send "a" are presented below.

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial3.begin(115200);
  Serial.println("Ready");
}
void loop() {
  // echo from 0 to 3
    if (Serial.available () >0) {
      Serial3.print (Serial.read ());
    }
    // print the data in serial 3
    if (Serial3.available () >0) {
      Serial.println(Serial3.read ()); 
      }
    
    
    // echo from 3 to 1
    if (Serial3.available () >0) {
        Serial1.print (Serial3.read ());
    }
    // print the data in serial 1
    if (Serial1.available () >0) {
      Serial.println(Serial1.read ()); 
      }
}

Output

Ready
⸮Ready
57
55

As you can see, the results are 57 and 55 while I think they should be 97.

Many thanks!
Lu Nguyen.

taterking:
on some boards the tx and rx pins are shared by the usb out. I would guess, you can't use the first set of serial pins and the usb serial at the same time.

Dear taterking,

Thank you for your reply.

As @sterretje has mentioned above, I use rx tx communication between serial 1 and serial 3. I am not sure whether the usb serial has effect on the assignment from serial 0 to serial 3.

Many thanks!

As you can see, the results are 57 and 55 while I think they should be 97.

acsii decimal value for character '9' = 57
ascii decimal value for character '7' = 55

cattledog:
acsii decimal value for character '9' = 57
ascii decimal value for character '7' = 55

Dear cattle dog,
Here, I do not send character '9' or '7' but character 'a'. So I expect that the result should be 97 and 97.
Many thanks!
Lu Nguyen.

lu_nguyen:
Here, I do not send character '9' or '7' but character 'a'. So I expect that the result should be 97 and 97.

No, you send '9' and '7'.

There is a difference between write() and print().

Whandall:
No, you send '9' and '7'.

There is a difference between write() and print().

Dear Whandall,
Thank you so much!
I have followed your suggestion replacing "print()" by "write()" and the result is 97.
However, only serial 1 has the data. I do not understand because I send the data from serial 3 to serial 1. I suppose that serial 3 and serial 1 should have the same value. The code and the output are presented as belows:

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial3.begin(115200);
  Serial.println("Ready");
}


void loop() {
  // echo from 0 to 3
    if (Serial.available () >0) {
      Serial3.write (Serial.read ());
    }


    // print the data in serial 3
    if (Serial3.available () >0) {
      Serial.print("Data in serial 3: ");
      Serial.println(Serial3.read ()); 
      }
    
    
    // echo from 3 to 1
    if (Serial3.available () >0) {
        Serial1.write (Serial3.read ());
    }


    // print the data in serial 1
    if (Serial1.available () >0) {
      Serial.print("Data in serial 1: ");
      Serial.println(Serial1.read ()); 
      }
}

The output when I send "a":

Data in serial 1: 97

Many thanks!
Lu Nguyen.

However, only serial 1 has the data. I do not understand because I send the data from serial 3 to serial 1. I suppose that serial 3 and serial 1 should have the same value.

Even at 115200 baud, I believe that Serial will be "slow" compared to the code execution. Serial is asynchronous, interrupt driven, and will not always execute in the sequential order you are expecting.

 // print the data in serial 3
    if (Serial3.available () >0) {
      Serial.print("Data in serial 3: ");
      Serial.println(Serial3.read ()); 
      }

My thinking is that Serial3.available ==0 at the time this statement is executed. If you place a delay(100) between the write and the read I think it will be read.

But then there is another problem with the second step because if the code above had actually read the data, it would no longer be in the buffer to read.

/ echo from 3 to 1
    if (Serial3.available () >0) {
        Serial1.write (Serial3.read ());
    }

To read Serial3 and then send to Serial and Serial1 I would do that in the same block of code, and place the Serial3.read() in a variable to pass to the two .write() statements.

this worked for me. if I enter 'A' on the serial monitor, I see 'A' on the monitor

i used serial 2 and serial 3, connecting TX3 to RX2 and TX2 to RX3.

void loop () {
    char c;

    if (Serial.available () >0) {
        c = Serial.read ();
        Serial3.write (c);      // to serial 2

     // Serial.println (" rec serial");
    }

    if (Serial2.available () >0) {
        c = Serial2.read ();
        Serial2.write (c);      // back to serial 3

     // Serial.println (" - rec serial-2");
    }

    if (Serial3.available () >0) {
        c = Serial3.read ();
        Serial.print (c);

     // Serial.println (" - rec serial-3");
    }
}

#define SPD   1200

void setup () {
    Serial.begin  (SPD);
    Serial1.begin (SPD);
    Serial2.begin (SPD);
    Serial3.begin (SPD);
    Serial.println ("Ready");
}

Dear gcjr,

Thank you for your reply.

When I follow your process, the code also works.

1st step: I send a character from serial monitor to serial 3.
2nd step: This character is then sent from serial 3 to serial 1 by Rx-Tx connection.
3rd step: Finally, the character is sent back from serial 1 to serial 3.

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial3.begin(115200);
  Serial.println("Ready");
}


void loop() {
  // echo from 0 to 3
    if (Serial.available () >0) {
      Serial3.write (Serial.read ()); // send to serial 1
    }


   // send back to serial 3
    if (Serial1.available () >0) {
      Serial1.write(Serial1.read ()); 
      }
      
    // print the data in serial 3
    if (Serial3.available () >0) {
      Serial.print("Data in serial 3: ");
      Serial.println(Serial3.read ()); 
      }
}

Output

Data in serial 3: 97

However, if I do not send the character back from serial 1 to serial 3, the 3rd step, the serial 3 is empty while serial 1 has received the character.

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial3.begin(115200);
  Serial.println("Ready");
}


void loop() {
  // echo from 0 to 3
    if (Serial.available () >0) {
      Serial3.write (Serial.read ());
    }


   // print the data in serial 1
    if (Serial1.available () >0) {
      Serial.print("Data in serial 1: ");
      Serial.println(Serial1.read ());
     // Serial1.write(Serial1.read ());
      }
      
    // print the data in serial 3
    if (Serial3.available () >0) {
      Serial.print("Data in serial 3: ");
      Serial.println(Serial3.read ()); 
      }
    


    // echo from 3 to 1
    if (Serial3.available () >0) {
        Serial1.write (Serial3.read ());
    }
}

Output

Data in serial 1: 97

Thank you so much!

Best regards,
Lu Nguyen.