Eeprom String Read - Two solutions with different answers

I am looking at a method that assigns '/0' at the write and reads back the data until this is found. In this way the string can be of any length
There are two ways to read, one is to append the read chars to a string, and the other to enter the char into an array.
The enclosed program shows both these methods, which on the face of it look identical, except that when I print out the recieved string there is an extra character on the end of the string
image


#include <Wire.h>
const int Disk1=0x57;              //Address of 24LC256 eeprom chip or ZS042

//Write to the Ext EEPROM
//========================================================================
void WriteEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
  delay(5);
}
 
 //Read From the Ext EEPROM
//========================================================================
byte ReadEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress,1);
  if (Wire.available()) 
    rdata = Wire.read();
  delay(5);  
  return rdata;
}

// Writes a string to EEprom
//-----------------------------------------
void EepromWriteString(int Disk,int Location,String Str)
{
  int len = Str.length();
  for(int i=0;i<len;i++)
    {
    WriteEEPROM(Disk, Location+i,Str[i]) ;
    }
  WriteEEPROM(Disk, Location+len,'\0');   // termination character          
}


// Reads a string from EEprom
//-----------------------------------------
String EepromReadString(int Disk,int Location)
{
  String Str=""; 
  int len=0;
  char ks;
  while(ks !='\0' )   //Read until termination character  
    {   
    ks=ReadEEPROM(Disk,Location+len);
    Str = Str+char(ks);
    len++;
    };
  return String(Str); 
}

// Reads a string from EEprom into a data array
//-----------------------------------------
String EepromReadStringData(int Disk,int Location)
{
  char data[100]; //Max 100 Bytes
  int len=0;
  char kd;
   while(kd !='\0' )   //Read until termination character  
    {   
    kd=ReadEEPROM(Disk,Location+len);
    data[len]=kd;
    len++;
   // Serial.println(char(kd));
    }
  data[len]='\0';
  return String(data);
}

void setup() 
  {
  Serial.begin (115200);        
  Wire.begin();
 
EepromWriteString(Disk1,1345, "Big Boys Toys");
delay(20);
//Serial.print("Array based   ");Serial.println(EepromReadStringData(Disk1,1345));
delay(20);
Serial.print("String based  ");Serial.println(EepromReadString(Disk1,1345));  
}
  
 void loop()
 {
 }  

Can anyone explain this please

Some bugs:

  • ks is undefined on the first iteration.
  • Then the next character is appended to the string. This character can be \0 which terminates a string in a char array but not a String.

Sorry, I don't understand how ks is undefined. It takes on the char of the EEprom read

Only after the first while() test.

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