Can I initilize 3 serial port in setup loop?

Dear All,

Actually I am rying hard to make working a GPS and GPRS module with Arduino Uno

In my setup loop , I initilize
Serial.begin() and cell.begin() but when I intialize uart_gps.begin(), the program do not retun the GPS fix while it work well.

if I start to initilize Serial.begin() and uart_gps.begin, I can get the GPS fix. But if later I initlize cell.begin, it freez while the GPRS start sending data to the server. It’s like if it do not accept the initialize 3 port or if the RAM goes down…

Does it tell you something?
Could you elucidate me?

void setup()
{
  //Initialize serial ports for communication with computer
  Serial.begin(TERMBAUD);
  //uart_gps.begin(GPSBAUD);
  cell.begin(GPRSBAUD);
  
  
  pinMode(redGPS, OUTPUT); // GPS red
  pinMode(blueGPS, OUTPUT); // GPRS blue
  pinMode(greenGPRS, OUTPUT); // GPRS green
 
 
   #ifdef LCDD 
  lcd.begin(numCols, numRows);
  lcd.blink();
  
  displayLCD("******************* Wellcome ****** to iBip  *******************");
  #endif
  
  #ifdef DEBUG
    Serial.println(F(" "));
    Serial.println(F("*************************************"));
    Serial.println(F("* Starting iBip Communication... *"));
    Serial.println(F("*************************************"));
    Serial.println(F(" "));
    

  #endif
/*
  Serial.println(F("Wait for module registered"));
  waitTil("+SIND: 11"); // keep printing cell output til we get "+SIND: 11"
  Serial.println(F("Module Registered"));
  */
  
  delay(5000);

#ifdef LCDD
  lcd.clear();
  lcd.home();
#endif

  blinkLed(greenGPRS, 1, 100);
  blinkLed(redGPS, 1, 100);
  blinkLed(blueGPS,1,100);
  
  
}

I also observed that if I removed uart_gps.begin()

and I make this change (replacing uart_gps by Serial)

 //while(uart_gps.available())
    while(Serial.available())
    {
      //byte c = uart_gps.read();
      byte c = Serial.read();
      // New valid NMEA data available
      if(gps.encode(c)) 
      {
        newGpsData = true;
      }
    
    }

and then I move the wire to the port 0 and 1, it works fine but if I continue using uart_gps on port 4 and 5

#define GPSRX 4 // Unactive for now, I use Serial instead of uart:gps
#define GPSTX 5 // Unactive for now
SoftwareSerial uart_gps(GPSRX, GPSTX); //Create a fake serial port 0 and 1 // Unactive for now

it deoes not work…

Then I should use Serial instead of a SoftwareSerial initilisation… and I would not prefere…

You haven't shown your whole code, but I guess the uart_gps and cell serial devices are instances of SoftwareSerial. If you read the documentation for SoftwareSerial under 'known limitations' you will see that only one SoftwareSerial can receive at a time.

Hello PeterH Yes it's exactely this. Bothe are initialize with SoftwareSerial. Then wich alternative I have? to use port 4 and 6 for uart_gps? As I mentionned, I can use Serial. and put my 2 wire in port 0 and 1 instead of 4 and 5 and not using SoftwareSerial but the problem, each time I sync my sctech to Arduino I have to remove the wire from port 0 and 1. I am a beginer but once I read it not a good idea to use Hardware port.

So I am interested for an alternative to use port 4 and 5

many thnak

Do you need to receive data on the cell's serial port? If not, just use listen to tell the gps to be the active listener. Alternatively, move the gps.begin to be later than the cell's in setup. If you need to receive on both, you'll need to manage which one is active with listen. If that's not feasible, you'll need to use hardware serial for one of them.

I suspect that your reading about the merits of using the hardware serial port was warning you that it's very useful for debugging and that you're therefore making life more difficult by giving that up to use it for another device. There's nothing inherently wrong with it though.

I suspect that your reading about the merits of using the hardware serial port was warning you that it's very useful for debugging and that you're therefore making life more difficult by giving that up to use it for another device. There's nothing inherently wrong with it though.

Then if I want to make my life easier, I should use hardware serial port. Then port 0 and 1. But annoyed thing, it's when I sync my code throught the USB cable to the Arduino, I got an error message excepted if I removed the two wires from port 0 and 1. Is there a way to avoid removing the wires while synchronizing the code?

Do you need to receive data on the cell's serial port?

Heuuu, I do not know. I wanted to have it separate. "Each of them have their own tunnel". Should I better use the same port for them?

#define GPRSBAUD 9600
#define GPRSRX 2
#define GPRSTX 3

SoftwareSerial cell(GPRSRX,GPRSTX);

If not, just use listen to tell the gps to be the active listener

May I ask you to show me an exemple? I absolutely have no idea how to do it

Thank a lot

Here's the doc for listen, which has an example: http://arduino.cc/en/Reference/SoftwareSerialListen

Great thank, I am going to have a look right now. But what would be your comment reagrding this

Then if I want to make my life easier, I should use hardware serial port. Then port 0 and 1. But annoyed thing, it's when I sync my code throught the USB cable to the Arduino, I got an error message excepted if I removed the two wires from port 0 and 1. Is there a way to avoid removing the wires while synchronizing the code?

Thank a lot

If you need to use the hardware Serial port for uploading then you will need to disconnect any other external circuits to free it up while you do that.

As far as the multiple serial ports go, you need to either design your solution so that you only need to listen to one SoftwareSerial port at a time, or change to an Arduino board that provides multiple hardware serial ports.

Great, PeterH

Thank for your clear answer

Cheers

Hello
It does not work very well on the Harware port… and I will try the listen port.
May be you call help me because I really spend a lot of time and I do not feel me sure.

In resumé
In my setup loop, I have this:

#define LOG_INTERVAL 10000
#define TERMBAUD 4800

#define GPRSBAUD 9600
#define GPRSRX 2
#define GPRSTX 3

//#define LCDD
#define GPRS_ACTIVE
#define GPS_ACTIVE
#define DEBUG

SoftwareSerial cell(GPRSRX,GPRSTX);
void setup(){
 Serial.begin(TERMBAUD);
  //uart_gps.begin(GPSBAUD); //SUPPRIME!
  cell.begin(GPRSBAUD);
  
  pinMode(redGPRS, OUTPUT);
  pinMode(greenGPRS, OUTPUT); 
  pinMode(redGPS, OUTPUT); 
  pinMode(blueGPS, OUTPUT); 
}

In my loop() loop, I have a lot of code and I will past all because my problem should come from the cell module.
I wrote some comment in RED in my loop

void loop(){
   // It will be execute only once, when I switch on my module
  if(firstTimeInLoop) {
    firstTimeInLoop = 0;

[color=red]    // I ACTIVED GRPS_ACTIVE, THEN IT WILL CHECK THE GPRS CONNECTION.
   // WHEN IT?S ACTIVE; IT ALWAYS CRACH WHILE IT TRY TO CELLECT THE FIRST GPS FIX
   // WHEN I UNACTIVE IT, I CAN GET THE GPS FIX. THEN I AM THINKING TO STOP USING THE
   // HARWARE PORT AND USE THE LISTING FUNCTION. BUT I WOULD LIKE TO UNDERSTAND THE REASON WHY IT WORK ONLY
   // WHEN GRPS_ACTIVE IS UNACTIVE[/color]
    #ifdef GPRS_ACTIVE
    
      #ifdef DEBUG
      Serial.println(F("Wait for module is registered (+SIND: 11) and  ready (+SIND: 4) ..."));
      freeRAM();
      #endif
      
      // LOOP UNTIL GPRS IS ACTIVE, WHEN THE DEFINE GRPS_ACTIVE IS ON
      while (GPRS_registered == 0 || GPRS_AT_ready == 0) {    
  
        String ready = getMessage();  
         if(ready == "+SIND: 1"){
         
           Serial.println(F("> SIM is inserted ..."));
           #ifdef LCDD 
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("SIM inserted (1)");
           #endif

           blinkLed(greenGPRS, 1, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1"){
           Serial.println(F("> SIM is ready ..."));
           
           #ifdef LCDD
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("SIM ready (10-1)");
           #endif
           
           blinkLed(greenGPRS, 10, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 11"){
           GPRS_registered = 1;
           Serial.println(F("> Module is registered to the network ..."));
           
           #ifdef LCDD
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("Module is registered to the network (11)");
           #endif
           
           blinkLed(greenGPRS, 11, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 3"){
           Serial.println(F("> GPRS is partially ready ..."));
           #ifdef LCDD
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("GPRS is partially ready (3)");
           #endif;
           blinkLed(greenGPRS, 3, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 4"){
           GPRS_AT_ready = 1;
           Serial.println(F("> GPRS is ready ..."));
           
           #ifdef LCDD
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("GPRS is ready (4)");
           #endif
           
           blinkLed(greenGPRS, 4, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 7"){
           Serial.println(F("> Emergency only ..."));
           
           #ifdef LCDD
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("Emergency only (7)");
           #endif
           
           blinkLed(redGPRS, 7, 100);
         }
         
         if(ready == "+SIND: 0"){
           Serial.println(F("> SIM card removed"));
           
           #ifdef LCDD
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("SIM vard removed (0)");
           #endif
         }
      
       if(ready == "+SIND: 8"){
           Serial.println(F("> GPRS Network Not Available (8)"));
           
           #ifdef LCDD
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("GPRS Network not available (8)");
           #endif
         }    
         
      }

    

      Serial.println(F(" "));
      Serial.println(F("READY TO GO"));
      Serial.println(F("***************************"));
      
      #ifdef LCDD
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("READY TO GO");
        lcd.setCursor(0,1);
        lcd.print("******************");
      #endif
   
    #endif // END ifdef GPRS_ACTIVE
 }  
 
[color=red]// NOW ALL GPRS CHECKS HAVE BEEN DONE. IF I UNACTIVE IT, I CAN GET THE GPS FIX[/color]
 
 #ifdef GPS_ACTIVE
   [color=red]// scheulde IS DEFINE BEFORE AT 10000 (10 SEC). THIS PART WORKS FINE, NO WORRIES[/color]
   if (millis() > scheduler)
   {
      scheduler = millis() + LOG_INTERVAL;

      /* ******* Check if there is fix checkGPS(); ************* */
      [color=red]// HERE IT CHECK  IF A FIX EMNA IS AVAILABLE. [/color]
      #ifdef DEBUG
        Serial.println(F(""));
        Serial.println(F("GPS gathering data..."));
      #endif

      [color=red]// HERE IT WAIT 3 SEC TO CATCH THE FIX[/color]
      for (unsigned long start = millis(); millis() - start < 3000;){
 [color=red]       // AS YOU CAN SEE HERE, I COMMENT THAT LINE IN ORDER TO USE THE HARWARE PORT INSTEAD OF port 4 and 5 AND IT WORKS ONLY IF I UNACTIVE GRPS_ACTIVE[/color]
        //while(uart_gps.available())
        while(Serial.available()){
          //byte c = uart_gps.read();
          byte c = Serial.read();
          // New valid NMEA data available
          if(gps.encode(c)){
            newGpsData = true;
          }
        }
      }  
     
      /* ******* END Check if there is fix checkGPS(); ************* */
[color=red]// IF THERE IS NEW DATA (newGpsData), it continue and call the fonction processGps(), to collect the lat, long, alt, etc. THIS WORKS WELL. I AM I AM SURE, MY PROBLEM COME FROM 4-5 LINE ABOVE
I CAN NOT UNDERSTAND WHY I CAN GET FIX WHEN GPRS_ACTIVE is ACTIVE (#define GPRS_ACTIVE) AND WHY IT I CAN GPS FIX WHEN ITS UNACTIVE (//#define GPRS_ACTIVE)
[/color]
  
      // If New valid GPS data available
      if (newGpsData)
      {
        #ifdef DEBUG
          Serial.println(F("New GPS fix available."));
          freeRAM();
          #endif
    
          blinkLed(blueGPS, 1, 200);
          // Get position (lat, long, alt...)
          processGPS();

          newGpsData = false;
          
        }else{
        
          #ifdef DEBUG 
            Serial.println(F("No GPS fix available."));   
            freeRAM();     
          #endif
      
          blinkLed(redGPS, 1, 200);
      
        }
    }
  #endif
  
}
// END LOOP

If you have any idea, it will be very nice.

(The follow of my message goes to the next post)

[code](This is the follow of my previous post/reply). Apparently text color does not work inside of co-de

Parralely, I will try to listen a port, but this is a bit anoyed because I will have to remove my display which is useful for debug message. It’s a 16 col 4 row display, then I need 6 port. If I want to use 2 serial port (4 and 5) to listen GPS, I will have to take two of the display. But any way…

Then I have question regarding myport.listing()
I will modify my code as following. Could let me know, if it is correct, to listen 2 port. The change are in green

setup()

#define LOG_INTERVAL 10000
#define TERMBAUD 4800
[color=green]#define GPSBAUD 4800[/color]
#define GPRSBAUD 9600

//#define LCDD
#define GPRS_ACTIVE
#define GPS_ACTIVE
#define DEBUG

#define GPRSRX 2
#define GPRSTX 3
SoftwareSerial cell(GPRSRX,GPRSTX);


[color=green]
define GPSRX 4 
define GPSTX 5 
SoftwareSerial uart_gps(GPSRX, GPSTX); //Create a fake serial port 0 and 1[/color]


void setup(){
 Serial.begin(TERMBAUD);
[color=green]  uart_gps.begin(GPSBAUD);[/color]
  cell.begin(GPRSBAUD);
  
  pinMode(redGPRS, OUTPUT);
  pinMode(greenGPRS, OUTPUT); 
  pinMode(redGPS, OUTPUT); 
  pinMode(blueGPS, OUTPUT); 
}

And now I have to add the cell.listen() and uart_gps.listen() and I am not sure about it.
Could you correct me? (I remove the lcd display to make the lecture easier)

void loop(){
   // It will be execute only once, when I switch on my module
  if(firstTimeInLoop) {
    firstTimeInLoop = 0;

[color=red]    // I ACTIVED GRPS_ACTIVE, THEN IT WILL CHECK THE GPRS CONNECTION.
   // WHEN IT?S ACTIVE; IT ALWAYS CRACH WHILE IT TRY TO CELLECT THE FIRST GPS FIX
   // WHEN I UNACTIVE IT, I CAN GET THE GPS FIX. THEN I AM THINKING TO STOP USING THE
   // HARWARE PORT AND USE THE LISTING FUNCTION. BUT I WOULD LIKE TO UNDERSTAND THE REASON WHY IT WORK ONLY
   // WHEN GRPS_ACTIVE IS UNACTIVE[/color]
    #ifdef GPRS_ACTIVE
    
      #ifdef DEBUG
      Serial.println(F("Wait for module is registered (+SIND: 11) and  ready (+SIND: 4) ..."));
      freeRAM();
      #endif
      
      // LOOP UNTIL GPRS IS ACTIVE, WHEN THE DEFINE GRPS_ACTIVE IS ON
      while (GPRS_registered == 0 || GPRS_AT_ready == 0) {    
  
        String ready = getMessage();  
         if(ready == "+SIND: 1"){
         
           Serial.println(F("> SIM is inserted ..."));

           blinkLed(greenGPRS, 1, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1"){
           Serial.println(F("> SIM is ready ..."));
           
           blinkLed(greenGPRS, 10, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 11"){
           GPRS_registered = 1;
           Serial.println(F("> Module is registered to the network ..."));
           
           blinkLed(greenGPRS, 11, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 3"){
           Serial.println(F("> GPRS is partially ready ..."));
        
           blinkLed(greenGPRS, 3, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 4"){
           GPRS_AT_ready = 1;
           Serial.println(F("> GPRS is ready ..."));
           
           blinkLed(greenGPRS, 4, 100);
           delay(1000);
         }
         
         if(ready == "+SIND: 7"){
           Serial.println(F("> Emergency only ..."));
           
           blinkLed(redGPRS, 7, 100);
         }
         
         if(ready == "+SIND: 0"){
           Serial.println(F("> SIM card removed"));
 
         }
      
       if(ready == "+SIND: 8"){
           Serial.println(F("> GPRS Network Not Available (8)"));
           
         }    
         
      }

    

      Serial.println(F(" "));
      Serial.println(F("READY TO GO"));
      Serial.println(F("***************************"));
   
    #endif // END ifdef GPRS_ACTIVE
 }  
 
[color=red]// NOW ALL GPRS CHECKS HAVE BEEN DONE. IF I UNACTIVE IT, I CAN GET THE GPS FIX[/color]
 
 #ifdef GPS_ACTIVE
   [color=red]// scheulde IS DEFINE BEFORE AT 10000 (10 SEC). THIS PART WORKS FINE, NO WORRIES[/color]
   if (millis() > scheduler)
   {
      scheduler = millis() + LOG_INTERVAL;

      /* ******* Check if there is fix checkGPS(); ************* */
      [color=red]// HERE IT CHECK  IF A FIX EMNA IS AVAILABLE. [/color]
      #ifdef DEBUG
        Serial.println(F(""));
        Serial.println(F("GPS gathering data..."));
      #endif

      [color=red]// HERE IT WAIT 3 SEC TO CATCH THE FIX[/color]
      for (unsigned long start = millis(); millis() - start < 3000;){
 [color=red]       // AS YOU CAN SEE HERE, I COMMENT THAT LINE IN ORDER TO USE THE HARWARE PORT INSTEAD OF port 4 and 5 AND IT WORKS ONLY IF I UNACTIVE GRPS_ACTIVE[/color]
        
        uart_gps.listen();[color=green]//NOW ACTIVE THE LISTEN ON PORT uart_gps[/color]
        while(uart_gps.available()){
          byte c = uart_gps.read();
          // New valid NMEA data available
          if(gps.encode(c)){
            newGpsData = true;
          }
        }
      }  
     [color=green]//AS WE ALL WE NEED, COLLECTION IN THE gps VARIABLE (gps.encode(c)), I LISTNEN BACK TO CELL PORT[/color]
      cell.listen();

[color=green]// TAHT ALL. IT'S CORRECT???[/color]
      /* ******* END Check if there is fix checkGPS(); ************* */
  
      // If New valid GPS data available
      if (newGpsData)
      {
        #ifdef DEBUG
          Serial.println(F("New GPS fix available."));
          freeRAM();
          #endif
    
          blinkLed(blueGPS, 1, 200);
          // Get position (lat, long, alt...)
          processGPS();

          newGpsData = false;
          
        }else{
        
          #ifdef DEBUG 
            Serial.println(F("No GPS fix available."));   
            freeRAM();     
          #endif
      
          blinkLed(redGPS, 1, 200);
      
        }
    }
  #endif
  
}
// END LOOP

DO MY COMMENTS ARE CORRECT?

Many thanks for your great help.
and I hope I was not to long and clear

Cheers
[/code]