View Full Version : Unsolved Writing to SD Card

04-05-2012, 02:04 AM
I have been using FSRW26 to try to write to an SD card, but I am unable for some reason to assign a variable and write it to the SD card. I know I must be missing something stupid. I am Gadget Gangster's tutorial to write a predetermined quotation to the card just like he does, but I cannot figure out how to write the value of a variable to the card.

As an engineer (non electronics related), with 30 years experience, I find it absolutely incredible that a simple method to apply an object to write data to an SD card is so illusive. Parallax has done a pretty good job with the basic stamp, but for the entry level people, too much time is being wasted on the Propeller. This should be simple and direct, and I am sure the final coding is, but why make it so darn complex to get a grasp of this? I know I am not alone.

Can anyone point me to a simple code that writes a variable to the SD card? I know how to use objects.

Duane Degn
04-05-2012, 02:20 AM
Edit: I realized after I wrote this post, that it's a bad example. It too complicated. I'm sure there are simplier ways to do this. I'll leave this post here in case it can be useful and because I don't like to delete posts.


One thing you need to decide is how you want your variables stored.

Do want them stored as computer native numbers or do you want them stored as ASCII characters.

If you want to be able to read the file from Excel for some other text reader, you probably want to store the number in ASCII characters.

In my PropBOE-Bot project (http://forums.parallax.com/showthread.php?138903-PropBOE-Bot), the servo pulse length are written to the SD card as ASCII characters.

Here's the section of code to store the variables.

if driveMode == _RcRecord
LongToDecStr(wheelSpeed[_PortWheel], 4, @sdBuffer)
LongToDecStr(wheelSpeed[_StarboardWheel], 4, @sdBuffer)

I convert the variables wheelSpeed[_PortWheel] and wheelSpeed[_StarboardWheel] to ASCII characters and store them in the buffer "sdBuffer" and then write the buffer as string to the card.

The SD card objects may have a method that does this conversion for you. I wanted to minimize the number of writes to the card so I built up a buffer to write all at once (now I wonder why I didn't add the "13" and "10" end of line characters to the buffer first).

Here's the method "LongToDecStr"

PUB LongToDecStr(value, digits, localPtr) | temp
'' Write an ASCII decimal number to localPtr
temp := 1
repeat digits
temp *= 10
value //= temp ' tuncate value that wont fit in digits
repeat digits
temp /= 10
byte[localPtr++] := (value / temp) + "0"
value //= temp

I'm using Kye's SD card object but the principles apply to FSRW26 as well.

If you don't want the data stored as ASCII data then you'd use a different method to write the values.

04-05-2012, 02:33 AM
Thanks, Duane.

I am unable to track where some of your variables are coming from.

To declare a variable so that it is a string, I can't use long?

Then to write the variable to the sD takes all of this? I don't follow some of the code you have listed. It must be part of your larger program.

For example, say I want to write the values of A and B to a card in ASCII format.

What would the declaration be?
What part of the could could run on its own to write to an SD card. I am using new Propeller BOE that just came out.

Duane Degn
04-05-2012, 02:34 AM
As I mentioned, I use Kye's "SD-MMC_FATEngine.spin" object for writing to a SD card.

It doesn't have a method to automatically write a variable as ASCII characters so you'd need to use a method such as "LongToDecStr" to write a variable as ASCII characters.

The SD object does have a writeLong method. So if your variable name is "variableX" and you wanted to write to a SD card you could use this code.


There are write methods for other variables sizes as well.

The PropBOE-BOT code I mentioned earlier is posted in the (currently) last post of the thread I linked to in my earlier post.

04-05-2012, 02:50 AM
Thanks. Where is Kyles object? I don't see it in the propeller object in obex.

04-05-2012, 03:10 AM
You can use the Simple_numbers object to generate strings from Variables, and then write them with the pputs methode to the SD card. Between the strings you can write the separation characters you need.

sd : "FSRW"
num : Simple_Numbers"

long x,y
'sd mount
'sd open


Duane Degn
04-05-2012, 03:13 AM
Thanks. Where is Kyles object? I don't see it in the propeller object in obex.

It's way to hard to find things in the OBEX IMO.

But I did find it.

Kye's object is very sensitive to card errors. I've had to reformat cards before they would work. "Quick" formats don't fix the problems (if there are any) either.

04-05-2012, 11:22 AM
Thanks, Andy and Duane!

Andy, it seems that your suggestion best fits where I am now. I was apparently having problems with the FSRW and the string issue thing. I will try it today. I bet it resolves my issue.