Serial buffer question

Hi

I am working on a project using an Arduino Mega as a master node in a network of Uno slaves. They communicate using async serial via an RS485 chip, the MAX1487. I have been getting some strange results with reading serial into the slave devices. One thing that I found was that the Uno serial buffer would get loaded up with bytes with mainly zeros but sometimes random values if I touched my finger around the area of any Serial pins on the Atmega chip.

I started trying different things to debug it, to try and work out if the overall problem was hardware or software and while I guess the above problem is pretty much something hardware related, I also found a strange thing happening with my mega.

I set up a temporary connection directly between the TX pin on the Uno and RX2 of the mega. Strange things were happening, like lots of inconsistent results, sometimes it would print out the data as expected, sometime not, sometimes it would print double.

Then I started looking into the mega and reduced back some code to the following and can't figure out what is happening.

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {

  if(Serial1.available()>0){
    int val = Serial1.available();
    Serial.println(val);
  }

}

When I run this code on my Mega, with nothing attached, it counts incrementally from 0 up to 63.

And if I make val equal Serial.read() is prints out a continuous stream of zeros. Both of these happen on Serial2 and Serial3 as well.

Like I said, for this test I have nothing else connected to the arduino or any of the pins. I have an AVRISP mk2 programmer that I have tried using to no effect either.

Why is the buffer continuously being loaded up with bytes?

Also if anyone has any ideas about the unstable serial pins (presumably something capacitance related?) they'd be much appreciated.

Thanks

Laurence

Maybe the problem is that nothing is connected to the pins and they are floating and producing spurious values. Put a resistor (perhaps 4k7) between the Rx pin and 5V and see what happens. Note that the Serial system normally idles HIGH.

You may find the examples in Serial Input Basics useful. They are simple reliable ways to receive data.

Have you a GND connection between your Mega and Uno ? (It is essential).

...R

Thanks for the reply

All the slaves and master are connected with common GND. The Rx pin of the slaves are normally connected to the RO pin on the MAX1487 chip. I can try adding in a pullup to see if that changes things. I will try your receive code too when I get a chance.

Still miffed about the Serial buffer filling up on the Mega though. The code that I posted was running on a standalone Mega, with nothing connected to it.

Thanks

Laurence

For the MAX1487, do you have resistors between the A and B data lines?

Hi

Thanks for the reply. Yeah I have a 120ohm resistor between the outputs of the master MAX1487. I haven't got them on the slaves. For the final set up I have the data travelling along untwisted 4 core cables, with 4pin XLR connectors. They vary in length between 3m and 6m. I know it's not ideal to have untwisted cable, but I have tried directly soldering a twisted pair of similar length between the pins of the master and one slave, bypassing the connectors and only 100mm in length to no discernible difference.

The weird serial buffer thing described above though is something with the arduino board itself that i am miffed about. As I said it happens without any other hardware attached to the bare arduino board. This is only on the Mega board when looking to read data from one port to another. If you directly poll to see the size of the serial buffer on the Uno, it does as expected and stays at 0. I only have one Mega board at the moment so can't see if it's repeatable just yet, but will try when I get another board.

Thanks

Laurence