UART over multiple Arduinos mysterious problem

Hi,

I am doing some comunication over UART between Nano board and some weird stuff is happening.
basicaly, if i send 2 bytes (the second byte is fixed 0xFF) the second arduino receives and sends back more 2 bytes, and this repeats over and over again.

If i send a variable number of bytes with the last one 0xFF to know when comunication is finished, the comunication stops, and i dont know why..

This works between The Mega board but not with the Nano board.. really weird stuff..

Code:

void serialEvent()
{
    if(principal.available())
    {
        if(principal.isSelfTurn())
        {
            principal.send("Termostato");
        }
    }
}

//Some class functions..
bool DeritecCommunication::available()
{
	while(_serialPort->available())
		return receive();
	return false;
}

bool DeritecCommunication::receive()
{
	//char received = _serialPort->read();
	_data[_dataIndex] = _serialPort->read();
	_dataIndex++;
	
	if(_data[_dataIndex-1] == (byte)0xFF)
	{
		_dataIndex = 0;
		_nextDevice = (int)_data[FROM]+1;
		if(_nextDevice==_totalDevices)
			_nextDevice = 0;
		return true;
	}
	
	return false;
}

bool DeritecCommunication::isSelfTurn()
{
	if(_self == _nextDevice)
		return true;
	return false;
}


void DeritecCommunication::send(String message)
{
	if(_self != _nextDevice)
		return;
	digitalWrite(_port, HIGH);
    _serialPort->write((unsigned short)_self);
   _serialPort->print(message);
	_serialPort->write((byte)0xFF);
    _serialPort->flush();
    digitalWrite(_port, LOW);
}

This is an excerpt of your code and absolutely useless as it is. Post the complete sketch and do that for both sides of the communication.

Just to eliminate one of the most common errors: did you connect the grounds?

This is the only code the program uses, I can post the intire code (+-1000 lines) but this is the essencial. Code is exactly the same both sides.

The problem is not hardware, i can ensure you that.
This works between Mega boards (2560) and not between Nano boards (328p)

Basically i implemented a turn by turn comunication over rs485. There are X Arduinos connected by 485, each Arduino have their own address. The Arduino with address 0x00 starts comunicate, after that is the turn of 0x01 and so one..
The arduino with address 0x00 knows how many Arduinos are in the table to know when is his turn.

Post the complete sketch, I can tell you almost nothing about this short excerpt, I even cannot see the variable declarations and the like. If you're able to get the same problem with a shortened sketch we're happy to look over less lines of code but in at least 1 out of 4 cases the error was not in the posted excerpt in the past.
As you're using the String class I would bet on a memory problem (you do know that the ATmega328 has a quarter of the memory of the ATmega2560?).

Hi, i figure out the problem, but not the cause.

Seems like when i working with the 328p, when i send some data via 485, "somehow" he receives also the same thing, what is impossible because with the 485 i receive or send, never at the same time.
With 2560 this problem never occurs, and it have the same code.
It is not a problem with memory i can ensure you.

The solution: After send some data i read all the RX buffer to clean it.