ESP32 Dev, reading a register of an I2C-Device

Why do you need to convert a c-string to a String object?

I think both your formulations should make test a String object containing the characters of words. Are you having issues?

string does not work for me. I also thought, that String is the pendant to string in Arduino C

I also printed words in ascii and it returns:
0 0 0 0 77 69 76 84 79 78 0 0 77 69 76 84 79 78 0 0 0 0 0 0 0 0
So it could not at all copy the char to String, because it was 0-terminated.

So i wrote a function that delets the 0

void deletenull() {
  boolean found = 0;
  boolean ending = 0;
  for (int i = 0; i < sizeof(words); i ++) {                //walk through the ar
    found = 0;
  Serial.print(words[i]);
    if (words[i] == 0 && ending == 0) {                     //if zero and searching for start
      for (int j = i; j < sizeof(words); j ++) {                    //start at i
        if (words[j] != 0 && found == 0) {                  //and find a char
          words[i] = words[j];                              //move it to i if not done
          words[j] = 0;

          found = 1;
        }
      }
    } else {
      ending = 1;
      if (words[i] == 0) {                                    //searching for the end
        for (int j = i; j < sizeof(words); j ++) {            //if found, delete the rest
          words[j] = 0;
        }
        return;
      }
    }

  }
}

This works in a loop, but with diffrent registers from the ic2 it´s confused. I think something is wrong with the writing into the words array.

Can you expand on that ? What "does not work" ?

for (int i = 0; i < sizeof(words); i ++)

What type of variable is words ?
Have you tried printing what sizeof(words) returns ?

words is a char array and i defiend the size to 10.

Serial.println(sizeof(words))=10

I can write

string test;

end i am getting the error: 'string' does not name a type

I can println(words) and see the word. So it´s working fine.

Let's back up a bit. How did we get from this statement to

string does not work for me.

We went over the String conversion syntax in the previous postings. The String object is capitalized. I don't think you need to convert a c-string (null terminated character array) to a String object .

//string test;
String test;
test=words;

No surprise there because string does indeed not name a type unlike String

How many printable characters is the words array of chars required to hold ?

As i said here:

I think, that the String reads the words until it finds a zero. And stops there. Mayby this is the reason, why it copys nothing? Because if i delete the zeros it works.
Serial.print(words) returns only Melton.
Serial.println(String(words)); returns nothing.
After some time (20 s) words returns MELTONTON or MELTONMELTON and now the string "test" ist filled with Serial.println(test)= TON /Serial.println(test)= MELTON
That´s the reason why i think somthing is wrong with

That´s the reason why i think somthing is wrong with

words[x] = (char)blockBuffer[x];

Yes, try this modification which only adds a character to the words array if it is accepted, and increments the index for words independently from the block buffer reading index. This should get rid of the 0's in the words array which I think happen when the block reading index is incremented but a character is not put into words.

uint8_t i2c_smbus_read_block ( uint8_t command, uint8_t* blockBuffer, uint8_t blockBufferLen ) {
  //uint8_t x, num_bytes;
  uint8_t x = 0; //index for block buffer explicitly set to 0
  uint8_t y = 0; //index for words set explicitly to 0
  uint8_t numBytes = 0;
  Wire.beginTransmission(deviceAddress);
  Wire.write(command);
  Wire.endTransmission(false);
  num_bytes = Wire.requestFrom(deviceAddress, blockBufferLen);
  if (num_bytes == blockBufferLen) // received the same amount as requested ?
  {
    while (Wire.available()) 
    {
      blockBuffer[x] = (uint8_t) Wire.read();
      
      if (blockBuffer[x] >= 65 && blockBuffer[x] <= 90)
      { //if x=upercase Letter, print it
        Serial.print((char)blockBuffer[x]);
        words[y] = (char)blockBuffer[x];//add to array
        words[y + 1] = '\0'; //null terminate
        y++; //increment words index
      }
      else if (blockBuffer[x] >= 97 && blockBuffer[x] <= 122)
      { //if x=lowercase Letter
        
        if (blockBuffer[x - 1] >= 65 && blockBuffer[x - 1] <= 90)
        { //if the last was upper,leave
          return num_bytes;
        }
        
        Serial.print((char)blockBuffer[x]);  //else print it
        words[y] = (char)blockBuffer[x]; //add to array
        words[y + 1] = '\0'; //null terminate
        y++;//increment words index
      }
      else if (blockBuffer[x] == 255) 
      {//stop
        //words[y + 1] = '\0'; //don't add to words
        return num_bytes;
      }
      x++; //increment block buffer reading index
    }
  }
}

words should be a null terminated cstring, and if you need the String object the conversion should be as suggested before-- String str = words.

Thanks a lot. This is great. I have no more questions.

uint8_t i2c_smbus_read_block ( uint8_t command, uint8_t* blockBuffer, uint8_t blockBufferLen ) {
  uint8_t num_bytes=0;
  uint8_t x = 0; //index for block buffer explicitly set to 0
  uint8_t y = 0; //index for words set explicitly to 0
  
  Wire.beginTransmission(deviceAddress);
  Wire.write(command);
  Wire.endTransmission(false);
  num_bytes = Wire.requestFrom(deviceAddress, blockBufferLen);
  if (num_bytes == blockBufferLen) // received the same amount as requested ?
  {
    while (Wire.available()) 
    {
      blockBuffer[x] = (uint8_t) Wire.read();
      
      if (blockBuffer[x] >= 65 && blockBuffer[x] <= 90)
      { //if x=upercase Letter, print it
        Serial.print((char)blockBuffer[x]);
        words[y] = (char)blockBuffer[x];//add to array
        words[y + 1] = '\0'; //null terminate
        y++; //increment words index
      }
      else if (blockBuffer[x] >= 97 && blockBuffer[x] <= 122)
      { //if x=lowercase Letter
        
        if (blockBuffer[x - 1] >= 65 && blockBuffer[x - 1] <= 90)
        { //if the last was upper,leave
          return num_bytes;
        }
        
        Serial.print((char)blockBuffer[x]);  //else print it
        words[y] = (char)blockBuffer[x]; //add to array
        words[y + 1] = '\0'; //null terminate
        y++;//increment words index
      }
      else if (blockBuffer[x] == 255) 
      {//stop
        //words[y + 1] = '\0'; //don't add to words
        return num_bytes;
      }
      x++; //increment block buffer reading index
    }
  }
}
int fetchWord(byte func)
{
  unsigned int b1, b2;
  Wire.beginTransmission(deviceAddress);
  Wire.write(func);
  Wire.endTransmission(false);

  Wire.requestFrom(deviceAddress, 2 );
  b1 = Wire.read();
  b2 = Wire.read();
  return word(b2, b1);
}

greetings Krogs

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.