Shop OBEX P1 Docs P2 Docs Learn Events
SD Card Data Tutorial. formating data — Parallax Forums

SD Card Data Tutorial. formating data

sir_hacksalotsir_hacksalot Posts: 15
edited 2014-07-12 16:09 in Learn with BlocklyProp
Hello again,

Im grinding my way through the tutorials. All has gone well until this little issue.

Docs:
Learn GCC tutorials / Simple Devices / SD Card Data:

Hardware:
Brand new Professional Development Board:
40004 Propeller memory Card.
SanDisc 1 GB SD

Issue:
The code provided in the Learn Library works as expected. After running in terminal I powered down, extracted the SD card, mounted it in my PC and Shazamm! There it is! The test.txt file shows up with perfect text data in NotePad.

Here is that code:
/*
   SD Minimal.side
 

   Create test.txt, write characters in, read back out, display. 
   
   http://learn.parallax.com/propeller-c-simple-devices/sd-card-data
 */
 

 #include "simpletools.h"                          // Include simpletools header    
 

 int DO = 22, CLK = 23, DI = 24, CS = 25;          // SD card pins on Propeller BOE
 

 int main(void)                                    // main function
 {
   sd_mount(DO, CLK, DI, CS);                      // Mount SD card
 

   FILE* fp = fopen("test.txt", "w");              // Open a file for writing
   fwrite("   'Stab self in neck..'\n", 1, 25, fp);      // Add contents to the file
   fclose(fp);                                     // Close the file
   
   char s[15];                                     // Buffer for characters
   fp = fopen("test.txt", "r");                     // Reopen file for reading
   fread(s, 1, 25, fp);                             // Read 15 characters
   fclose(fp);                                       // Close the file
 

   print("First 25 chars in test.txt that\n");       // Display heading
   print("appear just fine on my pc are:..\n\n");
   print("%s", s);                                 // Display characters
   print("\n");                                   // With a newline at the end
 }           
 


I felt like I was king of the world when this worked! The possibilities are endless!!! That being discovered, I decided to tackle the "Try this" challenge.
I wrote the code as shown in the tutorial. (I hope) and fired it off.
/*
 

  This is exactly copied (i hope) from the SD Minimal.side GCC Learn tutorial
 

  http://learn.parallax.com/propeller-c-tutorials 
 

*/
 

#include "simpletools.h"                      // Include simple tools
 



 

int main(void)
 

{
 

int DO = 22, CLK = 23, DI = 24, CS = 25; 
 



 



 

sd_mount(DO, CLK, DI, CS);                  // Mount SD card
 



 

  FILE* fp = fopen("data.txt", "w"); 
 

  int val = 500;
 

  fwrite(&val, sizeof(val), 1, fp);  
 

  val = -10000;
 

  fwrite(&val, sizeof(val), 1,fp);                                // Add contents to the file
 

  fclose(fp);                                 // Close the file
 

  
 

 
 

  fp = fopen("data.txt", "r");                // Reopen file for reading
 

  fread(&val,4,1,fp);    
 

  print("val = %d\n",val);
 

  fread(&val,4,1,fp);
 

  print("val = %d\n",val);
 

  fclose(fp);                    
 



 

                              
 

}    
 


The "run in terminal" ran as expected. I thought I was a few comma separated values from DAQ bliss until I attempted to read the file on my pc. Horrible Sadness followed. My will to live slowly wept away as I stared at the gibberish in notepad.

So began the Hacking.! About 14 hours at it now. I have caused carpal tunnel finger while navigating the Winchester Mystery House of "Library Reference".
I have so little knowledge about C that, though my efforts were quite valiant, I have to admit defeat.

Please tell me why the first code writes beautiful TEXT to the SD card and the Second code Example writes garbage. Though the Terminal reads it just fine.

I even wrote the code for the next step. I hope its not to dorky. It works in terminal but isn't decipherable as viable data on the PC. Im using rc_time with a simple variable resistor Automotive Tempe Sending unit. on Pin 5

220 Ohm to Pin5
Cap 10uf series 220 Ohm and VSS
Sensor is parallel to the cap. I don't know if that made any sense. It works....5 Digit resolution but it Goes the wrong direction for Temp up / Temp down. Really hot is small numbers and Cold is really big numbers. Ill figure that out later. I just want to see data from the SD on my PC.
/*
 

  Blank Simple Project.c
 

  http://learn.parallax.com/propeller-c-tutorials 
 

*/
 

#include "simpletools.h"                      // Include simple tools
 



 

void SDread(int q);
 



 

int DO = 22, CLK = 23, DI = 24, CS = 25; 
 

int val,temp,fp;
 

//-----------------[Main]------------------------------------------
 

int main()
 

{ 
 

SDread(24);
 

void SDread();
 



 

print("function call success!! \n");  //ignor this.  Just hacking my way around the tutorials
 

pause(200);
 

sd_mount(DO, CLK, DI, CS); 
 

int n = 0;
 



 

while(n < 20)                                   // Will this cause "sequential" writing to the SD Card?
 

                                                // I think its working but Im not sure about whats on 
 

                                                // the SD Card..
 

 {
 

   n = n + 1;  
 

 
 

  FILE* fp = fopen("data2.txt", "w");  
 

  high(5);                                      // Set P5 high
 

    pause(10);                                 //  Using QT or RC time with Varible Resistance sensor
 

                                               
 

    int dec,val = rc_time(5, 1);
 

    pause(15);
 



 

    fwrite(&val,sizeof(val),sizeof(int),fp);                                // Add contents to the file
 

    fclose(fp);
 

    pause(100);
 

    fp = fopen("data2.txt", "r");               
 

   fread(&val,sizeof(val),sizeof(int),fp);    
 

   print("SD write :    %d\n",val);                       
 

  }
 

 print("end\n");
 

}
 

 void SDread(int a)                              // Ignor this Function.  More Hack learning.
 

{                                               
 

  print("Made it to the Function call!\n");
 

  print("Variable is:...%d\n");
 

  pause(400);
 



 

}
 

 
 



Thank you for your help on this. I am actually working on a real world product that needs a lot more than my BS2p can handle.

Comments

  • edited 2014-07-09 17:09
    Writing int values to SD is somewhat different from writing the characters that represent the int values. The int value itself is likely to appear incomprehensible in notepad. However, there's a nice function called sprint (print to string) that you can use to create a formatted string that represents your value. Then, your code can write that string to the file. Here is an example:
    #include "simpletools.h"                      // Include simpletools header    
    
    int writeStrData(FILE *fp, int value);        // Function prototype
    
    int main(void)                                // main function
    {
      sd_mount(22, 23, 24, 25);                   // Mount SD card
    
      FILE* fp = fopen("test.txt", "w");          // Open a file for writing
      
      int dataItem = 1234;                        // Try a few sample data items
      writeStrData(fp, dataItem); 
      dataItem = 56;
      writeStrData(fp, dataItem); 
    
      char str[100];                              // Lots of chars
      memset(str, 0, sizeof str);                 // Write all 0 to str
      fp = fopen("test.txt", "r");                // Reopen file for reading
      fread(str, 1, sizeof str, fp);              // Read file or 100 chars, whichever is less
      fclose(fp);                                 // Close the file
    
      print("Chars in test.txt:\n");              // Display heading
      print("%s", str);                           // Display characters
    }    
    
    int writeStrData(FILE *fp, int value)         // Function
    {
      char s[27];                                 // Make array for string
      memcpy(s, 0, sizeof s);                     // Set all to 0.
      sprint(s, "dataItem = %d\r\n", value);      // Create string with value 
      char* address = strchr(s, '\0');            // Address of string terminator
      int length = (int) (address - s);           // length = address - string addr
      int result = fwrite(s, 1, length, fp);      // Add string to the file
      return result;
    }
    
  • davidmsdavidms Posts: 2
    edited 2014-07-10 02:07
    I just start here. green hand.
    Thanks for sharing
  • sir_hacksalotsir_hacksalot Posts: 15
    edited 2014-07-12 16:09
    Thank you very much for your insight on char vrs. int data storage. I spent another two days hacking blindly at the code you provided. I'm such a noob. I really want to move onto the next phase of the tutorials but I'm autistic and tend to be obsessive about some things. I haven't slept much in the past 30 hours. Im going to grind the forums for a few hours to see if anything turns up. Then Im off to bed.

    What I really want to do I store int data samples on the SD card using CSV for later analysis in Excel. I mean 10s of thousands of samples from multiple sensors that are all time coded to ach other. Probably and I2c array of sensors an clock. I am developing a product that will be shipped all over world and is to be in constant mobile deployment. I want to be able to provide technical assistance my customers. The only way I can begin to do that is to have the machine control software run a DAQ routine while the unit is operating. That way Im only a simple CSV file from knowing everything about the machine's performance, environmental and support system vectors and can thereby easily identify issues and provide solutions.
    The equipment is being field tested now without the DAQ. Im working a BS2p really hard just running primary system operations as well as three independent third party subsystems that monitor conditions live.
    Im really focused on this project. Your assistance with filling an SD card with INT data would be most helpful.

    Thanks.
    Parallax Forever!!
Sign In or Register to comment.