Page 1 of 1

SD card files

Posted: Thu Jan 31, 2019 12:39 pm
by Dave Squibb
Hi all,
I have an application where I need to read in parameters from an SD card to PIC variables. I want to write the parameters on to the SD card on a PC.
I am thinking of using notepad to write the files and saving them as .txt files
Say I have 2 lists of parameters saved as Type1.txt and Type2.txt. Each text file has (say) 8 variables, a mixture of char (8bit) and int (16 bit).
How would I list the variables in each text file to make it simple to retrieve them?
For example Type1.txt may have 50 232 1035 877 22 123 55 1122 to be stored.
Any help appreciated.
Thanks,
Dave.

Re: SD card files

Posted: Thu Jan 31, 2019 1:21 pm
by Benj
Hi Dave,

It sounds like you need a method of parsing through the text file data.

First of all I would recommend using a state machine approach, this should make the problem easier to handle.

https://www.matrixtsl.com/wiki/index.ph ... ateMachine

You might want to store all the values into the same type of variable e.g. if your max variable is a UINT then simply have an array of UINT that you store all the values in. This certainly would help to simplify a bit.

I can make you a simple example if that would help.

Re: SD card files

Posted: Thu Jan 31, 2019 1:29 pm
by Dave Squibb
Hi Benj,
Thanks for that. I'll have a read of the state machine info. Good point about making all variables the same.
Yes please, a simple example would be very helpful.
Thanks again,
Dave.

Re: SD card files

Posted: Thu Jan 31, 2019 5:12 pm
by Benj
Hi Dave,

Here is a simple example that I hope will get you started.
ParseDemo.fcfx
(12.16 KiB) Downloaded 349 times

Re: SD card files

Posted: Thu Jan 31, 2019 6:14 pm
by Dave Squibb
Wow, I'm glad you only did a "simple" example Benj! It'll take me some time to get my head around that.
Thanks very much for taking the time to do it. (I may well be back with a few questions)
Dave.

Re: SD card files

Posted: Thu Jan 31, 2019 6:21 pm
by Benj
Hi Dave,

I start by populating a string variable with the data to parse. In your program you probably wouldn't use a string variable and you would instead read the data direct from a file on an SD card. Instead of storing the length of the string in StrLength I would store the number of bytes in the file which I believe there is a function for in the FAT component.

After the loop the VarIndex variable will tell you how many numeric values have been parsed.

Let me know how you get on and please ask away if there is anything you would like more info on.

Re: SD card files

Posted: Fri Feb 01, 2019 12:50 pm
by Dave Squibb
Hi Benj,

Most of this makes sense to me with the help of the simulator. Struggling with these two lines

Code: Select all

NumericData[VarIndex] = NumericData[VarIndex] * 10
NumericData[VarIndex] = NumericData[VarIndex] + (StrData[StrIndex] - '0')
What is the *10 doing? Also the -'0'?

Thanks,
Dave.

Re: SD card files

Posted: Fri Feb 01, 2019 4:53 pm
by Benj
Hi Dave,

Let's take for example the string "123"

The variable starts with the value initialised to 0. We multiply by 10 to shift the previous value up a digit.

Var = Var * 10 = 0

We then add the current number to the variable as a decimal. We subtract '0' because the string contains ASCII numeric characters which are actually in the range of 48 to 57. Having '0' quotes around a character gives us the value of the ASCII character so '1' - '0' = 1.

https://www.asciitable.com/

Var = Var + (StrData[0] - '0') is equal to Var = Var + 1

So repeating again for the other two characters in the string.

Var = Var * 10 = 10
Var = Var + 2 = 12

Var = Var * 10 = 120
Var = Var + 3 = 123

Re: SD card files

Posted: Fri Feb 01, 2019 5:06 pm
by Dave Squibb
And all is now clear.
Thanks Benj.