Pages: [1]   Go Down
Author Topic: Proove a datastream of Length  (Read 230 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I´ve got the following problem:
I need to receive a char with 13 signs. I realised that way:

char Einlesen [13];
...
 while (Serial.available() <= 12) {
  }
  Serial.readBytes(Einlesen, 13) ;
...


Now there is the problem, that there can be a problem with the transmisson. Somtimes the Arduino receives more or less signs than the 13 needed signs.

When the Arduino receives less than 13 signs, it stays in the while loop. If there are more than 13 signs, the Arduino works with the first 13 signs and stores the other signs. When the next data arrives, the Arduino puts the old signs before the new signs, so the datastream is also incorrect.

I don´t really know how to realise this.
The Arduino should only work with the received signs if the lenght is 13 otherwise the Arduino should delete the received data and wait for the next datatransmission.

So I hope you can help me!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I need to receive a char with 13 signs.
A string with 13 characters, you mean?

From what? Can you make that device smarter, so that it sends an end of packet marker?

Quote
When the Arduino receives less than 13 signs, it stays in the while loop.
You could add a timeout, so it doesn't wait forever:
Code:
unsigned long startedWaiting = millis();
while (Serial.available() <= 12 && millis() - startedWaiting < 1000)
{
  // Spin our wheels doing nothing for up to one second
}
Logged

Pages: [1]   Go Up
Jump to: