I want to set up 2 or multiple UART connection in an Arduino UNO with other modules. at first I want to know if it is possible? Then if it is possible how can I set up it? (And please if you have a tutorial for this share with me)
As I search the internet I use Softwareserial library and myserial.listen() syntax,but unfotunetly it's not working. Thank you for your helps.
Yes, in case of two UART it is possible. The first is hardware UART on pins 0 & 1, the second is software emulation via SoftwareSerial library. If you need more than 2 UARTs - it would be better to choose an Arduino with more than one on-board Serial - i.e Mega, Nano Every or Leonardo
Apart from UARTs, other communication links are available; eg, I2C & SPI.
These are specifically designed as buses - meaning that just one link can carry communications with many "modules".
But they're not designed for long distances.
It is also possible to get external hardware UARTs which connect to the Arduino via I2C or SPI...
There are many more exotic options, such as the controllers of the w80x family - they each have 6 hardware UARTs.
It have Arduino support and are several times cheaper than Uno.
Success highly depends on what those modules are and how they work. If they only react on a request from your sketch, it should work. If they however just send unsolicited data, it will more than likely not work.
Baud rate is also a factor; although the documentation for SoftwareSerial states 115200 baud max, I've never heard of someone succeeding with it; 19200 should work, you might be able to push it to 38400.
HardwareSerial (as suggested above) is far preferred.
By the way, "it's not working" is not a good description of a problem
Again. Use one software serial instance, at as low a baud rate as possible.
For all others, use hardware serial ports. I strongly suggest that you actually use a device with a hardware port for every connection, as that will avoid the likelihood that you will use a software serial port beyond it's very limited capabilities.
There is one scenario where you might have success with multiple software serial devices. It must fit all of these criteria:
all serial devices on software serial instances MUST only talk when asked to
each transaction with a software-serial device must be executed to conclusion. For example, you cannot askA, askB, askC, then lookforresponsefromA, lookforresponsefromB, lookforresponsefromC. Guarantees failure.
No other interrupt-driven services should be active while software serial devices are being talked to and or awaiting responses from; exception is millis(), but read on.
This is because to do the necessary bit-timing for software serial, you must have absolute control over time. Interrupts, by their very nature, violate this. I suspect even the minimal interrupt time associated with millis() and related functionality, can be enough disruption to violate software serial dependencies, if software serial is used at high baud rates, though I have not gone through the code for either millis(), or the various software-serial implementations, to know this for sure.
@khak, it's been pointed out several times that an Uno is not a good choice for this application. Why do you continue with the attempts? Move to a board with multiple hardware UARTs and make your life much simpler.
Accoring to the following Example from Arduino Reference, two software UART Port may work; practically, they don't work:
#include <SoftwareSerial.h>
// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial portOne(10, 11);
// Set up a new SoftwareSerial object with RX in digital pin 8 and TX in digital pin 9
SoftwareSerial portTwo(8, 9);
void setup() {
// Set the baud rate for the Serial object
Serial.begin(9600);
// Set the baud rate for the SerialSoftware objects
portOne.begin(9600);
portTwo.begin(9600);
}
void loop() {
// Enable SoftwareSerial object to listen
portOne.listen();
if (portOne.isListening()) {
Serial.println("portOne is listening!");
} else {
Serial.println("portOne is not listening!");
}
if (portTwo.isListening()) {
Serial.println("portTwo is listening!");
} else {
Serial.println("portTwo is not listening!");
}
}
This is the sketch I have tried with UNO-1, UNO-2, and NANO. UNO-1 can receive data either from UNO-2 or NANO; but, both receive data from UNO-1.
#include<SoftwareSerial.h>
SoftwareSerial SUART(4, 5); //SRX = 4, STX = 5
SoftwareSerial SUART1(6, 7); //SRX = 6, STX = 7
unsigned int integersToSend[] = {12345, 65432, 333, 4, 5050};
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
SUART1.begin(9600);
}
void loop()
{
for (int i = 0; i < 5; i++)
{
SUART.print(integersToSend[i], DEC); //ASCII codes for decimal digits
SUART1.print(integersToSend[i], DEC); //ASCII codes for decimal digits
SUART.print(',');
SUART1.print(',');
}
SUART.print('\n'); //end of message mark
SUART1.print('\n'); //end of message mark
delay(1000);
SUART.listen();
if (SUART.isListening())
{
char myData[20]; //if (SUART.available() > 0)
{
byte m = SUART.readBytesUntil('\n', myData, sizeof myData - 1);
myData[m] = '\0';
Serial.println(myData);
}
}
//delay(2000);
SUART1.listen();
if (SUART1.isListening())
{
char myData1[20]; //if (SUART.available() > 0)
{
byte m1 = SUART1.readBytesUntil('\n', myData1, sizeof myData1 - 1);
myData1[m1] = '\0';
Serial.println(myData1);
}
}
}
@GolamMostafa If you'd read the rest of my post, you'd have noted the description of a limited-context scenario where multiple software serial instances can be made to work.