Go Down

Topic: Problems with Serial Bus (Read 931 times) previous topic - next topic

bluefix

Hello,

If I enter the String "W1-1356834120-20-60/2-1356834140-20-60/3-1356834160-20-60/4-1356834120-20-60/5-1356834140-20-60/6-1356834160-20-60/7-1356834120-20-60/8-1356834140-20-60/" to the Serial Monitor, the Arduino return this: "1-1356834120-20-60/2-1356834140-20-60/3-1356834160-20-60/4-135"

My Code:
Code: [Select]

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

void loop()
{
 if (Serial.available())
 {
   char SI = Serial.read();
   if (SI == 'W')
   {
     String Input;
     while (Serial.available())
     {
       Input = Input + char(Serial.read());
     }
   
     Serial.println(Input);
   }
}


Why the Arduino donĀ“t return "1-1356834120-20-60/2-1356834140-20-60/3-1356834160-20-60/4-1356834120-20-60/5-1356834140-20-60/6-1356834160-20-60/7-1356834120-20-60/8-1356834140-20-60/"?

Thank You,
bluefix

tocpcs

Hmm. How much SRAM does your Arduino have?

bluefix

#2
Jan 05, 2013, 01:36 pm Last Edit: Jan 05, 2013, 01:40 pm by bluefix Reason: 1
Arduino Uno rev. 3

SRAM     2KB


My Code is much longer.

Global Variables:

unsigned long end_of_ch[38];
int Channels[8];
boolean IsOpen[8];

wildbill

Your code assumes that once you see a 'W', all the other characters will be delivered back to back. That isn't necessarily so - there may be a time when transmission is in process, but nothing is available to read yet. If that occurs, you'll print what you have at that point and go back to looking for a 'W'. Since there isn't another, no more input will be read.

There's more to it than that I suspect. You're only processing 64 characters, which by a remarkable coincidence is the size of the serial read buffer.

billroy

Concur with wildbill. 

The serial input buffer is 64 characters.  You are only getting the first 64-ish of your 154 characters of input.  So right away we have to think "timing problem with buffer overflow".

The second Serial.available() test seems to have the intention of looking for the end of the line.  But checking Serial.available() is not a reliable way to detect line endings.  

It would be better to loop looking for a line ending character like '\n', and wait on serial available within that loop, if that makes sense.  Accumulate characters to the end of the line, not until random timing gives you serial.available of zero.

-br

Go Up