SoftwareSerial is hard

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:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9);

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

  mySerial.begin(9600);
}

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

Receiver:


#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?

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.

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

How do I do that?

Should I use:

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.

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.

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

#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.

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

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.

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


#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.

Check the tx goes to rx and vice versa.

Documentation: https://www.arduino.cc/en/Reference/SoftwareSerialRead
it comes up as one of the first search results.

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.

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

That might be it :neutral_face:

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

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.

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.

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

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.

On the Micro

Serial1.write(*byte*)dataPacket, sizeof(dataPacket));

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

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.

That does not convey any information.

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

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.

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.

The system should have a maximum of 2ms delay.

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

a7

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