Arduino mega serial1,2,3 programming question

Hi all ..

It does not work properly.

Any idea?

using megatron :slight_smile: 2560;

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
  }
}

Arlove:
It does not work properly.

Any idea?

Explain more detailed!
WHAT "does not work properly"?
"It"? What is "it" that doesn't work?

And what's that:

 delay(5);

You are blocking all program execution during serial communication for some time?

With 9600 baud there may be up to 960 characters per second transmitted.

But with a blocking time of 5 milliseconds after each character, you can handle at most 200 characters per second.

ty jurs I need 3 serial communication

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.

...R

ty Robin2 but I cant this . I need first serialread 1 after need serial 2 and finally need serialread 3 with mega.

is it impossible ?

Is it true? that I have written my code

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
}
}

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

...R

Already I said my problem I need serial controller. Using serial read all 1,2,3 RX1 TX1- RX2 TX2- RX3 TX3

for ex:

first

coming bayt serial1 and write serial ( this is important for me serial1--->serial) 1-0 it is basic

second

I need if coming serial and write serial3 ( this is very very important serial--->serial3!) 0-3

second or third

coming bayt serial3 and write serial ( this is important for me serial3--->serial) 3-0 it is basic

I hope...Robin understood me

thank you for everything.. @Robin2

Arlove:
I hope...Robin understood me

I don't think you have understood my question.

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.

...R

But even that is not simple because you cannot prevent Serial2 from receiving a byte while you are listening to Serial1.

You could, of course, save the data read from each port in a separate array. But, that seems to be well beyond OPs capabilities.

hmm now using mega 2560

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
    }
  }
}

any idea? thank you for support..

Serial.begin(115200);
Serial2.begin(115200);
Serial3.begin(19200); impossible

this code perfect but only gps

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.

Serial.begin(115200);
Serial2.begin(115200);
Serial3.begin(19200); impossible

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.

It does not make it impossible to have different baud rate! so Serial2 Serial2.begin(115200);
Serial3.begin(19200); and Serial.begin(115200)

in this case Serial3.begin(19200) is can can work?

All 4 serial ports can operate at different speeds. The speed that a port is operated at must match the speed of the device on the other end.

is this code true PaulS?

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
  }
}

PaulS:
All 4 serial ports can operate at different speeds. The speed that a port is operated at must match the speed of the device on the other end.

Thank you I will do