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.