Problem while reading several bytes on serial

Hi,

I am experiencing problems when trying to send and receive several bytes of data using the serial port. I’m pretty sure the problem comes from the reading as I tried to send a whole string and I received it perfectly. It seems like I can receive the first byte, but the others are filled with junk.

Here is my code:

void setup() {
  Serial.begin(115200);
}

byte buffer[128];

void loop() {
  if (Serial.available())
  {
    int i = 0;
    while (Serial.available())
    {
      buffer[i++] = Serial.read();
    }
    for (int j = 0 ; j < i ; j++)
    {
      Serial.print(buffer[j], HEX);
      Serial.print(':');
    }
    Serial.println("");
  }
  delay(10);
}

I send what I receive in HEX to figure out exactly what was received.

Here is what I receive when sending A, then AA, then AAA, then AAAA, etc:

41:
41:D0:
41:50:FF:
41:50:5:
41:50:5:41:
41:50:5:41:D0:
C1:50:5:41:50:FF:
41:50:5:41:50:5:
41:50:5:41:50:5:41:
41:50:5:41:50:5:41:D0:

I should receive only 41s, but I don’t even get always the correct number of bytes. There is definitely a pattern
Of course I checked the baud rate, which is correct. I tried with the serial monitor, Putty and my program written in C++, which is supposed to communicate with my Arduino (Lilypad) at the end.

Anyone have an hint?

Thank you

What is sending the data?

For these tests I use the serial monitor from the Arduino software. However the issue is the same if I try with PuTTy or my own software.

Can you explain this sentence, and how you tested this hypothesis?

I'm pretty sure the problem comes from the reading as I tried to send a whole string and I received it perfectly.

Because if I do something like this:

Serial.println("test");

I can see “test”.

Your code seems to work perfectly on a 2009, with 0021 IDE. Just sent forty 'A's via the serial monitor at 115200 baud.

This is the problem... If it works "sometimes", there must be a problem.

Which board do you have?

PS : I tried with the FTDI or with the cable. And it does not work with a Bluetooth Gold Mate neither.

Which board do you have?

Via USB to a Toshiba Satellite laptop.
Tried multiple times, worked correctly every time.

TomLeMort: ...Arduino (Lilypad) at the end....

Two questions:

  1. What is the operating voltage for your lilypad? 5 Volts? Or what?

  2. Have you tried lower bit rates? Maybe 9600 or some such thing? I don't think there is a fine enough granularity in the ATmega bit rate generator to communicate reliably at 115200 baud with an 8 MHz Arduino Clock. Receives one ASCII byte at a time OK (but just barely), but if there are two (or more) bytes in a row they tend to step on each other's toes. (I think).

TomLeMort: It seems like I can receive the first byte, but the others are filled with junk.

It is quite possible that it can affect one direction more than the other. (I think.)

Regards,

Dave

When I use the FTDI cable it is 5V. When I use the bluetooth mate, it is 3.7V.

OK seems to work up to 57600 bauds. However my application is time-critical. Is there a way to make it work at 115200 bauds?

By the way it doesn’t work with the bluetooth mate, either at 57600 or 115200. I’m still able to get correctly the first byte, receive 7 bytes instead of 8, etc.
In the past I didn’t have this problem since I only read one byte in the loop.

TomLeMort: ...a way to make it work at 115200 bauds?

Increase clock frequency and recompile bootloader. I haven't done the math lately, but it's only a little off with an 8 MHz clock. I'm thinking a 12 MHz clock would get the bit rate close enough to 115200 to be effective. (We all know that 16 MHz would work at 115200 MHz, but then 3.3 Volt operation would be out of spec. 12 MHz at 3.3 Volts is OK, I think.)

[/begin Disclaimer] I don't presently have a lilypad or any kind of bluetooth interface, so I can't confirm or test any of my thoughts on this particular subject just now. [/end Disclaimer]

Bottom line: This is about as far as I can go with specific (or non-specific) recommendations. (However...See Footnote.)

Maybe someone else...

Regards,

Dave

Footnote: For test purposes can you adjust whatever it is that is sending to your lilypad so that it sends two stop bits rather that one? Might not be a long-term solution, but could give another data point for a more complete analysis of the problem.

OK, I will check that. Do you have a good documentation about that? I never changed the bootloader, and I didn't know I can change the clock.

Looks like this:

http://arduino.cc/forum/index.php?topic=54623.0

It seems to be the solution, however I have no idea on how to do it. I don't even know what to search for. Any hint please?

Try adjusting the baud rate to 111 on the pc end to see if that has any affect.

I will try all this tomorrow at work. Besides, according to the specifications of the bluetooth gold mate, this is the default settings: • Baud rate 115,200 • 8 bits • No Parity • 1 stop bit • Hardware flow control enabled

http://www.sparkfun.com/datasheets/Wireless/Bluetooth/rn-bluetooth-um.pdf

This is why lower baud rates don't work with the bluetooth. It seems that there is no parity, and 1 stop bit. Nothiong special. Maybe the hardware flow control is a problem. In any case I have to find out how to change the Lilypad's clock to 12MHz if I want to use 115200. The alternative would be to set the bluetooth mate to 57600 bauds.

I changed the baud rate of the Bluetooth mate to 57600 bauds using a serial cable and it seems to work. I just hope it is sufficient.