Reputation: 41
Following is the code to transmit data from Srial monitor to other device and receive from other and print in the serial monitor the below code is working fine if i Don't transmit the data in between (i.e without this line)
esSe.write("test");
but when i write this line and upload.The device receive the data from the Serial monitor and the String("test") also. But the data transmitted by that device( or the data received by arduino becomes garbage)
I even tried to flush the transmitting buffer of the device through[esSe.flush()] but no change in the result
this the code i have used
#include <SoftwareSerial.h>
SoftwareSerial esSe(2, 3);
void setup() {
Serial.begin(9600); while(!Serial);
esSe.begin(9600); while(!esSe);
}
void loop() {
Serial.flush();
while(Serial.available())
esSe.print((char)Serial.read());
//esSe.write("test");
//esSe.flush();
while(esSe.available())
Serial.println((char)esSe.read());
//delay(10);
}
and when i give Delay of approx 50 milli second it works fine and in delay of 10 it give data and some garbage data too.
Upvotes: 0
Views: 1187
Reputation: 1579
SoftwareSerial
cannot transmit and receive at the same time (see #4 below). This answer lists the choices for serial ports, in this order of preference:
1) HardwareSerial
(you're using this for debug).
2) AltSoftSerial
is very efficient and reliable, but it requires pins 8 & 9 on an UNO.
After that, any other pins can be used with one of these two software serial libraries:
3) NeoSWSerial is less efficient than AltSoftSerial
, but it is much more efficient than SoftwareSerial
. It only supports baud rates 9600, 19200 and 38400, and it support simultaneous TX and RX. I maintain this library.
4) If you must use a different baud rate, SoftwareSerial
is the last choice. It blocks interrupts for long periods of time and can interfere with other libraries. It cannot transmit and receive at the same time.
If you can move to pins 8 & 9, change to AltSoftSerial
. If those pins are not available, change to NeoSWSerial
.
Upvotes: 1