Serial.read working in Arduino 16 but not in 17

I have this function that works okay in Ardunio 16 but when recompiled with Arduino 17 after a few chars returns garbage chars. It is reading from an XBee at 57600 baud connected to pins 0 and 1. If you send a few chars at a time is appears to work - but if you send a string of 5-20 it fails.

char readSerialChar() {
  int i =0; 
  char chrx; 
  while (i++ < 500) {            
    if(Serial.available()) {    
       chrx = Serial.read();
       Serial.print(chrx, BYTE);
       return chrx;
    }
  }      
}

The difference between 0016 and 0017 is very likely timing or coincidence.

char readSerialChar() {
  int i =0;
  char chrx;
  while (i++ < 500) {            
    if(Serial.available()) {    
       chrx = Serial.read();
       Serial.print(chrx, BYTE);
       return chrx;
    }
  }

It is possible to reach this point in the code. Without a return “garbage” is returned to the caller. What should be returned? Have you defined a “default” character in the rest of your Sketch? Or a “nothing available” character?

}

ah - thanks. Good catch I can try that and fix it to be sure - but the line Serial.print(chrx, BYTE); is actually displaying garbage characters. Sorry - I should have said that in my post rather than "returning". It is both printing and returning the garbage chars.

Can you (easily) test without the XBee radios?

I need to take some things apart - but I wiil try as soon as I can and let you know the results.

The baud rate -> register value calculation changes in Arduino 0017, supposedly to make it more accurate (and seemed to be in my admittedly limited tests). This is probably the source of the problem. The code is in HardwareSerial.cpp.

I am having an identical problem with a board I am developing. I can either plug in a USB to Serial cable or communicate using an X-Bee. The X-Bee module is configured for 57,600 baud.

With version 16 both the USB to Serial and X-Bee work perfectly.

With version 17 the USB to Serial works perfectly but the X-Bee does not. Information received by the X-Bee and sent to the ATmega328 is corrupted if the command is more than a few characters. Information sent from the ATmega328 to the X-Bee is transmitted to the PC with no problems regardless of length.

I have time to work on this if you let me know what you want me to change and/or test.

Thanks.

Try Arduino 0017 with the baud rate calculation formula from Arduino 0016 (in Serial::begin() in HardwareSerial.cpp).

I modified Arduino 0017 to use the Arduino 0016 baud rate register setting code and everything works perfectly.

On the surface it would appear that the 2X mode is the problem but if I pull the X-Bee and plug directly into my board using a USB to Serial cable it works perfectly with the original Arduino 0017 using 2X mode. So the problem is a combination of 2X mode and an X-Bee. It's possible it's the X-Bee and I haven't tried swapping them yet.

I wrote a test program for the baud rate setting code from both Arduino 0016 and 0017. I compared the results to table 19-12 from page 203 of the ATmega48PA/88PA/168PA/328P datasheet and the baud rate register settings are correct. Arduino 0017 will use the 2X mode for baud rates 2400, 28800, 57600, 115200 and 230400.

I will test my board at some of the other baud rates that use 2X mode in the next day or two and report my findings.

Since there are only a couple of people having this problem I'm not sure how much trouble shooting time this problem warrants. My application is not time/speed dependent so I'm planning on switching to a baud rate that does not use 2X mode.

Huh. In some tests I ran, the 0017 code worked where the 0016 didn't. Any other tests you can do would be useful.

Hey JoeD - I am still on Arduino 16 and would like to take advantuage of the nice features of 17 - can you tell me how to use the Arduino 16 baud rate register setting code"? Thanks much.

Arduino 0018 - Serial problems

I have had terrible problems trying a simple loop back test with xbee to xbee via an arduino.

I found an article that suggested a problem in the serial routines of 0017 so I figured the same problem in 0018.

The solution is to replace the Serial.begin() from the HardwareSerial.cpp of 0016

here it is save you hunting for it … and everthing now working for me!!

void HardwareSerial::begin(long speed)
{

_ubrrh = ((F_CPU / 16 + speed / 2) / speed - 1) >> 8;
_ubrrl = ((F_CPU / 16 + speed / 2) / speed - 1);
sbi(
_ucsrb, _rxen);
sbi(
_ucsrb, _txen);
sbi(*_ucsrb, _rxcie);

}

I'm seeing the same behavior in Arduino 0019. Serial i/o is fine at 9600 baud, but I get dropped characters reading at 57600 baud.

The bootloader is working fine, both directly and via XBees set at 57600 baud. Looking at the bootloader source, it uses the old baud rate setting and not the double rate setting.

I'm going to run a spreadsheet on the numbers, but it definitely seems that the new calculation is broken.

I've tested the older "Serial.Begin" in 0019 and it fixes it. Interested to hear your thoughts

When I calculated what baud rate the old settings should actually produce, I came up with 62500. I plugged that back into the Serial.begin(), and that cured the problem.

I don’t know if the XBee’s are actually running at 62500 baud, or if the timing is such that transmitting is more forgiving than receiving. I should haul out the scope and seen if I can get some timings.

BTW, this is quite specific for hardware serial. The NewSoftSerial stuff uses a lookup table, not a formula, and it will only accept a standard baud rate.

The 0018 code works with xbee using the DIY drones xbee adapter but not the Ardafruit adapater. If I recall 0018 attempts a different type of serial connection if it is available. I think the Ardafruit board causes 0018+ to believe that it can use the alternate connection.

I don't have the hardwareSerial code in front of me but I think you might see what I mean when you next look at it.

Interested to know your thoughts and which adapater board you are using for which device (xbee?)