Someone has and experience with OpenLog

Dear All,

I am starting with OpenLog which is a great product.

However, I pain to undertand somethings. I spent all my week-end trying to understand and reading the doc.
I created two functions. One for readind and one for writing (append).

Description
for exemple, when I power on my module, that function is called

#define SDGPSLOGFILE "GPSLOG.TXT"
#define SDLOGFILE "SEQLOG00.TXT"

sd_write(SDGPSLOGFILE,"START");
sd_write(SDLOGFILE,NULL)

It will craete two files (if it does not exist). For the first, it will append the text “START” to GPSLOG.TXT. The second will create SEQLOG.TXT (if it does not exist), but it will not append a specific text. It will records all Serial.print() which are uncomment for debuging.

Here is the code to reset the Openlog and to write to the SD card. It’s works, until I read the SD card. (See below the following code).

/* ********************
      OPENLOG
*********************** */
bool WI908::sd_reset(void)
{
  bool answer = false;
  unsigned int timeout = 3000;
  unsigned long previous;
  previous = millis();


  #ifdef DEBUG
    sprint(F("Rst SD"));
    sprint(F("\t\t\t"));
  #endif

  // Rest OpenLog
  digitalWrite(PIN_RSTSD, LOW);
  delay(100);
  digitalWrite(PIN_RSTSD, HIGH);

  // Wait for OpenLog to indicate file is open and ready for writing
  do{
    if(Serial.available())
    {
      if(Serial.read() == '<')
      {
        answer = true;
      }
    }

  }while((answer == false) && ((millis() - previous) < timeout));

 

  #ifdef DEBUG
    if(answer)
    {
      sprintln(OK_RESPONSE);
    }
  #endif

  return answer;
}


void WI908::sd_write(char * file, char * string)
{
  if(isSDok)
  {
    // Go to command mode
    Serial.write(26);
    Serial.write(26);
    Serial.write(26);

  
    //Wait for OpenLog to respond with '>' to indicate we are in command mode
    while(1) {
      if(Serial.available())
        {
          if(Serial.read() == '>')
          {
            break;
          }
        }
    }

    // APPEND TO THE FILE GPSLOG
    Serial.print(F("append "));
    Serial.println(file); //"GPSLOG.TXT OR SEQLOG00.TXT"
 
    //Wait for OpenLog to indicate file is open and ready for writing

    // MY PROBLEME IS HERE. THAT FUNCTION WORKS EXCEPTED AFTER READING A FILE
    // FOR EXEMPLE, WHEN I READ THE FILE GPSLOG.TXT ( sd_read("GPSLOG.TXT");)
    // THE FILE IS READ AND JUST AT THE END OF THAT FUNCTION sd_write(SDLGOFILE.TXT,NULL)
    // IS CALLED, AND STOP HERE BECAUSE IT NEVER RECEIVE THE <.
    // WHY IT WORK, BUT DOES NOT WORK JUST AFTER READIN A FILE??
    while(1) {
      if(Serial.available())
      {
        if(Serial.read() == '<'){
          break;
        }
      }
    }

    // If string is not NULL , write the string. If NULL write all debug Serial.println() in SEQLOG00.TXT
    if(strlen(string) > 0)
    {
      // Write the fix to the file
      Serial.print(F("$"));
      Serial.print(string);
      Serial.println(F("!"));
    }

  }
  else
  {
    #ifdef DEBUG
      sprintln(SDNOCARD);
    #endif
  }
}

This works, until now I read.

I also have a function for reading a file. I commented the code.
This work as well (I think :wink: ). When the file is red, it return a > to indicate it finish reading.
The problem, It is, how to leave the reading mode, because it was like if openLog continue returning unwish caracters.
For this, in the sd_read() function, at the end, I am calling sd_write(SDLOGFILE,NULL); to immediately write log into SEQLOG00.TXT from all Serial.print)

bool WI908::sd_read(char * file)
{ 
  // Empty buffer
  memset(buffer,'\0',BUFFERSIZE);
  uint8_t x = 0;
  bool answer = false;
  unsigned long previous;
  previous = millis();
 
  // Read for 3sec
  unsigned int timeout = 3000;
  
  // If sd_reset() return 1
  // When the openLog is power with no SD card, it return 0
  if(isSDok)
  {
    // Go to command mode
    Serial.write(26);
    Serial.write(26);
    Serial.write(26);

    // Wait for OpenLog to respond with '>' to indicate we are in command mode
    while(1) {
      if(Serial.available())
        {
          if(Serial.read() == '>')
          {
            break;
          }
        }
    }

    // Send command 'read GPSLOG.TXT 0 150 1' (read fileName from numberCaracter ASCII)
    Serial.print(F("read "));
    Serial.print(file);
    Serial.println(F(" 0 150 1"));

    //Wait for OpenLog to respond with 'r'
    while(1) {
      if(Serial.available())
      {
        if(Serial.read() == '\r')
        {
          break;
        }
      }
    }

    // Read the file
      do{
        if(Serial.available() > 0)
        {
       
          if(x < BUFFERSIZE-1) // Do not fill the buffer more the it size
           {
            char r = Serial.read();
            
            // OpenLog return > when it finish reading.
            if(r == '>')
            {
              // To leave the loop
              answer = true;
            }
            else
            {
              // Fill buffer
              buffer[x] = r;
              x++;
            }
        

          }
          else
          {
            // too many read. Buffer is too small
            #ifdef DEBUG
              sprintln(F("*Overflow! 5*"));
            #endif
          }
        }

      }while((answer == false) && ((millis() - previous) < timeout));

    // Close buffer
    buffer[x]='\0';
    delay(500);

    #ifdef DEBUG
      // Display the file content
      Serial.write(buffer);
      Serial.println(x);
      Serial.println(answer);
    #endif

    // Empty buffer
    memset(buffer,'\0',BUFFERSIZE);
  
    // Return the apeending mode to the LOGFILE.TXT
    sd_write(SDLOGFILE,NULL);
    // IS THERE ANOTHER WAY TO LEAVE THE READING MODE??
    // MY PROBLEM IS HERE. AT THIS MOMENT, SD_WRITE() IS CALL. IT GOES TO COMMAND MODE BUT NEVER RECEIVE THE >
  }
  else
  {
    #ifdef DEBUG
      sprintln(SDNOCARD);
    #endif

    answer = false;
  }
  return answer;

}

The problem
At the end of sd_read(), I am calling sd_write(SDLOGFILE,NULL); to leave the reading mode and to continue writing into LOGFILE.TXT the content of all uncommented Serial.print().

The problem start from here. sd_write(SDLOGFILE,NULL) put back OpenLog in command mode because f the 3 Ctrl+z.
Then it wait for > to indicate it’s in command mode, but after reading a file, it never receive that >.

And I can not understand why???

Do you have an experience with OpenLog and could you explain me how to have a good reading and writing code?

many thank for your help and support.

Cheers

Hello, I am still block with that problem. Some one can help? it will help a lot. Do you have some recommandtion or do you see an error on my code Some has an exemple to read and write to SD card?

Many thank