Go Down

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

wildbill

Ah, that's not all your code. If the missing code does time consuming stuff or has, God forbid, delay in it, then depending on when you send the serial data, it may well overflow the buffer. Try running the code you posted - does it work?

bluefix

With This Code:
Code: [Select]

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

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


it returns nothing.

If I use this Code (for Debugging):
Code: [Select]

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


it returns "1-1356834120-20-60/2-1356834140-20-60/3-1356834160-20-60/4-135ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ(...)"

tocpcs

The chip isn't receiving \n from you, so doesn't exit the while loop..

billroy

In the inner loop you are doing Serial.read without first assuring that Serial.available is greater than zero.  That's why it doesn't work, and why you get those y-umlaut characters.  They show up when you read when zero characters are ready.

-br


bluefix

Code: [Select]

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

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



I get Nothing back...  =(

Go Up