Problem reading input data using SoftwareSerial library

I can't read data using SoftwareSerial library, because SoftwareSerial.available() is always 0, no matter what I do.

I'm trying to communicate 2 Arduinos Uno, the 1 Serial (name of the first Arduino Uno) and 2 Serial (name of the second Arduino Uno). The 2 Serial will send data to 1 Serial, and 1 Serial will receive this data through a Software Serial port, and then re-write the data received to Hardware Serial Port, but I can't read this, and when I try to print what is available is software serial port, always shows 0. I know that I can do the otherwise, but I'm testing this, because in my future project I will need to use this software serial port to read input data, so this is just a test.

The pic of my simulation with the circuit is named Forum.PNG

My code is:
For 1 Serial (the data receiver):

#include <SoftwareSerial.h>

SoftwareSerial portOne (7,8);

String str1;

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);
  portOne.begin(9600);

}

void loop()
{
  while (portOne.available() == 0)
  {
    
  }
  str1 = portOne.readString();
  Serial.println(str1);
}

For 2 Serial (the data sender):

String str;

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:

}

void loop() {
  Serial.println("Testing from serial port2");
  delay(2000);
}

I have already read in another topics of this forums and in the internet, but any of them worked for me.

Thanks in advance.

1 There is nothing wrong with your codes except that they work intermittently with "hardware TX-line" of Sender connected with "software SRX line" of Receiver.

2. The following connection (Fig-1) works well as we observe in Fig-2; where, both Arduinos are connected using software UART Port (SUART).

3. Please, note that you should not use hardware UART Port (UART) to connect other devices as UART Port remains engaged with PC/IDE/SM.

4. You should also avoid the use of String (with Capital S) "data type/Class (with Capital C)" as this usage causes "memory leak/fragmentation" in AVR Architecture.

5.
uartUNO-UNO.png
Figure-1: Connection diagram of two Arduinos using SUART Ports

6.


Figure-2: Receiver's serial Monitor

7. Sender Sketch (your's one except with three more lines for SUART Port)

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

String str;

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  portTwo.begin(9600);
}

void loop() 
{
  Serial.println("Testing from serial port2");
  portTwo.println("Testing from serial port2");
  delay(2000);
}

8. Received Sketch (your's one).

#include <SoftwareSerial.h>

SoftwareSerial portOne (7, 8);

String str1;

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);
  portOne.begin(9600);

}

void loop()
{
  while (portOne.available() == 0)
  {

  }
  str1 = portOne.readString();
  Serial.println(str1);
}

uartUNO-UNO.png

Image from Original Post so we don't have to download it. See this Simple Image Posting Guide

...R

@batata123, your image is unreadable. A photo of a simple pencil drawing that shows the connections will be much better.

Are you using two real Unos or are you just doing a simulation?

I have found that it is actually easier to communicate between two Arduinos using I2C. Have a look at this Arduino to Arduino I2C Tutorial

If you still want to use Serial have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

@GolamMostafa

  1. Is that a problem they communicate intermittently?

  2. So, just to be clear, I can't communicate UART with SUART, only SUART with SUART, is that correct?

  3. Yeah, I know that, I just connected Virtual Terminal at Proteus to simulate, so I could see what was being written at the Serials Ports.

  4. It would be better to use char arrays instead of Strings?

  5. I've tried your code, but it didn't work as well. The portTwo sends the message with no problem, but portOne didn't receive them. The portOne.available() is always zero. I don't know if this happens because I'm simulating them on Proteus and if I tried this in real it wouldn't, but I don't have the hardware with me in the next two weeks, so I'm trying to finish to code before they arrive. See what happened when I simulated with your code.

Anyway, thank you for your time and your support.

@Robin2 I'm sorry, I didn't know that. Thank you for let me know. I have updated my post.

  1. I'm sorry if the image wasn't clear enough, but I put a new image at the reply to GolamMostafa, I hope that is better to see.

  2. As I have replied to GolamMostafa, I'm only simulating, I don't have the real hardware with me right now.

  3. I will have a look at your post about communication Arduino to Arduino I2C. Thank you.

  4. Thank you for this tip, but my problem right now is that I can't receive any data using Software Serial Ports, I don't know if it happens because I'm simulating, and if I had this in real it would be ok, but in Proteus (the software I'm using to simulate) it doesn't work.

Anyway thank you for your time and your effort.

Remember --
1. If something does not work in theory, that will never work in practice.

2. If something works in theory, that may not work in practice.

I have used the real hardware to test UART communication link; whereas, you have used simulators. The discussion here is totally out-of-phase. Please, get hardware gadgets and practice the working codes.