itoa help, reading text file from SD card

Hello folks.

I am using a Rogue Robotics uMMC Serial Data Module to interface with a SD card : (see post below for the link)

On the card I have a file with 64 comma delimited values (byte sized values 0-255). This is generated by a program on my computer, written as a .txt file.

i can read the information off the card with this :

#define MSG_HEADER       255 //header byte
#define MSG_LEN          65  //Length of array

int esc = 27;  //ascii escape key
int carrot= 62;  //ascii 'c'
int stick = 0; 
boolean checker;

unsigned int MyArray[]=
{
  254,254,254,254, 0,0,0,0, 254,254,254,254, 0,0,0,0, 
  0,0,0,0, 254,254,254,254, 0,0,0,0, 254,254,254,254,
  254,254,254,254, 0,0,0,0, 254,254,254,254, 0,0,0,0,
  0,0,0,0, 254,254,254,254, 0,0,0,0, 254,254,254,254};

void setup()
{     

  delay(500);
  Serial.begin(9600);
  delay(1000);
  Serial.print(esc, BYTE);
  delay(10);
}

void loop()
{
  if(Serial.available()){
    checker=CarrotThere();
    if (checker==true){
      OpenFile();
      Serial.flush();
    }
    else if(checker==false){
      // digitalWrite(13, LOW);
      Serial.flush();
    }
  }

  delay(1);
}


void OpenFile(){
  Serial.print("C 1");
  Serial.print(13,BYTE);
  delay(1);
  Serial.print("O 1 R /FRAME.TXT");
  Serial.print(13,BYTE);
  delay(1);

  if(Serial.available()){
    checker=CarrotThere();
    if (checker==true){
      Serial.print("R 1 65");
      Serial.print(13,BYTE);
      GetArray();

    }
    else{

      // Serial.flush();
    }
  }

}

void DoThing(){

// this is where i'll probably need to do the itoa conversion
// but i will need to make a flexible length array
// identify commas, and return the array not as the long byte array  
// but as the one i wrote to the text file

  for(int i =1;i<MSG_LEN;i++){
    Serial.println(MyArray[i]);

  }
  Serial.println();

}

boolean CarrotThere(){

  stick=Serial.read();
  if(carrot==stick){
    stick=0;
    return true;
  } 
  else{
    stick=0;
    return false;
  }

}


void GetArray(){
  boolean gotMsg = false;

  delay(1);
  CarrotThere();
  if(Serial.available() && (Serial.read()==MSG_HEADER)) {
    while(Serial.available()<=MSG_LEN);
    for(int i=0;i<MSG_LEN;i++){
      MyArray[i]=Serial.read();
      gotMsg=true;
    } 
  }
  if (gotMsg==true){
    DoThing(); 
  }
  else{
    Serial.flush();
  }
}

However, if the text file contains "255, 127, 0, ...," the Arduino reads each character in the file as a byte. So i think i'm gonna need to do an itoa call, parse out the commas as identifiers for each byte, and stick the new numbers together into my array of 64 bits. Another issue I'm running into, is that the length of the file will change every file I read, so while I figure I can count commas, i won't know how long i'll be looking for each byte.

now, I am absolutely clueless as how to approach this aspect of it. i can read the file fine off the card, but it's not coming in any way i can use. Looking through the forum for the itoa stuff has been helpful in the sense that i beileve its the thing i need, but actual implementation is a different thing.

Thanks!

Here's a link to the Rogue reader :: uMMC Data Storage Module – Rogue Robotics

You'll need to write a "get next byte from file" function, and you'll also need a way to signal end-of-file. Rather like 'getc', 'getchar' and 'fgetc' in Unix.

As for ASCII-to-binary-integer conversion, yes, 'atoi' is the way to go. Accumulate characters into a small array, stop when you read a comma, then terminate the string with '\0' and call 'atoi'.

Er, atoi() is so simple, why reserve memory for an array? Start with a long value = 0 and a boolean neg = false. If the first character is an ASCII '-', then set neg = true. From that point, each digit you encounter, value = 10*value + (character-'0'). Stop when you see a non-digit, such as a comma or newline. If that neg flag is true, value = -value.

That's an even better way to do it!