Using more RS485 boards connected to one Arduino board

Hi, please help me, I would like to use two RS485 boards connected to one Arduino Uno board using softwareserial library. I use 2x Arduino Nano as masters to send data and send values through RS485 connection to Arduino Uno for next using. There are two cables, so I use 2x RS485 on slave side (I think, it is easier to create code). Data are sent as string. When I try connection first master to slave or second master to slave separately, it works perfectly for me, but when I try to programm it together to one code, it doesnt work and I dont know why. May be it is a simple mistake, but I am beginner :-). Thanks very much. Code placed here is cropped as the most simple code only for test, but it doesnt work, it shows nothing.
I tested it a little and I found, which interface is first in deffinition void setup INTERFACE.begin, that interface works, second interface doesnt work (that is true only, when dont use INTERFACE.listen()).
When use INTERFACE.listen(), both interfaces doesnt work.

#include <SoftwareSerial.h>

SoftwareSerial portOne(10,11); 
// RX on digital pin 10
// TX on digital pin 11

SoftwareSerial portTwo(12,13); 
// RX on digital pin 12
// TX on digital pin 13

int enablePin = 2;
int enable2np = 3; 

char data[30];
char vysl[20];
int i = 0;
int j = 0;

int pocet = 0;

void setup()
{
  Serial.begin(9600);
  //while(!Serial) {}
  //Serial.setTimeout(50);
  portOne.begin(9600);                 // initialize serial at baudrate 9600:
  portTwo.begin(9600);
  pinMode(enablePin, OUTPUT);
  pinMode(enable2np, OUTPUT);
  //delay(10); 
  digitalWrite(enablePin, LOW);
  digitalWrite(enable2np, LOW);
}

void loop()
{
portOne.listen();

while (portOne.available())
  {
    char y = portOne.read();
    if (y != '\n')//newline character
    {
      vysl[j] = y;
      j++;
    }}

portTwo.listen();
while (portTwo.available())
  {
    char x = portTwo.read();
    if (x != '\n')//newline character
    {
      data[i] = x;
      i++;
    }}

Serial.println(pocet);
Serial.print("Data z prevodniku RS485: ");
Serial.println(vysl);
Serial.print("Data z 2.NP: ");
Serial.println(data);
     
i = 0;
j = 0;
pocet++;
Serial.println("-----------------------");
      
//delay(1000);
  
}

With my brief knowledge of softwareserial, I'll put forward my theory.

I can see your code is switching between listening on portOne and portTwo.

I'm not sure that your approach is working as you think it is.

You switch to portOne and check to see if there are any characters available. Chances are there are not as you've only just started listening on portOne so your code drops out of the while loop.

Your code then switches to portTwo and checks to see if there are any characters available. Again, chances are there are not as you've only just started listening on portTwo so your code drops out of the while loop.

You print out some information and repeat the process.

A software serial port that is not "listening" won't store characters in the background like a regular hardware serial port would.

I think your main issue is that you do not know when your Nanos are going to send data and therefore miss some if not all of the data if you happen to be listening on the other software serial channel.

It would be simpler if you were to setup your 2 Nanos to use the same RS485 bus. If you were to then get your UNO to query each Nano in turn, then you can be sure of when the response is coming.

1 Like

Agreed; make the Uno the master.

This is the essence. In effect, you can not listen on more than 1 swSerial port at a time. You can however listen on 1 hwSerial & 1 swSerial port at a time. Still swSerial may not always be fully reliable.
I suggest you use a board with more than 2 hwSerial ports

Use 1 softserial and use RS485 like this.

image

Ref: Reading rs-485 rtu with arduino

1 Like

That is the most appropriate method. master sends data request to the slaves and waits for answer, Common GND is not required though.

Hi all and really thanks for your help.

  1. Depend on your thoughts I tested a little and found, that when I use for both interface FOR (10000x) {portOne.listen(); while (portOne.available()) ...}, it started to work, both interface :-). I know, this is bad sollution and brute force sollution, but it is interensting, generally every 30. loop shows fresh values, its about 60 second.

  2. I tried different method, as you said,
    How to Communicate 3 or 32 Arduinos via RS-485 - Hackster.io
    but it again doesnt work for me. This example is built as the communicaiton is the main function, but not in my sollution.

My plan is:

second floor -> Nano + LCD display, measure temperature and humidity (DHT22), show values and send values to Uno with RS485.

first floor -> Nano (same function)

Cellar -> Uno + LCD display, receive values from RS485, show values from house, set heating, send values to RPi via USB (web and mySQL, will be in future)

So sending values to cellar is one of the function, not main function, Nano cant wait whole the time to receive signal from Uno to send values, it must meassure on analog port and print values in LCD display. I would like to Nano measures and shows values even, when communication doesnt work. Thats why (I think) that example doesnt work for me, I need some synchronization or I dont know :-).

Do you have any idea or experiences like different hardware for communication, some tricks or hacks ??? :slight_smile: Thanks a lot.

Your Nanos can happily measure and display while waiting for a command from the Uno that the Uno wants some data. I suggest that you read / study Robin's updated serial input basics tutorial to get the Nano side working.

After that you can write the matching Uno code.

You might want to have a read of Nick Gammons webpage on RS485. He discusses how to connect devices together, a simple packet transmission system with example code and even gives you a library to use.

1 Like

Why you think it can't work? Room temperature and humidity don't change in seconds. The Cellar is the master of the game. Think in the same way you use multiple DS18B20 sensors. The master gives both nodes an commando to start measure temperature and humidity. After x seconds (example twice the time needed to measure) the master asks second floor the results. Second floor answer. Master process the values and after that he ask the same things for first floor. First floor answer.

Polling every minute or half minute is more then enough.

I'm polling 9 nodes, temperature (DS18B20), water level and RTC temperature, in a green house every 10 minutes. @38400Bd it takes 2 seconds.

1 Like

RS485 is a bus, it has more than two seats. Why switch back and forth between 2 boards and incur the overhead of software serial?

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