Sending data from two Serial ports to another Serial port

Hello,

As mentioned in the title, I'm trying to send the data that I got from two other serial ports by using another serial port.

The serial ports I use to receive data: Serial2 and Serial3
The serial port that I want to use to send the received data in Serial2 and Serial3: Serial1

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

TinyGPSPlus gps1;
TinyGPSPlus gps2;

char data_1;
char data_2;

double  lat1;
double  lat2;
double  lon1;
double  lon2;

TinyGPSCustom pdop(gps1, "GPGSA", 15);
TinyGPSCustom pdop2(gps2, "GPGSA", 15);
TinyGPSCustom vdop(gps1, "GPGSA", 17);
TinyGPSCustom vdop2(gps2, "GPGSA", 17);

void setup() {
   Serial1.begin(4800);
   Serial2.begin(4800);
   Serial3.begin(4800);
   pinMode(13, OUTPUT); //LED
   pinMode(18, OUTPUT); //LED
   pinMode(19, OUTPUT); //LED
   pinMode(20, OUTPUT); //DE
   pinMode(21, OUTPUT); //RE#
  }

void loop() {

  while (Serial3.available() > 0) //Second GPS receiver
    if (gps1.encode(Serial3.read())){
      displayInfo1(); //function to print variables such as time, speed and course.
      afstand_bearing_koers(); //function to print variables such as distance and bearing between two GPS receivers.
      digitalWrite(18, HIGH);
    }
    
  if (millis() > 5000 && gps1.charsProcessed() < 10)
  {
    Serial.println(F("GPS1 niet gevonden: check bedrading."));
    while(true);
  }

  while (Serial2.available() > 0) //First GPS receiver
    if (gps2.encode(Serial2.read())){
      displayInfo2();  //function to print variables such as time, speed and course.
      afstand_bearing_koers(); //function to print variables such as distance and bearing between two GPS receivers.
      digitalWrite(19, HIGH);
    }
    
  if (millis() > 5000 && gps2.charsProcessed() < 10)
  {
    Serial.println(F("GPS2 niet gevonden: check bedrading."));
    while(true);
  }
}

void displayInfo1() //printing relevant data from the received nmea-sentences starts here.
{
  Serial.print(F("GPS1: "));
  if (gps1.location.isValid())
  {
    lat1 = gps1.location.lat();
    Serial.print(lat1, 6);
    Serial.print(F(","));
    lon1 = gps1.location.lng();
    Serial.print(lon1, 6);
    Serial.print(F(" "));
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  if (gps1.speed.isValid())
  {
    Serial.print(gps1.speed.knots());
    Serial.print(F(" "));
    Serial.print(gps1.speed.kmph());
    Serial.print(F(" "));
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  if (gps1.course.isValid())
  {
    Serial.print(gps1.course.deg());
    Serial.print(F(" "));
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  if (gps1.time.isValid())
  {
    if (gps1.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps1.time.hour());
    Serial.print(F(":"));
    if (gps1.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps1.time.minute());
    Serial.print(F(":"));
    if (gps1.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps1.time.second());
    Serial.print(" ");
  }
  else
  {
    Serial.print(F("INVALID"));
  }

    if (gps1.altitude.isValid() && gps1.satellites.isValid() &&
    gps1.hdop.isValid())
  {
    Serial.print(F("ALT= "));  Serial.print(gps1.altitude.meters()); 
    Serial.print(" PDOP= ");    Serial.print(pdop.value());
    Serial.print("|");
    Serial.print(F(" HDOP= ")); 
    double h_dop = gps1.hdop.value();
    Serial.print(h_dop / 100);
    Serial.print(" ");
    Serial.print("VDOP= "); Serial.print(vdop.value());
    Serial.print("|");
    Serial.print(F(" SATS= ")); Serial.print(gps1.satellites.value());
  }
  
}

void displayInfo2()
{
  Serial.print(F("GPS2: "));
  if (gps2.location.isValid()) //Laat locatie zien
  {
    lat2 = gps2.location.lat();
    Serial.print(lat2, 6);
    Serial.print(F(","));
    lon2 = gps2.location.lng();
    Serial.print(lon2, 6);
    Serial.print(F(" "));
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  if (gps2.speed.isValid())
  {
    Serial.print(gps2.speed.knots());
    Serial.print(F(" "));
    Serial.print(gps2.speed.kmph());
    Serial.print(F(" "));
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  if (gps2.course.isValid())
  {
    Serial.print(gps2.course.deg());
    Serial.print(F(" "));
  }
  else
  {
    Serial.print(F("INVALID "));
  }
  
  if (gps2.time.isValid())
  {
    if (gps2.time.hour() < 10) Serial.print("0");
    Serial.print(gps2.time.hour());
    Serial.print(":");
    if (gps2.time.minute() < 10) Serial.print("0");
    Serial.print(gps2.time.minute());
    Serial.print(":");
    if (gps2.time.second() < 10) Serial.print("0");
    Serial.print(gps2.time.second());
    Serial.print(" ");
  }
  else
  {
    Serial.print(F("INVALID"));
  }

    if (gps2.altitude.isValid() && gps2.satellites.isValid() &&
    gps2.hdop.isValid())
  {
    Serial.print(F("ALT= "));  Serial.print(gps2.altitude.meters()); 
    Serial.print(" PDOP= "); Serial.print(pdop2.value());
    Serial.print("|");
    Serial.print(F(" HDOP= "));
    double h_dop_2 = gps2.hdop.value(); 
    Serial.print(h_dop_2 / 100);
    Serial.print(" ");
    Serial.print("VDOP= "); Serial.print(vdop2.value());
    Serial.print("|");
    Serial.print(F(" SATS= ")); Serial.print(gps2.satellites.value());
  }
  
}

//void interpolatie(){
//  //punt maken zodat de afstand tussen twee nauwkeurigere punten gemeten kan worden
//  nieuwlat1 = 1.5*lat1;
//  nieuwlon1 = 1.5*lon1;
//  nieuwlat2 = 1.5*lat2;
//  nieuwlon2 = 1.5*lon2;
//}

void afstand_bearing_koers(){
  if (gps1.location.isValid() && (gps2.location.isValid()))
  {
  Serial.print(" Afstand= ");
  unsigned long afst =
  Serial.print((TinyGPSPlus::distanceBetween(
    lat1,
    lon1,
    lat2,
    lon2)));
  
  Serial.print(" Koers= ");
  double koers =
  Serial.print(TinyGPSPlus::courseTo(
    lat1,
    lon1,
    lat2,
    lon2));
  const char *cardinalGPS = TinyGPSPlus::cardinal(koers);
  Serial.print(" ");
  Serial.print(cardinalGPS);
  Serial.print(" ");
  }
  else
  {
    Serial.print("INVALID");
  }

  Serial.print("Referentiepunt= ");
  double ref = 
  Serial.print(((lat1+lat2)/2),6);
  Serial.print(",");
  Serial.println(((lon1+lon2)/2),6);
}

I saw other people using Serial.write() to send data. But the functions that I use to print the relevant data from the GPS receivers are voids. So I can't use something like Serial.write(). So what can I do to send the same data by using Serial1 that I get in my Serial Monitor from Serial2 and Serial3?

Change "Serial.print" to "Serial1.print". If you want the output to go to both, make a copy of each "Serial.print..." line and change one of them to "Serial1.print...".

Thanks for the quick reply! I changed some of the Serial.print() to Serial1.print() and those data were not visible in the Serial Monitor, which is right. I think I should say that I'm using a USB-RS485-WE cable which is connected to the pins of Serial1. What happened just now was when I started receiving data via this cable, my cursor started moving rapidly and opened some other programs that were already openend, like crazy. Pulling the cable out stopped doing it. I think it's a software issue, but I have no idea where...

whenever I make DE (pin20) HIGH (to send data from Serial1), this problem starts.

Quite funny but strange, it works perfectly now!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.