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);
}
}
}
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.
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");
}
// 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 ());
}
}
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.
sterretje:
You don't print to serial monitor 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.
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.
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 ());
}
}
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.
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 ());
}
}