Arduino Mega with multiple Serial

HI,

There is my code for getting data on RX from Serial1 from a RFID board. I then transmit this same data to a XBEE on Serial TX and RX.

Everything is working well for the first time my RFID board detect a card.

Where the problem is when come the second, third, 4, 5, 6 time…communication with the Xbee fail for 2 sec and I don’t read good data from Serial1 (RFID board)

I tested this program without Serial1 and RFID card and I also tested another program with just Serial1 (RFID) and everything was working fine.

So, I suspect these two loops, Serial.available and Serial1.available from doing naughty things when together…

Or maybe it’s just a variable, and at the second time, it has not been reinitialized…??
Have any clue?

I am working on this error since 3 hours…:frowning:

// Serial variable
int serialCount = 0;        // A count of how many bytes we receive
int serialCount_2 = 0;
int n = 0;
int event = 0;
unsigned char RxByteCount, RxSerialState, ValidMsgRxied, RxError;
unsigned char TxXsum,TxXsum2,RxXsum,RxXSum2;
unsigned char RxBuffer[15],RxBuffer_2[13];
unsigned char TxBuffer[28];
char rx;
int p;
int j;
int k;
//END serial variable



//System status variable
int sys_status;
int led = 13;
double up_time_1, up_time_2, up_time, upv;
//END system status variable





void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(led, OUTPUT);
  
  
}

void loop()
{
  
  
  led_status();
  
 
    
  switch(RxSerialState)
  {
      case 0:                     //  Check for Start Delimiter
        if(RxBuffer[0]== 0x7E){
          RxSerialState=1;
          RxXsum=0x00;
          ValidMsgRxied=0;
          RxError=0;               
        }
        else 
          RxSerialState=0;         
        break;   
    
      case 1:                     // Read Byte Count High               
        RxSerialState=2;
        break;
    
      case 2:                     // Read Byte Count Low   
        RxByteCount = RxBuffer[2];         
        if ( (RxByteCount < 30) || (RxByteCount > 0) )
          RxSerialState=3;
        else
          RxSerialState=0;
        break;
    
      case 3:                     // Read API ID
        RxXsum = RxXsum + RxBuffer[3];
        p=4;   
        RxByteCount--;
        if ( RxBuffer[3] == 0x81){           // API Type=0x81 sur XBEE;
           RxSerialState=4;
        }
        else
           RxSerialState=0;
        break;
    
      case 4:                     // Read Data
        RxXsum=RxXsum+RxBuffer[p];
        p++;
        if (RxByteCount == 0)
        {
          RxSerialState=5;
          RxXsum=255-RxXsum;
        }
        RxByteCount--;
        break;
    
      case 5:                     // Read XSUM
        //if ( (RxXsum==RxBuffer[p]) && (RxByteCount == 0) )
        //{
        ValidMsgRxied=1;
        RxSerialState=0;
        //}
        //else 
        //   RxError=1;
        break;
      }   // end switch





  if(ValidMsgRxied == 1){
      
            
        TxBuffer[0] = 0x7E;
        
        TxBuffer[2] = 0x18;
        TxBuffer[3] = 0x01;
        TxBuffer[4] = 0x00;
        TxBuffer[5] = 0x00;
        TxBuffer[6] = 0x01;
        TxBuffer[7] = 0x01;
        TxBuffer[8] = 0x22;//START
        TxBuffer[9] = RxBuffer_2[0];
        TxBuffer[10] = RxBuffer_2[1];
        TxBuffer[11] = RxBuffer_2[2];
        TxBuffer[12] = RxBuffer_2[3];
        TxBuffer[13] = RxBuffer_2[4];
        TxBuffer[14] = RxBuffer_2[5];
        TxBuffer[15] = RxBuffer_2[6];
        TxBuffer[16] = RxBuffer_2[7];
        TxBuffer[17] = RxBuffer_2[8];
        TxBuffer[18] = RxBuffer_2[9];
      
        TxBuffer[19] = RxBuffer_2[10];
        
        TxBuffer[20] = RxBuffer_2[11];
        TxBuffer[21] = RxBuffer_2[12];
        TxBuffer[22] = 0x00;
        TxBuffer[23] = 0x00;
        TxBuffer[24] = 0x00;
        TxBuffer[25] = 0x00;
        TxBuffer[26] = 0x00;
          
          
        TxXsum2 = 0;
        for(p=3; p <= 26; p++){
           TxXsum2 += TxBuffer[p];
        }
        TxXsum = 0xFF - TxXsum2;
        
        TxBuffer[27] = TxXsum;
    
        Serial.write(TxBuffer,sizeof(TxBuffer));
        
        ValidMsgRxied=0;
        RxBuffer[0] = 0x00;
        
    
        for(k=0; k <= 19; k++){ //Clear le RxBuffer
          RxBuffer_2[k] = 0x00;
        }
        for(k=0; k <= 26; k++){ //Clear le TxBuffer
          TxBuffer[k] = 0x00;
        }
        
    //}//END event = 1
         
               
  }//END vail message received
}//END LOOP


void serialEvent1() {
  
  if(Serial1.available()>=13) {
    
    event = 1;

    for(j=0; j <= 12; j++){
     
      RxBuffer_2[j] = Serial1.read();
     
    }//END FOR
  }//END IF 
}//END function 


void serialEvent() {
  
  if(Serial.available()>=15) {
    
    for(int n=0; n <= 14; n++){
      
        RxBuffer[n] = Serial.read();
        serialCount++;
     
    } 
  } 
}

void led_status(){

  sys_status++;
   

  if (sys_status <=15000){
    digitalWrite(led, LOW);
  }
  if (sys_status >15000){
    digitalWrite(led, HIGH);
  }
  if (sys_status >=30000){
    sys_status = 0;
    digitalWrite(led, LOW);
  }


}

Hope this one will not dissapear…

There is my code for getting data on RX from Serial1 from a RFID board. I then transmit this same data to a XBEE on Serial1 TX and RX.

Why are you (trying to) using one Serial port for two different purposes?

  switch(RxSerialState)

RxSerialState is not even defined. Fail.

      case 1:                     // Read Byte Count High               
        RxSerialState=2;
        break;

What? What this code does and what the comment says are no where near the same.

    serialCount = 0 ;

    for(int n=0; n <= 14; n++){
      
      //digitalWrite(led, HIGH);
      RxBuffer[n] = Serial.read();
      serialCount++;
      //digitalWrite(led, LOW);
      

    }

What is serialCount doing?

PaulS:

There is my code for getting data on RX from Serial1 from a RFID board. I then transmit this same data to a XBEE on Serial1 TX and RX.

Why are you (trying to) using one Serial port for two different purposes?

  switch(RxSerialState)

RxSerialState is not even defined. Fail.

      case 1:                     // Read Byte Count High               

RxSerialState=2;
        break;



What? What this code does and what the comment says are no where near the same.

serialCount = 0 ;

for(int n=0; n <= 14; n++){
     
      //digitalWrite(led, HIGH);
      RxBuffer[n] = Serial.read();
      serialCount++;
      //digitalWrite(led, LOW);

}



What is serialCount doing?

1- Sorry for the typing error, its Serial1 and Serial
2- I didnt put the vaible list, now I do
3- Before it was doing something, now I dont need it, so it just step over, I do nothing in case 1
4- Serial Count was just for debugging

Everything you mention is working properly,

The only problem is I the 2 loops of Serial.available and Serial1.available.

Dont I need to put some restriction from one to another? On the second time I read Serial1, everything fail…

So why the first time everything work well and everyother time it doesnt?

This is the first time I play with multiple Serial port and this give me lot o trouble…

Do you know that serialEvent() is even called? What about serialEvent1?

Take the XBee shield off, and put it away. Use Serial and a cable to debug the reading of the RFID data.

When you know that that works, use Andrew Rapp's XBee library to format the packets. NULL terminate the packets properly, and use strcat() to copy them.

Your code is very complicated, and there are no comments to explain what it is supposed to be doing. It makes some assumptions that there is serial, and serial1, data to deal with, when there may not be.