Read Write files using Openlog + Arduino

Have questions about a SparkFun product or board? This is the place to be.

Moderators: phalanx, TS-Tim

Post Reply
Aspire
Posts: 12
Joined: Thu Apr 09, 2009 8:45 am

Read Write files using Openlog + Arduino

Post by Aspire » Sun Apr 25, 2010 1:40 pm

I am new to Openlog (v.1.5). I am able to interface it with Arduino Duemilanov.
Openlog enters command mode with just ONE Ctrl+z.
Below is a sample code that writes !!99 to file, and I want to read it back. Somehow, writing executes well with one minor glitch. When I open file in Wordpad it starts from line 2 instead of 1. :x
I am not able to read back from file. :evil:
Please help

Code: Select all

#include <NewSoftSerial.h>

#define RXPIN 8
#define TXPIN 9

NewSoftSerial logger(RXPIN,TXPIN);

void setFile(char fname[40])
{
  int wait = 200;
  Serial.println(fname);
  logger.print(26, BYTE);
  delay(wait);
  logger.print("new ");
  logger.println(fname);
  delay(wait);
  logger.print("append ");
  logger.println(fname);
  delay(wait);  
}

void writeFile(char fname[40], int offset)
{
  int wait = 200;
  Serial.println(fname);
  logger.print(26, BYTE);
  delay(wait);
  logger.print("write ");
  logger.print(fname);
  logger.print(" ");
  logger.println(offset);
  delay(wait);
}

void delFile(char fname[40])
{
  int wait = 200;
  Serial.println(fname);
  logger.print(26, BYTE);
  delay(wait);
  logger.print("rm ");
  logger.println(fname);
  delay(wait);
}

void readFile(char fname[40], int start, int length)
{
  int wait = 200;
  logger.print(26, BYTE);
  delay(wait);
  logger.print("read ");
  logger.print(fname);
  logger.print(" ");
  logger.print(start);
  logger.print(" ");
  logger.println(length);
  delay(wait);  
}

void setup() {
  pinMode(RXPIN, INPUT);
  pinMode(TXPIN, OUTPUT);
  delay(1000);
  Serial.begin(9600);
  logger.begin(9600);
  delay(3000);
 // delFile("test.log");
  setFile("test.log");
  logger.print(33,BYTE);
  logger.print(33,BYTE);
  logger.print(99, DEC);
  logger.print(26, BYTE);
  delay(200);
  logger.println("sync");

  readFile("test.log",0,8);
  while(logger.available()>0){
   // if(logger.read() == '!'){
   //   if(logger.read() == '!'){
  Serial.print(logger.read(),DEC);
     // }}
  }
  Serial.println();  
}

void loop() { 


}

User avatar
sparky
Site Admin
Posts: 1337
Joined: Mon May 05, 2003 6:02 pm
Location: Colorado
Contact:

Re: Read Write files using Openlog + Arduino

Post by sparky » Wed Apr 28, 2010 10:15 am

I think I found some of your problems.

1) Because you never know what state OpenLog is in (is it in file mode or already at the command prompt?), you may need to press enter after sending ctrl+z to clear out some erroneous ctrl+zs. For example, if you load a new sketch onto an OpenLog that is already at command prompt, Openlog will receive 'ctrl+z' and then 'rm test.log'. '#rm test.log' is not a valid command (where # is the ctrl+z character).

2) OpenLog echos all the characters and commands while in command mode. This causes a ton of junk to sit in the logger.read() buffer. You need to do a logger.flush() to try to remove the extra junk.

3) Depending on your OpenLog version, you will need to send the escape character once or three times. In version 1.5 and below, the escape character is one ctrl+z. Well, during an Arduino bootload, OpenLog would receive a ctrl+z in the stream and drop to command mode then proceed to try to parse the incoming HEX file as commands. So in v1.51 I added multiple escape characters, so v1.51 and above require three ctrl+zs. So that's the new defines in the code you see below.

Eventually, we should write some '>' and '<' parsing so that we know where we are and what we're doing. But for now, this code works without it:

Code: Select all

/*
  SparkFun Electronics
  Nathan Seidle
    
  Example code for Arduino for creating a new file on OpenLog, appending to it, synching, then reading it back using NewSoftSerial
  
  Remember, println sends a carriage return (ASCII 13) and a line feed (ASCII 10). This will cause extra weird characters to show
  up in the log file. When sending commands to OpenLog it might be better to use
  logger.print("new ");
  logger.print(fname);
  logger.print(13, BYTE);

  Instead of:
  logger.print("new ");
  logger.println(fname);

*/

#include <NewSoftSerial.h>

#define RXPIN 8
#define TXPIN 9

NewSoftSerial logger(RXPIN,TXPIN);

#define NEW 1
#define OLD 0

//Send ctrl+z once for older versions v1.5 and below
//#define OPENLOG_VERSION OLD
//Or three times for the newer v1.51 and above
#define OPENLOG_VERSION NEW

//Creates a file, such as 'test.log'
void setFile(char fname[40])
{
  int wait = 200;
  Serial.print("Creating file: "); //Debug to terminal
  Serial.println(fname); //Debug to terminal

  //Send escape command
  logger.print(26, BYTE);
#ifdef OPENLOG_VERSION  NEW
  logger.print(26, BYTE);
  logger.print(26, BYTE);
#endif
  //We don't know where we are without checking. Either we just dropped to command prompt, or we were already sitting at the command prompt.
  //If we were already at the command prompt, then we've got a bunch of command characters sitting there.
  //Press enter to dump them
  logger.print(13, BYTE);

  delay(wait);

  //Create new file
  logger.print("new ");
  logger.println(fname);

  delay(wait);
  
  //Begin logging to that file
  logger.print("append ");
  logger.println(fname);

  delay(wait); 
}

/*void writeFile(char fname[40], int offset)
{
  int wait = 200;
  Serial.println(fname);

  //Send escape command
  logger.print(26, BYTE);
#ifdef OPENLOG_VERSION  NEW
  logger.print(26, BYTE);
  logger.print(26, BYTE);
#endif

  delay(wait);
  logger.print("write ");
  logger.print(fname);
  logger.print(" ");
  logger.println(offset);
  delay(wait);
}*/

void delFile(char fname[40])
{
  int wait = 200;
  Serial.print("Deleting file: "); //Debug to terminal
  Serial.println(fname);

  //Send escape command
  logger.print(26, BYTE);
#ifdef OPENLOG_VERSION  NEW
  logger.print(26, BYTE);
  logger.print(26, BYTE);
#endif
  //We don't know where we are without checking. Either we just dropped to command prompt, or we were already sitting at the command prompt.
  //If we were already at the command prompt, then we've got a bunch of command characters sitting there.
  //Press enter to dump them
  logger.print(13, BYTE);

  delay(wait);
  logger.print("rm ");
  logger.println(fname);
  delay(wait);
}

void readFile(char fname[40], int start, int length)
{
  int wait = 200;

  //Send escape command
  logger.print(26, BYTE);
#ifdef OPENLOG_VERSION  NEW
  logger.print(26, BYTE);
  logger.print(26, BYTE);
#endif
  //We don't know where we are without checking. Either we just dropped to command prompt, or we were already sitting at the command prompt.
  //If we were already at the command prompt, then we've got a bunch of command characters sitting there.
  //Press enter to dump them
  logger.print(13, BYTE);

  delay(wait);
  logger.print("read ");
  logger.print(fname);
  logger.print(" ");
  logger.print(start);
  logger.print(" ");
  logger.print(length);

  //OpenLog is echoing all the commands up to this point including 'read test.log 0 100', etc. We don't want this stuff in the buffer
  logger.flush(); //Remove all the trash in the current buffer

  //Now send the enter command to OpenLog to actually initiate the read command
  logger.print(13, BYTE);

  delay(wait); 
}

void setup() 
{
  pinMode(RXPIN, INPUT);
  pinMode(TXPIN, OUTPUT);
  pinMode(13, OUTPUT);

  delay(1000);

  Serial.begin(9600);
  logger.begin(9600);

  delay(3000);

  delFile("test.log");

  setFile("test.log");
  logger.print(33,BYTE);
  logger.print(33,BYTE);
  logger.print(99, DEC);
  logger.print("Yay!");

  //Send escape command
  logger.print(26, BYTE);
#ifdef OPENLOG_VERSION  NEW
  logger.print(26, BYTE);
  logger.print(26, BYTE);
#endif

  delay(200);
  
  //Force a buffer record. This is always needed. OpenLog will auto sync if there is no activity after 5 seconds.
  logger.println("sync");
  delay(200);


  readFile("test.log",0,8);

  char test_char;
  while(logger.available()>0)
  {
    test_char = logger.read();
    
    //Attempt to parse out the value read
    if(test_char == '!')
    {
      test_char = logger.read();
      if(test_char == '!')
      {
        Serial.print("Found it!!: ");
        //Read two characters
        Serial.print(logger.read(), BYTE);
        Serial.println(logger.read(), BYTE);
      }
      else
      {
        Serial.print("Weird2: ");
        Serial.println(test_char);
      }
    }
    else
    {
      Serial.print("Weird1: ");
      Serial.println(test_char);
    }
  }
  Serial.println(); 
}

void loop() 
{
  //Blink on completion  
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
Hope this helps!
-Nathan
Pursue the cheese, win the excellence.

kenobi.obione
Posts: 2
Joined: Mon Jul 11, 2011 8:20 am

Re: Read Write files using Openlog + Arduino

Post by kenobi.obione » Mon Jul 11, 2011 8:27 am

Hi all,
I recently purchase an arduino pro mini 5v,328 and openlog device. For the last few weeks I have been successfully using the devices in harmony. Last week however, I discovered that the text files the openlog was recording, were gibberish/garbled text that does not in any way represent the serial print lines I have been sending it in my code. I am curious if anyone knows how to fix this and get back to recording correct characters.

I know the openlog device worked at one point so unless the chip fried, i was able to write to it at one time. I have also checked that the baud rate is correct, thinking that that may have perhaps been the problem.

any thoughts or help will be greatly appreciated. thanks
:)

kenobi.obione
Posts: 2
Joined: Mon Jul 11, 2011 8:20 am

Re: Read Write files using Openlog + Arduino

Post by kenobi.obione » Mon Jul 11, 2011 8:53 am

never mind i fixed it. thanks anyway tho. accidentally plugged the wire into the incorrect port on arduino. lol
RXI goes to TXO not RXO lol.

Post Reply