Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: xmen on Aug 11, 2016, 05:03 pm

Title: SoftwareSerial Causing bad reading ?
Post by: xmen on Aug 11, 2016, 05:03 pm
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 ?

Code: [Select]
#include <SoftwareSerial.h>

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

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

void loop() { // run over and over
  //if (mySerial.available()) {
 //   Serial.write(;
 // }
  if (Serial.available()) {
    int toRead = Serial.available();
    byte buffer2[256];
    Serial.readBytes(buffer2, toRead) ;
Title: Re: SoftwareSerial Causing bad reading ?
Post by: -dev on Aug 11, 2016, 05:25 pm
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.  :P

Title: Re: SoftwareSerial Causing bad reading ?
Post by: Robin2 on Aug 11, 2016, 08:34 pm
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.