Go Down

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

TomLeMort

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:

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


TomLeMort

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.

AWOL

Can you explain this sentence, and how you tested this hypothesis?
Quote
I'm pretty sure the problem comes from the reading as I tried to send a whole string and I received it perfectly.

TomLeMort

Because if I do something like this:
Code: [Select]
Serial.println("test");
I can see "test".

AWOL

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

TomLeMort

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.


AWOL

Quote
Which board do you have?

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

davekw7x

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


Regards,

Dave

TomLeMort

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?

TomLeMort

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.

davekw7x

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


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.

TomLeMort

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.

TomLeMort

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?

bilbo

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

Go Up