2019年2月8日 星期五

String_Write EEPROM & Read EEPROM

/*
   Circuits4you.com
   Reading and Writing String to EEPROM Example Code
   Oct 2018
   https://circuits4you.com/2018/10/16/arduino-reading-and-writing-string-to-eeprom/

   佔用空間為字串長度 + 1 ,需注意
*/

//String key1 = "1000000000";
//String key2 = "20000000-";
//String key3 = "300000000--";

String key1 = "FAPOUNY1BRB3HIH2";
String key2 = "G1RZYYND9TV636UF";
String key3 = "GT34WTYAZ6E0HGK7";

#include <EEPROM.h>

//void writeString(char add,String data);
//String read_String(char add);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);


  //------------ write EEPROM to WifiName --------------------------------------------------------
  writeString(0xA0, key1);  //Address 0xA0 and String type key1 (char array +1 )
  writeString(0xB1, key2);  //Address 0xB0 and String type key2 (char array +1 )
  writeString(0xC2, key3);  //Address 0xC0 and String type key3 (char array +1 )


  //------------ read EEPROM to WifiName --------------------------------------------------------
  key1 = read_String(0xA0);   // read Address 0xA0 to String key1
  key2 = read_String(0xB1);   // read Address 0xA0 to String key1
  key3 = read_String(0xC2);   // read Address 0xA0 to String key1

  Serial.print("Read Data key1:");  Serial.println(key1);
  Serial.print("Read Data key2:");  Serial.println(key2);
  Serial.print("Read Data key3:");  Serial.println(key3);


  /*
    //-----write -----------------------
    String data = "Hello World";
    Serial.print("Writing Data:");
    Serial.println(data);
    writeString(10, data);  //Address 10 and String type data
    delay(10);


    //-----read -----------------------
    String recivedData;
    recivedData = read_String(10);
    Serial.print("Read Data:");
    Serial.println(recivedData);
    delay(1000);
  */

  // 串流String 內容固定長度方式
  //  Serial.println(key1); Serial.println(key2); Serial.println(key3);
  //Serial.println(key4); Serial.println(key5); Serial.println(key6);
  // saveKey(key1,0x80);  // 0x80 ~ 0x8F  (16 Byte)
  // saveKey(key2,0x90);  // 0x90 ~ 0x9F  (16 Byte)
  // saveKey(key3,0xA0);  // 0xA0 ~ 0xAF  (16 Byte)

  //  readKey(key1, 0x80); // 0x80 ~ 0x8F  (16 Byte)
  //  readKey(key2, 0x90); // 0x90 ~ 0x9F  (16 Byte)
  //  readKey(key3, 0xA0); // 0xA0 ~ 0xAF  (16 Byte)      // 將key3 從 EEPROM帶回來


}





void loop() {
  // put your main code here, to run repeatedly:

}


//<< ==== EEPROM  write_String    =============================================
void writeString(byte addr, String data) {
  int _size = data.length();
  int i;
  for (i = 0; i < _size; i++) {
    EEPROM.write(addr + i, data[i]);
  }
  EEPROM.write(addr + _size, '\0'); //Add termination null character for String Data 加入空白結束字符
//  Serial.print("write char: "); Serial.write(data, _size);      // 顯示字元陣列內容
//  Serial.print("  len: ");    Serial.print(_size);
  Serial.print("write address : 0x"); Serial.print(addr, HEX); Serial.print(" ~ 0x"); Serial.println(addr + _size , HEX);
}


//<< ==== EEPROM  read_String    =============================================
String read_String(byte addr) {
  int i;
  char data[126];                     //Max 126 Bytes
  int len = 0;
  unsigned char k;
  k = EEPROM.read(addr);
  while (k != '\0' && len < 500) {    //Read until null character 讀取直到null字符
    k = EEPROM.read(addr + len);
    data[len] = k;
    len++;
  }
  data[len] = '\0';                   // 先找出(while)  空白結束字符(\0),帶入 string 長度(len)
  return String(data);
}


  //<< ========== save key String to EEPROM =============================================
byte saveKey(String strKey, word addr) {
  String keystr = String((String)strKey);
  uint8_t len = keystr.length();                               // 自動取得串流長度
  uint8_t keyData[len];
  //  for(word i=0;i < len;i++)keyData[i] = keystr.charAt(i);    // 將keystr串流轉換成keyData字元陣列
  for (word i = 0; i < len; i++) {
    keyData[i] = keystr.charAt(i);                                //來源為串流才使用將(串流)轉換成 (字元陣列)
    EEPROM.write(addr + i, keyData[i]);
  }
  Serial.write(keyData, len);                                  // 顯示字元陣列內容
  Serial.print("  len="); Serial.println(len);
  //  EEPROM.commit(); // Buffer SAVE to flase  (ESP8266 Only)
  delay(200);
}


  //<< ========== read key EEPROM to Stringg   ==========================================
byte readKey(String &strKey, word addr) {                          // &將 EEPROM read帶回來的值 寫回帶進來的變數key_
  uint8_t len = strKey.length();                                   // 串流長度
  uint8_t keyData[len];
  for (word i = 0; i < len; i++)keyData[i] = EEPROM.read(addr + i);
  Serial.write(keyData, len); delay(100); Serial.println(" ");      // 顯示字元陣列內容
  String strKey1 = (char*)keyData;                                 // uint8_t    將字元陣列keyData  轉換成串流keystr
  strKey = strKey1;
}


沒有留言:

張貼留言