I have connected terminals 2 & 3 (with a 200-ohm resistor) on a 3.3V 8MHz Pro Mini, but cannot get the example code to work.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);
void setup()
{
Serial.begin(9600);
Serial.println("Goodnight moon!");
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available()){
Serial.println("mySerial available");
Serial.print((char)mySerial.read());
}
if (Serial.available())
mySerial.print((char)Serial.read());
}
I get the "Goodnight moon", but not the "Hello, world?" or the "mySerial available". Putting a scope on pin 3 (or 2) shows that the serial signal is there, clean and square and seemingly the right speed (about 1ms per byte). Briefly shorting terminal 2 to ground causes the "mySerial available" to print, telling me that the interrupt is working in some situations. But that's it, no characters. I get the same results with pins 8 & 9 and with a 1k or a wire in place of the 200-ohm, so I don't think it's a hardware or connection issue. I tried inserting pinMode commands as shown in some other examples. What am I missing?
SoftwareSerial does not talk via the standard USB to the PC; you need to connect your PC to pins 2 and 3 as well. You can not do that directly (either a TTL/USB converter or a TTL/RS232 converter).
This is not my program, but the example code from the Arduino Libraries Reference. I believe it is supposed to take input from the PC via the UART, pass it to another device via SoftwareSerial, and then take the receipts from the other device via SoftwareSerial and pass them back to the PC through the UART. In my case, the other device was an XBee, and when that did not work I put a jumper wire in place of the XBee and replaced my code with the example code as part of the debugging process. I understood that the example code would echo back the characters I sent. When you say SoftwareSerial will not talk and listen at the same time, how much delay must there be? The XBee introduces some, but that didn't work any better and I have no control over when the XBee sends. It seems to me that at 9600 Baud, there should be more than enough time for the M328p to both talk and listen.
If I have this wrong, maybe somebody can tell me what the example code is supposed to do.
I believe it is supposed to take input from the PC via the UART, pass it to another device via SoftwareSerial, and then take the receipts from the other device via SoftwareSerial and pass them back to the PC through the UART.
That is correct. The key is the "other device". SoftwareSerial can NOT be used without another device to talk to.
In my case, the other device was an XBee
What kind? How was it configured? What was it communicating with?
and when that did not work
For some undefined definition of work, with no details to back up the claim...
I understood that the example code would echo back the characters I sent.
Your understanding was wrong.
When you say SoftwareSerial will not talk and listen at the same time, how much delay must there be?
It isn't a matter of delay. While it is sending data, it is not listening. While it is listening, it is not sending data. As soon as it gets done sending, it resumes listening.
It seems to me that at 9600 Baud, there should be more than enough time for the M328p to both talk and listen.
For the hardware, that is true. For the software, that is nothing but wishful thinking.
Thank you. Though the original problem remains mysterious, your advice about the flaws in my debugging approach got me to go back and try again. The Xbee is configured for 9600 Baud, 1 start bit, no stop bit or parity. It is not talking to anything else. I checked the Xbee by connecting it to my PC. Per the data sheet, when you send it "+++" (no CRs or LFs) it goes into setup mode and responds with an "OK". This works as advertised when connected directly to the PC. When I used the code shown, with the PC connected to the Arduino UART and the XBee data in connected to pin 3 and the XBee data out connected to pin 2, it did not. My mistake in debugging was changing both the code and the hardware at the same time. Since you said the direct connection would not work, I re-tested the software by using an Arduino Uno and an Xbee connected through 3V-5V level shifters. The test code worked just fine. So, back to the 3V, 8Mhz and now it's working. So, my original code still doesn't work but I now know to leave the hardware alone while debugging.
You can help by answering one more question. The fact that SoftwareSerial can't send and receive at the same time makes it clear that it doesn't work the way I thought it did. And all that C++ code is way beyond me. But tell me, does it use any hardware timers? Since I need to use at least one, I need to make sure there's no conflict there.