Hello. I've made a search on google but do not find an answer to my question, and the examples didn't work.
I have an Arduino UNO with 2 SoftwareSerial ports: one using the pins 2 and 3, and the other 10 and 11.
The first (pins 2 and 3) are connected to a GPS, and the second (10 and 11) are connected to a external cable. My need is: receive data from GPS, format it and send to the cable. but this do not work.
For a test I wrote only one line for the second SS. If I comment these lines I can monitor the data. If I comment I only can see the data from second software serial port.
//SoftwareSerial mySerialB(10, 11);
//mySerialB.begin(9600);
//mySerialB.println("mySerialB");
Full code:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
SoftwareSerial mySerialA(2, 3);
//SoftwareSerial mySerialB(10, 11);
TinyGPS gps;
void gpsdump(TinyGPS &gps);
void printFloat(double f, int digits = 2);
void setup()
{
mySerialA.begin(9600);
//mySerialB.begin(9600);
Serial.begin(9600);
delay(1000);
}
void loop()
{
bool newdata = false;
//mySerialB.println("mySerialB");
if (mySerialA.available())
{
char c = mySerialA.read();
if (gps.encode(c))
{
newdata = true;
}
}
if (newdata)
{
gpsdump(gps);
}
}
void gpsdump(TinyGPS &gps)
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print("Lat: ");
printFloat(flat, 5);
Serial.println(" ");
Serial.print("Lon: ");
printFloat(flon, 5);
Serial.println(" ");
}
void printFloat(double number, int digits)
{
// Handle negative numbers
if (number < 0.0)
{
Serial.print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i) rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
Serial.print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) Serial.print(".");
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
Serial.print(toPrint);
remainder -= toPrint;
}
}