Hello,
I am trying to figure out how to communicate between two devices with the SoftwareSerial library. I was first trying to echo a byte from the transmitter to the receiver of the same SoftwareSerial object, but apparently it doesn't seem to be possible.
I made some progress with two Arduino UNO. Let's call them the left-hand and right-hand devices. The left-hand device waits for an input byte from the Serial console. When it gets one, it sends it to the right-hand device which sends it back to the left-hand device. The left-hand device then acknowledges the receiving of the byte, thus confirming the round-trip.
Pin 10 of the left-hand device is connected to pin 11 of the right-hand device.
Pin 11 of the left-hand device is connected to pin 10 of the right-hand device.
Left-hand device sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() // run over and over
{
if (mySerial.available()) // A byte is arriving after a round-trip
{
char c = mySerial.read();
Serial.print("Received ");
Serial.println(c);
}
if (Serial.available())
{
char c = Serial.read();
Serial.print("Sending ");
Serial.println(c);
mySerial.write(c); // Send the byte for a round-trip
}
}
Right-hand device sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop()
{
if (mySerial.available()) // A byte has arrived
{
char c = mySerial.read();
Serial.print("Received ");
Serial.println(c);
mySerial.write(c); // Send it back
}
}
Problem: When I input a string such as "RUN FORWARD" to the Serial console of the left-hand device, this is a typical result I get:
Sending R
Sending U
Received R
Sending N
Received U
Sending W
Received N
Sending A
Received W
Sending R
Received A
Sending D
Received R
Received D
Only "RUNWARD" was sent (and I received "RUNWARD"). Why didn't all the bytes were sent?
If you have other remarks by-the-way about SoftwareSerial (or any method to communicate between microcontrolers) and how to use it, it will be mostly appreciated!