Write to SD Card

02 Nov 2011

Hi there NEWBIE ALERT.. Hi i am new to this forum so here is my first post.

I need a bit of help reading a .txt file from an SDcard. I have looked all over and all i can find is how to write to the card.

The data in the txt file will be along the lines of

//////

A1.1, B2.2, C3.3,

A1.2, B2.1, C3.1,

etc......

//////

with this in mind i then want to split each line up as an instruction e.g.

A1.1 B2.2 C3.3

and finally lol extract the string and floating point variables into registers

e.g.

A1.1 String Instruction1 = A ; Float Data1 = 1.1 ;

B2.2 String Instruction2 = B; Float Data2 = 2.2;

C3.3 String Instruction3 = C; Float Data3 = 3.3;

I hope my explanation was precises and look forward to any help any one can give.

02 Nov 2011

fopen and fgets or fgetc are the C functions which work with the filesystem:

void read_textfile()
{
     FILE *fp;               // setup a pointer to file

     fp=fopen("/sd/somefile.txt","r");  // open the file for read operations
     if (fp)                            // if we got a handle the start to read
     {

                   // after this you can use fgetc to read a single character from the file
                   // or fgets to read a string on which you can use scanf to read the A/B or C and float numbers
                  
02 Nov 2011

Erm.. you know you've titled it "write to an SD Card", when it appears to be read ?

anyway..

there's a library that lets you read key/value pairs.

http://mbed.org/users/shintamainjp/notebook/configfile_en/

it *might* be just what you need ? I've used it for reading floats from a config file.

value1=1.1 value2=2.2 value3=3.3

cheers

Dave

02 Nov 2011

Thanks for your kind help.

Yeah your write David just noticed all i can say it was very early in the morning when i posted, and i was up very late working on this project :)

02 Nov 2011

just read your links and again thanks.

But how can i split a String

e.g. A1.1

into a String and a float

e.g. String = A float = 1.1

02 Nov 2011

You can do this character by character using getc (or fgetc) or interpret a complete string by using scanf (or fscanf):

scanf(buffer,"%c%f",&mystring, &myfloat);

Here I assume the string you mention is only a single character and the float is right next to it and the complete string is already stored in 'buffer'.

For more info on scanf try: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

03 Nov 2011

Thanks that's great will give it ago as soon as possible

03 Nov 2011

hi John,..

I didn't quite understand, I thought A, B and C didn't change. If you are saying A,B and C could be changed as well as the values, i.e. it could be,

X2.4
Y2.3
G3.7

where the letters can be changed, the you need to code it yourself as per Gert's suggestions.

However, if the strings are always the same, then you can just make them the keys, and then read the keys and the values.

thing is, oif the strings can change, then you are going to need a big case statement to deal with what they possibly could be, if they really are commands. If they are text or somesuch to display on a screen, then obviously that is something different. It's hard to know anything to suggest without knowing the problem to solve.

03 Nov 2011

HI thanks for the reply.

Yeah the letters (Strings) will be changing as you stated.

What iam trying to do is build a motion controller for driving a set of motors, and the motion of the motors will be dictated my the commands within the txt file on the SDCard.

So Ineed to split the following code up

e.g Command Motor1 Motor2 Move, X10, Y20, Move, X20, Y10, etc

What i need to do is split these lines up to obtain the relevant information i.e. X10 needs splitting into string = X; and float = 10;

from then i can do some maths etc to control the motors.

03 Nov 2011

how about a standard record format then..

MOTORnXnnnnYnnnn

MOTORnXnnnnYnnnn

MOTORnXnnnnYnnnn

i.e.

MOTOR1X0010Y0020

MOTOR1X0005Y0005

MOTOR2X0012Y0012

then what I would personally do is write a little parser method that grabs a line at a time, and decodes it.

Just the other day I was playing with the GPS libraries, they basically do that. They read incoming from a serial port, and decode it usefully.

No reason why the incoming couldn't be a stream of get chars from your sd file ?

I'm afraid I'm a bit of a C beginner myself, I'm more used to other more procedural languages, and *dare* I say it.. Java. ;-) but that's how I'd set off.