Basic Serial and SoftwareSerial Questions

Thanks in advance to anyone who has the patience to read all of this and help me learn. I’ve highlighted specific questions and pleas for help in red :slight_smile:

I’m attempting to learn about serial communications, particularly how to use SoftwareSerial on a Sparkfun Redboard (Arduino Uno). However I’m having some trouble understanding the basics, even after going through some of the easy examples I found online.

I’ve seen a couple examples very similar to the one below (originally seen in this discussion, https://forum.arduino.cc/index.php?topic=161151.0), but I can’t get it to work. From my understanding, I should be able to connect by USB to the Arduino, load this sketch, and then whatever I type in the Arduino IDE serial monitor should get echoed back to me. (Additionally, if I had a device hooked up that was sending responses, this sketch would display the responses to the serial monitor, but I’m not doing that yet.)

I attempted to comment on the sketch below to illustrate how I understand the statements, but please correct my comments if I’ve misunderstood a concept.

#include<SoftwareSerial.h> //Include the SoftwareSerial library

//Setup the mySerial object. We will transmit to the object using Pin 3 and receive

//from the object using Pin 2

SoftwareSerial mySerial(2,3);

void setup()

{

mySerial.begin(9600); //Open the mySerial port for communication

Serial.begin(9600); //Open the serial port for communication

}

void loop()

{

if (mySerial.available()) //Has the mySerial object received something? (i.e. have we

//typed something into the serial monitor and transmitted

//it to the object using Pin 3?)

Serial.write(mySerial.read()); //If yes, read the data going to the object and

//display it to the serial monitor. Basically we are

//echoing what we sent.

if (Serial.available()) //Is there data waiting in the receive buffer of the serial port?

//(i.e. has the object transmitted something that was received

//on Pin 2 which is now waiting in the serial port receive

//buffer?)

mySerial.write(Serial.read()); //If yes, read what is in the serial buffer, and

//transmit it back to the object. I have particular

//trouble understanding this line. If mySerial sent me

//something and it’s sitting in the serial receive

//buffer, why wouldn’t we display it to the serial

//monitor? Instead it looks like we’re sending it right

//back to the mySerial object.

}

Thanks!

Travis

For those interested, I made the same post on an Arduino forum and received additional comments: http://forum.arduino.cc/index.php?topic=445289.new#new

The code forwards data coming in on the SoftwareSerial RX pin to the hardware serial port TX (or D1) pin, which then goes via another chip and the USB port to whatever program is listening on your pc (in your case the serial monitor program)

Similarly data is forward the other way too, data comming from pc- USB port (via the serial translator chip) into the hardware RX/D0 pin of the Arduino, to the software serial TX pin (then going to some attached serial device).

This is done because the hardware serial port and the software serial port (or multiple hardware serial ports in other Arduino boardmodels) are isolated from each other. The program is just the middle (mail)man. When something is received into the right hand (from pc/serial monitor), this transfers it into the left hand. Which the left-hand then sends out (to other hardware) into the world. And vice-versa.

In serial communication “echo-ing” often means the TX pin and RX pin of the same port are physically connected together (or in software by sending out on TX what comes in on RX), so you can check if there is a working path of data communication in both directions upto that point in the chain. Also called ‘null-modem’ sometimes. If the program would echo data then you would get the same character back from it once you send them. If nothing comes then there is a break in the chain. If you get something else than sent, then the signal is corrupted along the way. If you get what you sent out then all is ok (up to that point).

With this program you do not need to have anything connected to the Arduino for it to run. This is correct. But it doesn’t do anything useful either. As it’s sole purpose is to transfer serial data across 2 ports vice-versa. At the very least connect some leds to the software serial pins to see if the pin voltage level changes. (cathode to the pin, anode to Vcc, because the signal is high when idle. Pin going low should light up the led when the zero-bits are sent.) You can probably do without current limiting resistors for the leds but let’s better be safe than sorry and use 1000 ohm in series with them.