Go Down

Topic: Proove a datastream of Length (Read 309 times) previous topic - next topic

ref110

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!

PaulS

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: [Select]
unsigned long startedWaiting = millis();
while (Serial.available() <= 12 && millis() - startedWaiting < 1000)
{
  // Spin our wheels doing nothing for up to one second
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy