-->
Page 1 of 1

I2C - Wire.requestFrom()

PostPosted: Tue Sep 06, 2016 2:25 pm
by Ruandv
I am would like to understand why is my array displaying 3 (funny) characters as shown in the images attached.

I successfully get the data from the slave. Then directly after the loop i print out the data and then i get funny characters at the end. Can some one explain to me what these characters are and why are they random values?

this is the code i use to read the data that the slave return.

Code: Select allvoid requestData(){
  Serial.println("Request Returning");
  //clean the array
  for(int i =0;i<32;i++)
  {
     data[i]=' ';
  }
  delay(10);
  if (Wire.requestFrom(slaveId,25))
  {
    int i =0;
    while (Wire.available())
    {
      char c;
      c=Wire.read();
      if(c>0x1F && c<0x7F)
      {
        Serial.println(c);
        data[i] =c;
      }
      i++;
    }
    Serial.println(data); //This is the line that i am getting the funny characters on. See image link below
  }
  else
  {
    Serial.println("Unable to");
    Serial.println("Retrieve the data requested"); 
  }
}

Image

Re: I2C - Wire.requestFrom()

PostPosted: Tue Sep 06, 2016 4:40 pm
by Barnabybear
Hi, you set data[i] as 32 bytes
Code: Select all  //clean the array
  for(int i =0;i<32;i++)
  {
     data[i]=' ';
  }

- See more at: http://www.esp8266.com/posting.php?mode=reply&f=8&t=11669#sthash.bdOohjRV.dpuf

then you read 25 bytes to c and say data[i] is equal to c
Code: Select all    while (Wire.available())
    {
      char c;
      c=Wire.read();
      if(c>0x1F && c<0x7F)
      {
        Serial.println(c);
        data[i] =c;
      }
      i++;
    }

 - See more at: http://www.esp8266.com/posting.php?mode=reply&f=8&t=11669#sthash.bdOohjRV.dpuf

Lastly you say
Code: Select all        Serial.println(data);

 - See more at: http://www.esp8266.com/posting.php?mode=reply&f=8&t=11669#sthash.bdOohjRV.dpuf

which is
Code: Select all     data[i]

- See more at: http://www.esp8266.com/posting.php?mode=reply&f=8&t=11669#sthash.bdOohjRV.dpuf


Which is 32 bytes with only 25 set but you so the rest is junk.

Sorry for the
Code: Select all- See more at: http://www.esp8266.com/posting.php?mode=reply&f=8&t=11669#sthash.bdOohjRV.dpuf

but the site owners think its usefull to ad this to everything you copy from this site - I've given up deleting it.