SoftwareSerial Communication Issues

I've successfully used Software Serial a number of times in the past to communicate in both directions with other devices, often through the use of the sparkfun RS232 level shifter.

An excerpt from a working program is below:

#include <Bridge.h> //needed for comm between microcontroller and proccesor on Yun
#include <FileIO.h> //Used for logging data to a file stored on the microSD card
#include <Process.h> //Used to run php scrips from the Linux command line
#include <SoftwareSerial.h> //Used for serial communication over pins other than the hardware serial pins. Only pins 8,9,10,11,14,15,16 can be used for RX line on Yun.
SoftwareSerial Station1(8, 2); // RX, TX
SoftwareSerial Station2(9, 3); // RX, TX
SoftwareSerial Station3(10, 4); // RX, TX
SoftwareSerial Station4(11, 5); // RX, TX
SoftwareSerial LCD(13, A0); // RX, TX     only TX line used

void setup() {

  Serial.begin(9600);
  Station1.begin(9600); //sets baud rate for serial connection
  Station2.begin(9600); //sets baud rate for serial connection
  Station3.begin(9600); //sets baud rate for serial connection
  Station4.begin(9600); //sets baud rate for serial connection
  LCD.begin(9600);
  delay(5000);
  Bridge.begin(); //initiate the SPI based communication between the microcontroller and processor on the Yun
  FileSystem.begin(); //Initializes the SD card and FileIO class
  delay(5000);


void Read1() {
  Station1.listen();  //turns the RX line on for this station
  Station1.println("M"); //sends the readout a command to send back 1 reading
  delay(delayfactor); //delay for processing
  result = ""; //clear out the result string
  while (Station1.available() > 0) {   // read the output of the command
    char c = Station1.read();  //read the next character
    if (isDigit(c))  //only store charaters that are digits (some other control characters and spaces are present
      result += c;
  }
  readings[0] = result.toFloat() / 100.0;  //conver the string into a floating point number
}

My loop calls the Read1,2,3,4 functions under certain circumstances and everything works perfectly.

I don't know what's wrong, but I can't seem to get SoftwareSerial to work in my new programs. For example, see the below self-test script I wrote:

#include <SoftwareSerial.h> //Used for serial communication over pins other than the hardware serial pins. Only pins 8,9,10,11,14,15,16 can be used for RX line on Yun.
SoftwareSerial MySerial(10, 11); // RX, TX

int n;

void setup() {
  Serial.begin(9600); //starts serial communication for serial monitor and bridge communication
  MySerial.begin(4800);
  delay(5000); //delay to allow serial to start
  MySerial.listen();
  Serial.println("Booted");
}

void loop() {
  MySerial.println("Data from port script");
  MySerial.write(120);
  delay(100);
  while (MySerial.available() > 0) {
    char inByte = MySerial.read();
    Serial.println("Byte");
  }
  delay(2000);
  Serial.println("Looped");
  Serial.println(n);
  n++;
}

Every time a byte is available, I should see "Byte" on my serial console, but all I ever get is the "Looped" and the counter.

On the hardware side for this sketch, 10 and 11 are just jumpered together.

Thanks for any thoughts.

That's very curious, I don't immediately see anything wrong with it. I tried in on one of my Yuns, and I can confirm that the data is being sent out, so the problem appears to be on the receive side. Like you, I have used SoftwareSerial successfully in the past.

My first thought is that on the '32U4 based Arduinos, like the Leonardo and Yun, only certain pins can be used for receive, but you are using a valid pin. So that's not it.

A problem like this usually turns out to be something silly and obvious - so obvious that it's overlooked the first dozen time you look for it. Hopefully someone will see the problem and figure out a solution.

I'm still puzzled by this.

I went into the library and changed the error Return for Read to give a little clearer picture.
Read normally returns -1 for both the port not being listened to and and empty buffer. I changed the error for empty buffer to a unique number and that is the error code that comes back out of the Read function. So the TX data is never making it into the RX buffer.

The Write function is supposed to return a 1 if it fully executes and it does return a 1.

I can't read any data off of the TX line from my computer (through a level shifter) or with it jumpered to the RX line on the Yun. The level shifter lights do show activity, but no data seems to come through.

DarkSabre:
I can't read any data off of the TX line from my computer (through a level shifter)

Hmmm... this can be read two ways:

  • Can't read data from the computer's TX line (receiving on Yun)
  • Can's read data from the Yun's TX line (receiving on computer)

If you mean 1, that would make sense based on my duplication of the issue, as the Yun is transmitting, but it can't receive. If you mean 2, then that is different than my experience, because using your code and a logic analyzer, I could verify that the data is being transmitted properly.

Well, what I meant was that I couldn't read the data from a Yun on the computer (still have not managed to).

That said, I hooked up an o-scope and verified like you that there is data transmission going on (attached) from the Yun TX line. I'm going to try to look tomorrow to confirm that the data matches what it should be sending.

DarkSabre:
Well, what I meant was that I couldn't read the data from a Yun on the computer (still have not managed to).

You say you have a level shifter, does your level shifter also invert the data? Assuming your computer is expecting RS-232 levels, you must invert the data that comes out of the Yun.

I'm going to try to look tomorrow to confirm that the data matches what it should be sending.

On my end, I did verify that I was getting the right data. I'd be surprised if you aren't, but it's still worth verifying it on your part.