SoftwareSerial Causing bad reading ?

I'm using Arduino mini, since it doesn't have any UART port left. I thought to use SoftwareSerial, but it turned out not very good. Its making hardware serial to read bad data.

For example, if you upload the following code, and open the SerialMonitor and write any text longer than 4 characters, it will trim the middle text. Like 123456789, will only read 123789. But if I comment the line "mySerial.write(buffer2,toRead);" it works as expected. Is there anyway to fix this ?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(5, 6); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() { // run over and over
  //if (mySerial.available()) {
 //   Serial.write(mySerial.read());
 // }
  if (Serial.available()) {
    int toRead = Serial.available();
    byte buffer2[256];
    Serial.readBytes(buffer2, toRead) ;
    mySerial.write(buffer2,toRead);
    Serial.write(buffer2,toRead);
  }
}

SoftwareSerial disables interrupts for the entire time it is transmitting each character. This means that transmitting just one character with mySerial.write is preventing Serial from saving up to 57600/9600, or about 6 characters. The UART can buffer 2 of them before Serial gets to them, which explains the 4 lost chars.

The best way to have a second serial port is with AltSoftSerial on pins 8 & 9.

Next best is NeoSWSerial on any other pins, but only at bauds 9600, 19200 and 38400.

Worst is SoftwareSerial. :stuck_out_tongue:

Cheers,
/dev

Can you read all the input data before you send any data?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R