SD Card Help

I’m trying to experiment with the SD Card and I’m confused on what the commands do. How do you create a file, add text, save the file & open and read files? Example code would be great and the C++ Pro API is slightly confusing and not helpful. Any help would be great!

https://github.com/3038922/v5debug/blob/master/include/ncrapi/system/configSet.hpp
This is a class library I wrote, I am also a novice, not too professional, you can refer to it.

Here’s a simple example showing how to use the VCS SD card save and load file API. This saves some information into a file called “test.h” on the SD card and then reads it back again.

#include "robot-config.h"

// storage for some information to save
uint8_t     myTestData[ 100 ];
uint8_t     myReadBuffer[ 1000 ];

int main() {
    // set the test data to something detectable
    for(int i=0;i<100;i++) {
        myTestData* = i * 2;
    }
    
    // write test data to SD Card
    int nWritten = Brain.SDcard.savefile( "test.h", myTestData, sizeof(myTestData) );

    // did that work ?
    if( nWritten > 0) {
        // display on screen how many bytes we wrote
        Brain.Screen.setCursor( 2, 2 );
        Brain.Screen.print( "We wrote %d bytes to the SD Card", nWritten );

        // now read it back into a different buffer
        int nRead = Brain.SDcard.loadfile( "test.h", myReadBuffer, sizeof(myReadBuffer) );

        // display on screen how many bytes we read
        Brain.Screen.setCursor( 3, 2 );
        Brain.Screen.print( "We read %d bytes from the SD Card", nRead );

        // and display some of the data
        Brain.Screen.setCursor( 6, 2 );
        for(int i=0;i<8;i++)
            Brain.Screen.print("%02X ", myReadBuffer*);
    }
    else {
        Brain.Screen.printAt( 10, 40, "Error writing to the SD Card" );        
    }
}

In addition to this, the standard C API can be used, code like this (just a snippet, will not compile)


    // Test C file API
    FILE *fp;
    printf("read VEX_Messages.h\r\n");
    fp = fopen( "VEX_Messages.h", "rb" );
    if( fp != NULL ) {
      fseek( fp, 0, SEEK_END );
      int len = ftell( fp );
      fseek( fp, 0, SEEK_SET );
      printf("that worked, size is %d\r\n", len);
      
      if( len > 0 && len < 128000 ) {
        char *buf = (char *)malloc( len );
        int nRead = fread( buf, 1, len, fp );
        printf("we read %d bytes\r\n", nRead );
        free(buf);
      }
      fclose(fp);
    }
    else {
      perror("could not open file");
      printf("\r");
    }

Also, under certain conditions C++ streams can be used but we have found some issues with VCS in this area so I would avoid that for now.**

8 Likes

I could not find the corresponding python class for accessing the SD Card…is it in the current release?

@ljohri Currently, VCS does not do Python, just C++ and Modkit. If you were looking at Python documentation for V5, it was for Robot Mesh Studio, not Vex Coding Studio. (Two different products from different companies, similar names and uses.) And SD card support is not yet enabled in RMS.

After running this same exact code I got a couple errors I think all starting from this one at the uint8_t part
07%20PM

That’s strange, wonder how/why those [ were removed. I will fix the original code.

1 Like

thanks that fixed that but that myTestData* = i * 2; does not work anymore it gives the error: expected expression myTestData* I * 2, which is the same thing weirdly enough

that I don’t know what to do from here for that I think its the whole unsigned char thing

I get the feeling that code was corrupted somehow, perhaps in the forum migration, I’ll put another version later that works.

1 Like

Here, this is what it should be

#include "robot-config.h"

// storage for some information to save
uint8_t     myTestData[ 100 ];
uint8_t     myReadBuffer[ 1000 ];

int main() {
    // set the test data to something detectable
    for(int i=0;i<100;i++) {
        myTestData[i] = i * 2;
    }
    
    // write test data to SD Card
    int nWritten = Brain.SDcard.savefile( "test.h", myTestData, sizeof(myTestData) );

    // did that work ?
    if( nWritten > 0) {
        // display on screen how many bytes we wrote
        Brain.Screen.setCursor( 2, 2 );
        Brain.Screen.print( "We wrote %d bytes to the SD Card", nWritten );

        // now read it back into a different buffer
        int nRead = Brain.SDcard.loadfile( "test.h", myReadBuffer, sizeof(myReadBuffer) );

        // display on screen how many bytes we read
        Brain.Screen.setCursor( 3, 2 );
        Brain.Screen.print( "We read %d bytes from the SD Card", nRead );

        // and display some of the data
        Brain.Screen.setCursor( 6, 2 );
        for(int i=0;i<8;i++)
            Brain.Screen.print("%02X ", myReadBuffer[i]);
    }
    else {
        Brain.Screen.printAt( 10, 40, "Error writing to the SD Card" );        
    }
}

somehow text for arrays was messed up. So code like this myReadBuffer* should be myReadBuffer[ i ]
I suspect that a migration script mistakenly took [ i ] as an italic tag or something like that.

4 Likes

Thanks that worked. I guess that was the only problem

So, where would you input your data, for say, I am trying to put in encoder values from v5 motors (velocity and speed). How would I go about writing that data in?

I would presume that you are trying to create a recording function based off of your want to record motor values to the sd card. speaking as somebody who already has one of these types of programs working you have 2 main options in relation to what data you want to store. if you store motor velocities you will need to add about 120 to all values before sending it to the file and subtract 120 when you pull it back out or use it. the reason for this is because the sd card can only create text files of uint8_t which is an unsigned integer from 0 - 255. if you want to record encoder values you will need to do some fancy math to get the values under 255, possibly calculate the difference between each segment. after you know what information you would like to store you need to now store that info. create an array of uint8_t for each motor or value you want to store then transfer data into that array. when you have filled all the arrays save the file to the sd card by using brain.sdcard.savefile(“textfilename.txt”, uint8_t array name, uint8_t array length); when you want to read it back in type the same command but swap savefile for loadfile.

Wait so if you want to use the sd card you have to call it

@terminator6985 IT would appear so

Vexforumers usually don’t like it when people revive old topics, please refrain from doing so please.

1 Like