Go Down

Topic: Problem while reading several bytes on serial (Read 4732 times) previous topic - next topic



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:

Code: [Select]
void setup() {

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);

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:
Code: [Select]

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


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:
Code: [Select]
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.


Apr 06, 2011, 10:46 pm Last Edit: Apr 06, 2011, 10:58 pm by davekw7x Reason: 1

...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).

Quote from: 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.)




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.


Apr 06, 2011, 11:26 pm Last Edit: Apr 06, 2011, 11:32 pm by davekw7x Reason: 1

...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...



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:


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.

Go Up