Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No need to wait after Wire.requestFrom() #1

Open
Koepel opened this issue Aug 15, 2017 · 2 comments
Open

No need to wait after Wire.requestFrom() #1

Koepel opened this issue Aug 15, 2017 · 2 comments

Comments

@Koepel
Copy link

Koepel commented Aug 15, 2017

After the Wire.requestFrom() there is no need to wait for something and the timeout with millis() is not needed.

When the Wire.requestFrom() returns, the I2C transaction has completely finished and the received data is waiting in a buffer in the Wire library.

After the Wire.requestFrom() you may call Wire.available() to test if the same number of bytes was received as was requested.

@Risele
Copy link
Owner

Risele commented Sep 6, 2017

You mean, that I could change
while ((Wire.available()<6) && ((millis() - WaitingBeginTime) < _TimeoutMillisec))
{
//Do nothing, just wait
}
if ((millis() - WaitingBeginTime) < _TimeoutMillisec)
{
//....
}
//...

just for
if (Wire.available()<6)
{
//....
}
?

@Koepel
Copy link
Author

Koepel commented Sep 6, 2017

Yes, I think so.

To me, it makes more sense to test for 6 bytes. If 6 bytes are requested, and 6 bytes are received, then everything is okay. You have to fill in the "..." and the "???".

  Wire.requestFrom(_Address, (uint8_t)6);
  if (Wire.available() == 6)
  {
    uint8_t data[6];
    for (uint8_t i = 0; i<6; i++)
    {
      data[i] = Wire.read();
    }
    if ((CRC8(data[0],data[1],data[2])) && (CRC8(data[3],data[4],data[5])))
    {
      ...
    }
  }
  else
  {
    _Error = ???;         // an error on the I2C bus
  }

The situation that Wire.available() does not return 6 bytes is very rare. It could be a problem on the I2C bus, or it could be a collision in a multi-master I2C bus. However, many problems on the bus are not detected and with some problems the Wire library might crash-halt and stop the sketch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants