0

I have 2 Arduino Nano, they are connected via ports 2 and A2

While I use only one port to receive, it works properly. But if I activate the second port, system crash (and even first port don't work properly)

Code of first Arduino:

#include <SoftwareSerial.h>
SoftwareSerial SerialPort2(2,A2);
SoftwareSerial SerialPort3(3,A3);

void setup() {
  SerialPort2.begin(9600);
  SerialPort3.begin(9600);
  Serial.begin(9600);
}

void loop() {
  delay(500);
  SerialPort2.listen();
  if(SerialPort2.available()){
    char value = SerialPort2.read();
    Serial.print(value);
    Serial.print(" First port\n");
  }
  // IF YOU REMOVE PART BELLOW, THE 'SerialPort2' WILL WORK PROPERLY
  SerialPort3.listen();
  if(SerialPort3.available()){
    char value = SerialPort3.read();
    Serial.print(value);
    Serial.print(" Second port\n");
  }
  // END OF THIS PART
}

Code of second Arduino:

#include <SoftwareSerial.h>
SoftwareSerial SerialPort2(2,A2);

void setup() {
  SerialPort2.begin(9600);
}

void loop() {
  SerialPort2.write('a');
  delay(1000);
}

PS in real system I need to listen 6 ports

  • you think the other side will wait until you listen? you switch listen() so fast so no port will listen – Juraj Feb 23 at 19:32
  • @Juraj yes of course – Abionics Feb 23 at 19:33
  • so no it will not wait. it has no clue to know if the port listens – Juraj Feb 23 at 19:34
  • switching listen() clears the receive buffer. after listen() wait for data – Juraj Feb 23 at 19:38
  • @Jurai each serial has own buffer – Abionics Feb 23 at 19:40
0

SoftwareSerial is only a "good" choice (for certain values of "good") if you only use one instance, and that is the only serial port in the system.

Using SoftwareSerial to read from 2 ports simultaneously is not possible. Using SoftwareSerial to read data in tandem with any other hardware serial ports will result it data loss.

SoftwareSerial is only any real use when you are using it to request data from another device and times when you command it to happen.

To receive data in an "unsolicited" fashion SoftwareSerial is not good.

Instead you need a hardware serial port for each incoming signal. And obviously you can't do that with a small board like a nano. So you need to think more creatively.

If you have one real serial port you can talk to one device. But there are other communication channels at your disposal. So you take one board and have that communicate to one of your serial devices. Then you take the data read and convert it into another communication protocol that is capable of sharing time with other boards. Then that, and 5 other boards, all send their data (or get data from) one central "master" board. In effect you have 6 "UART to Protocol X" converter boards, and one "Protocol X" master board.

Alternatively choose a board that has 6 UARTs. There are many higher-end chips like some ATSAMD, SAMX, and PIC32 chips, that have 6 UARTs available to you.

1

SoftwareSerial can only listen to one port at a time. You cannot listen to both, no way. To really listen to both, you need an Arduino with more hardware Serial ports, where most of the protocol is done in hardware, thus can receive without CPU intervention. The Arduino Mega has 4 hardware Serial ports.

Or you could impose a protocol on the Serial stream to make the listening Arduino the master. You would listen to the first serial interface, send a start byte over this interface, that tells the slave Arduino to start sending. It sends and the listening interface receives (The transmission has to be delimited by either a time limit, e.g. listen for a specific time, or by a stop byte). Then you listen to the next interface, send again the start byte and receive. And so on.

With such a protocol, you could even use a single serial interface. The protocol makes sure, that only one slave is transmitting at a time. Though you should add resistors to every slave to protect their TX pins.

in real system I need to listen 6 ports

Serial (UART) is really not the best interface type for such topology. You should consider using interfaces like SPI. It doesn't need additional electronics and is better fit for multiple slaves.

  • In my system I have 6 masters and 1 slave. What interface do you recommend to use? – Abionics Feb 23 at 20:31
  • 1
    I have written about using 1 master, because I suggested to overthink your topology. Multi master is difficult with every interface, that you have on the Nano. You should think about, if you really need multi master functionality. You didn't describe your application, so I cannot know about that. Or, if you need it, then you may have to extend to a more complex interface, that is not supported by the Nano itself. – chrisl Feb 23 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.