Go Down

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

billroy

Um, what happened to checking for the end of line?

If you're simply going to thrash around, without trying harder to understand what's going on, you'll get nowhere.  Maybe a little break would help.

One more time.

You need to capture characters up to the end of line.

To capture each character, you must be sure it is there first.


-br


bluefix

#11
Jan 05, 2013, 02:32 pm Last Edit: Jan 05, 2013, 02:37 pm by bluefix Reason: 1
I´m sorry, but i´m really desperate, because i´m sitting on this problem for a week...

Ok.
What is the character for an end of the line, if it isn´t '\n'?

Code: [Select]

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

void loop()
{
  if (Serial.available())
  {
    char SI = Serial.read();
    if (SI == 'W')
    {
      String Input;
      char c = Serial.read();
      while (Serial.available() && c != [end of line character])
      {
        Input = Input + c;
        if (Serial.available)
        {
          c = Serial.read();
        }
      }
      Serial.println(Input);
    }
  }
}


Would it be now correct?

P.S. And Yes a coffee would be the right now ;-)

wildbill

Your problem is that serial communications are very slow compared to the Arduino's ability to process them, especially at 9600 baud. As I said earlier, you cannot depend on the string you are sending all arriving at the same time. I believe that the only reason your original version was close to working was because other parts of your code slowed things down so that the data you sent filled the read buffer and the rest was thrown away. Given that it was then sitting on the arduino ready to be read, you could use your while loop to pull the data without fear that nothing would be there.

Now that does not apply. Before you read each character, you must be sure that one is available. Get rid of your while loop. In loop, if a character is available, read it and then check what it is. If it was 'W', set your collection string to "". If it was '\n' print the string and set it to "". any other character, add it to the string.

That should do it. A switch would be handy for replacing the ifs. Then get rid of String and use char arrays instead. Then add some logic that ignores characters until you see 'W'. Get the absolute minimum working first though.


bluefix

You´re right. The rest of my code (about 500 rows) is very long.
One loop() last about 200ms. But I´ve got an idea how to fix the buffer problem.

Thank you all!
(And sorry for my stupidity  ;) )

PeterH


I´ve got an idea how to fix the buffer problem.


I hope your idea is very very similar to the approach suggested by wildbill.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up