Arduino mega serial ports communication

Hello guys help me please!
I am doing project for vehicle tracking. i have to send latitude and longitude to online server. i am using arduino mega serial1 port for GSM and serial2 port for GPS. Through GSM/GPRS i have to send data to server. GPS is getting coordinates well and also GSM is sending data to server well.
Problem is this that i don’t know how can i send serial2 port (GPS) latitude and longitude to serial1 port GSM to server.How i merge program please help!

GPS CODE:

#include “TinyGPS++.h”
TinyGPSPlus gps;

void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{

if (Serial1.available())
{
gps.encode(Serial1.read());
}

if (gps.location.isUpdated())
{
Serial.print("LAT= "); Serial.println(gps.location.lat(), 7);
Serial.print(“LNG= “); Serial.println(gps.location.lng(), 7);
Serial.print(”\n”);
}
}

GSM Code:

<

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

//SoftwareSerial mySerial(17, 16); // RX, TX
TinyGPSPlus gps;

int addr = 0;
int trig = 7;
int echo = 8;
//tmElements_t tm;

void setup() {
Serial.begin (9600);
Serial2.begin (9600); // For PC and ardrino communication we setup bits/second (baud rate)

gps.begin
//Serial2.begin(9600); // Open serial port 1 (TX=pin 18, RX=pin 19)
Serial.print(“Starting…\n”);
}

void loop(){

Serial2.println(“AT+SAPBR=3,1,“APN”,“internet””);
ShowSerialData(2000);

Serial2.println(“AT+SAPBR=3,1,“CONTYPE”,“GPRS””);//setting the SAPBR, the connection type is using gprs
ShowSerialData(2000);

Serial2.println(“AT+SAPBR=1,1”);
ShowSerialData(2000);

//initiating HTTP
Serial2.println(“AT+HTTPINIT”);
ShowSerialData(2000);

Serial.println(“Sending sensor data to sevrer”);

Serial2.println("AT+HTTPPARA=“URL”,“http://www.expertsstack.com/monitor.php/?lat=3&lng=2&tc=10"”);
ShowSerialData(2000);

Serial2.println(“AT+HTTPACTION=1”);
ShowSerialData(2000);

delay(2000);

}

void ShowSerialData(int cmdDelay)
{
//Serial211111.print(“asdf”);
Serial2.print("\r");
delay(cmdDelay);
while(Serial2.available()!=0)
{
Serial.write(Serial2.read());
}
}

/>

Please show code in code tags </>.

I don’t see your problem. Provided that the GSM code uses Serial2, where it should use Serial1 in the combined code, you only have to change all occurrences of these names.

For better maintainable code you can use #define to create better names for the serial ports, e.g.

#define gsmPort Serial1

Then you can redirect gsmPort to whatever serial port you like, in a single place. In the remaining code use gsmPort instead of SerialX.

Hi, Combining two sketches can be difficult.. See some suggested ways to approach that HERE:

Stay organized, and only add 1 or 2 lines or change 1 or 2 lines at a time and then click VERIFY.