# SoftwareSerial is hard

**URL:** <https://forum.arduino.cc/t/softwareserial-is-hard/956522>\
**Category:** Programming\
**Created:** [February 8, 2022, 3:39am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522 "2022-02-08T03:39:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 3:39am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/1 "2022-02-08T03:39:32Z")

</div>

I could just be being stupid, but the following soft serial test is throwing me for a loop. The output reads "-1". Here's my codes:

Sender:

```cpp
#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9);

void setup()
{
  Serial.begin(115200);
  while (!Serial) {
  }

  mySerial.begin(9600);
}

void loop() 
{
  mySerial.write(10);
}

```

Receiver:

```cpp

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9);

void setup()
{
  Serial.begin(115200);
  while (!Serial) {
  }

  mySerial.begin(9600);
}

void loop()
{
  int thing = mySerial.read();
  Serial.println(thing);
}

```

I feel like I'm doing something horribly wrong but I can't find much documentation about software serial. Is there a way I can fix this? I'm just trying to send the number 10. Later I plan to send a pretty large int array.

Can someone help me get software serial working so I can continue with my project?

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 8, 2022, 3:51am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/2 "2022-02-08T03:51:03Z")

</div>

> [@anon77129126](#):
>
> ```arduino
> int thing = mySerial.read();
> Serial.println(thing);
> 
> ```

You actually need to test to see if there is something in the buffer to read. Since you got a -1, that means there is nothing in the buffer to read.

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [February 8, 2022, 3:55am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/3 "2022-02-08T03:55:06Z")

</div>

Also, look into the difference between . **write** () and . **print** ()

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 3:59am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/4 "2022-02-08T03:59:14Z")

</div>

How do I do that?

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 4:00am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/5 "2022-02-08T04:00:42Z")

</div>

Should I use:

```cpp
mySerial.print("10", DEC);

```

instead?

EDIT - Looks like that's only good for chars. How do I send numbers?

EDIT - I tried removing the quotes. Still getting -1.

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [February 8, 2022, 4:19am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/6 "2022-02-08T04:19:20Z")

</div>

What did you read ?  
.**write()** is for simple binary values, while .**print()** will look after the conversion into characters for display.

Look at reply #2 for the -1 issue.

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 4:21am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/7 "2022-02-08T04:21:31Z")

</div>

I'm just trying to send an int. Currently using

```cpp
#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9);

void setup()
{
  Serial.begin(115200);
  while (!Serial) {
  }

  mySerial.begin(9600);
}

void loop() 
{
  mySerial.print(10, DEC);
}

```

on sender. Same code as earlier on the receiving side.

I've already seen reply 2. I'm not sure how to do that.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 8, 2022, 4:22am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/8 "2022-02-08T04:22:13Z")

</div>

> [@anon77129126](#):
>
> Should I use:

No; I'm reasonably sure that the compiler will throw a warning. Use _mySerial.write()_ as you did if you want to send numbers.

> [@anon77129126](#):
>
> How do I do that?

You can either ignore the received data if it's -1 or you can use _mySerial.available()_ to test if there is data and only read in that case.

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 4:24am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/9 "2022-02-08T04:24:13Z")

</div>

Changed the sender code back to the original. Receiver code:

```cpp

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9);

void setup()
{
  Serial.begin(115200);
  while (!Serial) {
  }

  mySerial.begin(9600);
}

void loop()
{
  if (mySerial.available()) {
    int thing = mySerial.read();
    Serial.println(thing);
  }
}

```

Currently no output on the serial monitor.

EDIT - I need to sleep. I will be responsive again ~11:00 EST.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [February 8, 2022, 4:35am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/10 "2022-02-08T04:35:34Z")

</div>

Check the tx goes to rx and vice versa.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [February 8, 2022, 5:31am UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/11 "2022-02-08T05:31:39Z")

</div>

> [@anon77129126](#):
>
> but I can't find much documentation about software serial. I

Documentation: [https://www.arduino.cc/en/Reference/SoftwareSerialRead](https://www.arduino.cc/en/Reference/SoftwareSerialRead)  
it comes up as [one of the first search results](https://www.google.com/search?q=soft+serial+read).

it's clearly described that the function read() will return -1 if nothing is available on SoftSerial.

easiest way is to check if the readed value is \>=0 ...then you have received a character on SoftSerial.

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 12:51pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/12 "2022-02-08T12:51:30Z")

</div>

I have a buffer check in place as per the request of someone else.

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 12:52pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/13 "2022-02-08T12:52:47Z")

</div>

That might be it 😐

Thinking of my circuit, I had pin 8 connected to pin 8 and pin 9 connected to pin 9 instead of crossing the two.

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 3:15pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/14 "2022-02-08T15:15:42Z")

</div>

Okay, so if I did theoretically get this working, I need to send data packets back and forth between two devices. Assume that device 1 is an Arduino Nano, and device 2 is an Arduino Micro (my current test setup, the Nano will be replaced with a WeMos D1 Mini in the final version).

Device 1 needs to send a data packet (probably an array) of ints containing various values. Device 2 needs to receive the data packet, and then send it's own data back.

This will repeat multiple times per second over the soft serial connection.

How would I structure the packets, clear the buffer, etc?

EDIT - If my requests are impossible to fulfill with serial I am looking into I2C.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 8, 2022, 6:16pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/15 "2022-02-08T18:16:33Z")

</div>

They are not impossible, actually quite easy.

Maybe start by telling us what you want to send from 1 to 2 and what from 2 to 1. Sending as text would be the easiest (see Robin's _updated serial input basics_ on this forum to get ideas); sending in binary format is a little more complicated as you need to make sure that you know where packets start and end.

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 6:26pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/16 "2022-02-08T18:26:03Z")

</div>

I need to sent an array from the Micro to the Nano that looks like this (or similar, I don't really know yet):

```cpp
int dataPacket[9] = { var1, var2, var3, var4, var5, var6, var7, var8, var9, var10 };

```

I then need to send an identical array from the Nano to the Micro. This should start upon startup of both Arduinos (they will start at the same time) and continue until both are turned off.

I need the data to be exchanged rapidly.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 8, 2022, 6:50pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/17 "2022-02-08T18:50:24Z")

</div>

On the Micro

```c++
Serial1.write(*byte*)dataPacket, sizeof(dataPacket));

```

On a phone right now, can't give you exact Nano code. The basics would be something like

```c++
if (mySerial.avalaible() > 18)
{
  read and store 18 bytes
}

```

This excludes any hardening that would make sure that your packets are complete and not corrupt.

Note: I'm not sure about endianness difference between ESP and AVR (Micro, Nano etc); if they differ, you will need yo take that into account.

> [@anon77129126](#):
>
> rapidly

That does not convey any information.

Also be aware that SoftwareSerial can't run on high speeds.

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 6:54pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/18 "2022-02-08T18:54:57Z")

</div>

> [@sterretje](#):
>
> This excludes any hardening that would make sure that your packets are complete and not corrupt.

I don't care if the data is corrupt, the bad data will be replaced with new data in milliseconds.

EDIT - If the data continues coming in corrupt then there's a problem though.

> [@sterretje](#):
>
> Note: I'm not sure about endianness difference between ESP and AVR (Micro, Nano etc); if they differ, you will need yo take that into account.

I doubt there's any differences but there's only one way to find out.

EDIT - There's also a software serial implementation for ESP systems.

> [@sterretje](#):
>
> That does not convey any information.

The system should have a maximum of 2ms delay.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [February 8, 2022, 6:58pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/19 "2022-02-08T18:58:55Z")

</div>

> [@anon77129126](#):
>
> I doubt there's any differences but there's only one way to find out.

Why do I feel like the one way you think there is to find out is

not the only way

not the best way

I know you'd rather just hack your way through this and come here asking questions when you get a bit stuck, but spend some time with this

[Serial Input Basics - updated](https://forum.arduino.cc/t/serial-input-basics-updated/382007)

a7

---

<div class="post-metadata">

**Author:** ![anon77129126](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon77129126](https://forum.arduino.cc/u/anon77129126)\
**Post date:** [February 8, 2022, 7:04pm UTC](https://forum.arduino.cc/t/softwareserial-is-hard/956522/20 "2022-02-08T19:04:18Z")

</div>

It looks like the tutorial you linked to is specifically focused on ASCII

[Next page](https://forum.arduino.cc/t/softwareserial-is-hard/956522.md?page=2)
