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:
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.
What is the operating voltage for your lilypad? 5 Volts? Or what?
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.)
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.
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
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.