Communication between two arduino UNO via Tx and Rx

Hello All,
I am trying to send a string or a letter from one arduino to another but the program does not work at all.
Two arduino are set baurate: 9600. One on COM4 and another on COM6, but when I send, I don't get anything from both in serial monitor.
One Tx connects Two Rx and One Rx connects to Two Tx.
Any help will be greatly appreciated cause I am new with this.
Here is my code for the sender:

String msg = "";
void setup() {
  Serial.begin(9600);

}

void readSerialPort() {
msg = "";
if (Serial.available()) {
  delay(30);
  while (Serial.available() > 0) {
    msg += (char)Serial.read();
  }
  Serial.flush();
}
}
void loop() {
  readSerialPort();
  if (msg != ""){
    char Mymessage = msg.charAt(0);
    Serial.write(Mymessage); //Write the serial data
  }
  

  delay(1000);

}

And here is the code for receiver:

char Mymessage[2]; //Initialized variable to store recieved data
  
  void setup() {
    // Begin the Serial at 9600 Baud
    Serial.begin(9600);
  }
  
  void loop() {
    Serial.readBytes(Mymessage,2); //Read the serial data and store in var
    Serial.println(Mymessage); //Print data on Serial Monitor
    delay(1000);
  }

Also show us your wiring between the two Arduinos.

This tells the cpu to wait until Serial data has finished sending, it won't clear out the incoming cache.

Welcome to the forum.
I see this is your first forum post and it seems to me that you are also new to "arduino".

The Arduino RX and TX are the communication "hardware" between the Arduino and the PC.

Some arduinos have more than one TX and RX, such as the arduino mega, but the arduino UNO only has one.

If you use these same RX and TX for communication between the arduinos UNO, there will be a "conflict" of transmitting and receiving data between the arduinos and the PC.

Using arduino UNO, for projects like yours, it is recommended to use the Softserial.h library.


My goal is send a command from pc to sender (example: "A0") then receiver should print out "A" in its serial monitor.

Thanks

Please do not post pictures of code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

You will not be able to send from the PC to an arduino when the Rx line of that arduino is connected to the Tx line of another arduino. The Tx from the USB-to-serial chip is fed to the Rx of the atmega328 through a resistor, the Tx from the other arduino will overpower the signal from the USB-to-serial chip and it will never reach the atmega328.

Thanks for the answer.
Just figure out 10 minutes ago. Take the Tx from sender out and it worked.

Thanks again

You need an Example/Tutorial. Follow these steps, see the output, and then write your own sketch to reproduce the results.

1. Connect UNO-1 and UNO-2 as per diagram of Fig-1 using Software UART (SUART) Port. The Hardware UART Ports of both UNOs are engaged with PC/SM for sketch uploading and debugging.
uartUNO-UNO
Figure-1:

2. Upload the following sketch (not tested) in UNO-1 (Arduino-1).

#include<SoftwareSerial.h>
SoftwareSerial SUART(7, 8); //SRX = 7, STX = 8

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

void loop() 
{
  SUART.println("Hello UNO-2!");
  delay(1000);
}

3. Upload the following sketch (not tested) in UNO-2 (Arduino-2)

#include<SoftwareSerial.h>
SoftwareSerial SUART(7, 8); //SRX = 7, STX = 8

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

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    char ch = SUART.read();
    Serial.print(ch);
  }
}

4. Press RESET Buttons of both UNOs.
5. Check that Serial Monitor of UNO-2 shows the following message at 1-sec interval.

Hello UNO-2!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.