I plan on having two serial devices connected to an arduino. I am not sure whether to use the uno or mega. I am writing a program for autonomous navigation. It will use one of the serial ports to receive gps information and calculate its course based on that, but I want to be able to override that with manual commands from a computer via an xbee (the other serial connection).
Is it possible just to use softserail and monitor it for a command?
LittleDice:
Is it possible just to use softserail and monitor it for a command?
Yes, although you'd be better off putting the GPS on the software UART - missing or misinterpreting user commands is more risky than corrupting a single packet of GPS data.
#include <NewSoftSerial.h>
NewSoftSerial nss(rx, tx);
// [...]
while (nss.available()) {
char c = nss.read();
}
Don't use the default SoftwareSerial class - it's inefficient in comparison to NewSoftSerial, and its only advantage is that it's included with the IDE.
This means that you can’t write code like this:
void loop()
{
if (device1.available() > 0)
{
int c = device1.read();
...
}
if (device2.available() > 0)
{
int c = device2.read();
...
}
}
This code will never do anything but activate one device after the other.
That's if you've got more than one software serial port. NSS only supports one port active at a time - each port needs to be set to active when it's being used. However, you can use as many hardware UARTs as you've got avalable + one NSS port without issues.
Please read ALL of the page, as well as ALL of the examples, before pulling something out of context.
This line: "An important point here is that object.available() always returns 0 unless object is already active." is literally right before the one you quoted, but you quite evidently didn't read it. (emphasis mine).
I mean no disrespect I am just trying to understand how it works. So basically I can just make a loop and run through it to keep checking if I am overriding it?
The *.available() method returns the number of bytes in the buffer. You will need to periodically check if it is non-zero (buffer has bytes to be read), and then process them if they are available.