void setup()
{
Serial.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);
delay(500);
}
void loop()
{
if (Serial2.available()){ // if the shield has something to say
Serial.write(Serial2.read()); // display the output of the shield
}
delay(5);
if (Serial3.available()){ // if the shield has something to say
Serial.write(Serial3.read()); // display the output of the shield
}
}
If that was my project I don't think I would process the data one byte at a time. I would receive a complete message on Serial2 and a complete message on Serial3 and then send them to the PC.
The examples in serial input basics are simple reliable non-blocking eays to receive data. You could make two separate copies of the function (with 2 separate buffers, obviously) to receive data from Serial2 and Serial3.
if (Serial2.available()){ // if the shield has something to say
Serial.write(Serial2.read()); // display the output of the shield
}
delay(5);
if (Serial3.available()){ // if the shield has something to say
Serial.write(Serial3.read()); // display the output of the shield
}
}
void loop()
{
if (Serial2.available()){ // if the shield has something to say
Serial.write(Serial2.read()); // display the output of the shield
}
delay(5);
if (Serial3.available()){ // if the shield has something to say
Serial.write(Serial3.read()); // display the output of the shield
}
}
The problem with this code is that you have no control over which port produces the next byte so whatever is receiving the data will have no way to know which character came from which serial port.
If you explain what you are trying to do (rather than how you think it might be done) we may be able to give better advice
Tell us what the project is for. For example is to control a coffee maker or the door of a chicken-coop ? (both of which regularly appear in the Forum)
If you really need to receive bytes from Serial1 and then from Serial2 you will have to write code that waits until it actually receives a byte from Serial1 before it tries to get a byte from Serial2.
But even that is not simple because you cannot prevent Serial2 from receiving a byte while you are listening to Serial1.
I have a sim900 gsm (it is need uart1) rx1-tx1 (19200)
and I have a sim28 gps module (it is need uart2) rx2 tx2 (115200)
and I have a serial monitor this is a usb cable (it is need serial uart0) rx tx
and another problem gps and gsm module different baud rate I hate this.
this code perfect but only gps I need gps and gsm different baud rate. so I need all uart using.
#include <TinyGPS.h>
#include <String.h>
TinyGPS shield;
void setup()
{
Serial.begin(115200);
Serial2.begin(115200);
}
// The getgps function will interpret data from GPS and display on serial monitor
void getgps(TinyGPS &gps)
{
// Define the variables that will be used
float latitude, longitude;
// Then call this function
shield.f_get_position(&latitude, &longitude);
Serial.println("--------------------------------------------------------------");
Serial.print("latitude: ");
Serial.print(latitude,5);
Serial.print(" longitude: ");
Serial.println(longitude,5);
int year;
byte month, day, hour, minute, second, hundredths;
shield.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
// Print data and time
Serial.print("GMT(+3) HOUR- ");
Serial.print((hour+3), DEC);
Serial.print(":");
if (minute<10)
{
Serial.print("0");
Serial.print(minute, DEC);
}
else if (minute>=10)
{
Serial.print(minute, DEC);
}
Serial.print(":");
if (second<10)
{
Serial.print("0");
Serial.print(second, DEC);
}
else if (second>=10)
{
Serial.print(second, DEC);
}
Serial.print(" ");
Serial.print(day, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.println(year, DEC);
Serial.print("altitude-speed ");
Serial.print(gps.f_altitude());
Serial.print("m ");
Serial.print(gps.f_speed_kmph());
Serial.println("km/h");
}
void loop()
{
byte a;
if ( Serial2.available() > 0 ) // if there is data coming from the GPS shield
{
a = Serial2.read(); // get the byte of data
if(shield.encode(a)) // if there is valid GPS data...
{
getgps(shield); // then grab the data and display it on the LCD
}
}
}
Because that is the ONLY thing you have programmed it do deal with.
Now that you KNOW you can get data from the GPS which is connected to the Serial2 pins, it is time to write some code to get data from/send data to other serial ports.
Calling Serial3.begin() is NOT impossible. But, you have not described that there is anything attached to the Serial3 pins. Nor have you shown any code that attempts to use the Serial3 instance. Nor have you said what makes you think that using Serial3 is impossible.
void setup()
{
Serial.begin(115200);
Serial2.begin(115200);
Serial3.begin(19200);
delay(500);
}
void loop()
{
if (Serial2.available()){ // if the shield has something to say
Serial.begin(115200);
Serial.write(Serial2.read()); // display the output of the shield
}
if (Serial3.available()){ // if the shield has something to say
Serial.begin(19200);
Serial.write(Serial3.read()); // display the output of the shield
}
}