I am trying to receive from two ports (one at a time) using Softwareserial on Arduino UNO.
That is, when data on one serial is not available it should go to the other serial and receive its data.
Here is the code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX
SoftwareSerial XSerial(10, 11); // RX, TX
void setup() {
Serial.begin (115200);
mySerial.begin(9600);
XSerial.begin(9600);
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
if (XSerial.available()) {
Serial.write(XSerial.read());
}
if (Serial.available()) {
XSerial.write(Serial.read());
}
}
Because the default is listening from the last initialized serial (XSerial), in a case where no data are available at pin 10, I cannot receive from pin 8 at all.
can't say i understand the limitations for using two software serial ports except to say that the input pin must constantly be monitored to detect the individual bits appearing on it.
have you considered using a single RS-485 interface that allow more than two endpoints. A polled, master/slave approach can mediate use of the common link
You need a better listening protocol. I don't think that you can switch to a listener and immediately check for availability... you probably need to wait at least one character time, probably more. At 9600 bits per second, one character time is about one millisecond. This is a long time in the Arduino universe.
Alicefaisal:
That is, when data on one serial is not available it should go to the other serial and receive its data.
I think you misunderstand how Serial generally and SoftwareSerial in particular works.
A character can only be available after the serial port has been listening for a period of time. And serial data arrives very slowly by the standards of an Ardunio processor. The fact that Serial.available() shows nothing at one instant does not mean that a character is not in the process of being received and will be available 1 or 2 millisecs later.
Using two instances of SoftwareSerial only makes sense if you KNOW that only one device is sending data while you are listening for that device. The best way to achieve that is with devices that only send data when the Arduino asks them to - and most devices don't work like that.
If you need to receive data from two different serial devices then you should really use an Arduino Mega which has 3 spare HardwareSerial ports.
Robin2:
Using two instances of SoftwareSerial only makes sense if you KNOW that only one device is sending data while you are listening for that device.
i wouldn't think that using HardwareSerial. up until now, i would have assume SoftwareSerial is the same as HardwareSerial with limited speed.
gcjr:
i wouldn't think that using HardwareSerial. up until now, i would have assume SoftwareSerial is the same as HardwareSerial with limited speed.
That's true for a single instance of SoftwareSerial.
SoftwareSerial is a bit like an FM radio - it can only play one programme at a time. If you are listening to BBC Radio2 you miss everything that is being broadcast for BBC Radio1