Using Multiple Serials Freezes Mega2560

Hi together,

I'm working with a custom 2560 board using multiple serial ports to communicate with 2 PCs (one for debugging only) and a Leonardo at 9600 baud.

My problem is that the 2560 freezes when I use multiple serials at once a lot. It then stops reacting to any means of communication until it is reseted. After I took the debugging PC out of the equation by disabling that serial connection and programmed the other PC to wait for an "ok" message after it sent a message before it sends another, I pretty much had this problem fixed, but sometimes it still happens.

Here are some questions I was asking myself. Any help would be appreciated!

Is this a known problem? Could it be that the Serial library is based on interrupts and that the chip gets stuck in one of those interrupt routines when receiving messages on multiple ports at once? I am using the TimerOne library. Could that interfere with above business? Would it make sense to close the other serial ports while communicating on one?

Thanks for listening! ff

Is this a known problem?

No.

Could it be that the Serial library is based on interrupts

Yes.

and that the chip gets stuck in one of those interrupt routines when receiving messages on multiple ports at once?

No. During an ISR, other interrupts are disabled.

I am using the TimerOne library. Could that interfere with above business?

Depends on how you are using it, but, it seems unlikely.

Would it make sense to close the other serial ports while communicating on one?

Not to me.

Where is your code? This is the Programming section, where the requirement is that you post your code.

Sorry, the code is more than 1000 lines long and the forum only allows for 9500 characters max :(

amphibious: Sorry, the code is more than 1000 lines long and the forum only allows for 9500 characters max :(

Can you simplify this to the minimal sketch capable of demonstrating the problem? This may well surface false assumptions or misunderstandings about what's going on and may enable you to solove the problem for yourself, but if not then at least it's reduced the amount of irrelevant code we need to wade through. But one way of another you need to post a complete sketch that demonstrates the problem.

ok, I’ll try :

const int buffer_max = 16;

struct buffer { char Input[6][buffer_max];
				int bufpos;
				int strngnum;
				};
				
buffer Serial0Buf = {0}; // debug
buffer Serial1Buf = {0}; // PC
buffer Serial3Buf = {0}; // cl1






void loop(){

check_pcStatus();
/////////////////////  check Buttons  
check_StandbyButton(); 
/////////////////////  check Flags
  for (int i = 0; i<=(Number_of_Clients); i++){
	for (int u = 0; u<=(MaxParams - 1); u++){
		if (bitRead(Flags[i], u) != 0){
		  if (u < 23){
		  bitClear (Flags[i], u);
		  }
		  execute_command (u, i);
		  
		}
	}
  }

/////////////////////////////////////////////////
//buffer Serial1
while (Serial1.available() > 0){
  	if (Serial1.available() > 30){ // prevent overflow
  	while (Serial1.available() > 0){}
  	Serial1.println("too much information!");
  	}
	byte Serial1Byte = Serial1.read();
	Serial1Parser(Serial1Byte);
	}
  
while (Serial3.available() > 0){
  	if (Serial3.available() > 30){ // prevent overflow
  		while (Serial3.available() > 0){}
  	  	}
	byte Serial3Byte = Serial3.read();
	Serial3Parser(Serial3Byte);
	}

}


void Serial1Parser (byte inByte){
switch(inByte){
	
	case '\n':									 
	Serial1Buf.Input[Serial1Buf.strngnum][Serial1Buf.bufpos] = '\0';
	process_incoming(Serial1Buf.Input);				  
	Serial1Buf.bufpos = 0;
	Serial1Buf.strngnum = 0;
	//Serial.println("end of line");
	break;
	 
	case '\r':
	break;
	
	case ';':	 // our divider, should be space or such
	Serial1Buf.Input[Serial1Buf.strngnum][Serial1Buf.bufpos] = '\0';
	Serial1Buf.strngnum++;
	Serial1Buf.bufpos = 0;
	break;
  
	default:  
	  
	  if (Serial1Buf.bufpos <= (buffer_max - 1)){
		  Serial1Buf.Input[Serial1Buf.strngnum][Serial1Buf.bufpos] = inByte;
		  Serial1Buf.bufpos++;		  
		}
		else if (Serial1Buf.bufpos > (buffer_max)){
		  Serial1.println(Serial1Buf.bufpos);
		  Serial1.println("buffer overflow");
		}
	break;
	}
return;	
}

Possible bug 1

  for (int i = 0; i<=(Number_of_Clients); i++){

Do you really mean <= or should it be < . If I have 10 clients and there numbered from 0 then its < not <=.

Possible bug 2

	for (int u = 0; u<=(MaxParams - 1); u++){

As above.

Tip Use the auto format (tools menu on IDE) on your code and then recheck your logic. I think you have an endless loop.

Mark

Thanks so far!

I'll have a look at it as soon as possible. Grz ff