Writing values to SD card
value:=123445
r := sdfat.popen(string("value.txt"), "w")
repeat y from 0 to strsize(@value)
r:=byte[noparse][[/noparse]@value[noparse][[/noparse]y]]
sdfat.pputc(r)
sdfat.pputc(13)
sdfat.pclose
THis should write 123445 to the card in file "Value" but doesn't. Help?
I tried pwrite(@value,strsize(@value)), no luck.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·······
······· "What do you mean, it doesn't have any tubes?"
······· "No such thing as a dumb question" unless it's on the internet
········
Comments
If you declared it as a long, it's a binary number, not a sequence of characters. Take a look at the "dec" routine
in FullDuplexSerial for an example of a routine to write a series of digits for a number. If you did declare it as a
byte array long enough to hold a string, where do you store the value? "value := 123445" won't do it. Lastly,
you're outputting the terminating zero byte to the file. Did you want that?
The··sdfat.pputc(13) was taken directly from the only write example in sdrw-test. This is the only write example I can find for the sd card. Maybe should be $0D?
·
repeat 3 sdfat.pputc("H") sdfat.pputc("E") sdfat.pputc("x") sdfat.pputc(13)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·······
······· "What do you mean, it doesn't have any tubes?"
······· "No such thing as a dumb question" unless it's on the internet
········
Try the following:
value:=string("123445") r := sdfat.popen(string("value.txt"), "w") repeat y from 0 to strsize(value)-1 r:=byte[noparse][[/noparse]value][noparse][[/noparse]y] sdfat.pputc(r) sdfat.pputc(13) sdfat.pputc(10) sdfat.pclose
This is off the top of my head but I think it's right.
Note that to actually write an integer out as ASCII you will need to convert the
integer value to a sequence of byte values that contain the ASCII digits.
[noparse][[/noparse]Edited to not output the trailing zero.]
Post Edited (rokicki) : 3/3/2008 6:54:15 PM GMT
1. Conver number to ascii string··· aka. Dec(value,scaleby)
2. Read the "string" and write the characters. Basically just the reverse of reading the characters.
r:= sdfat.mount(0) 'term.hex(r,2) x:=0 r := sdfat.popen(string("hobbs.txt"), "r") 'term.hex(r,2) mult:=1 repeat while r=>0 r:= sdfat.pgetc if r => ("0") and r =< ("9") byte[noparse][[/noparse]@infoin[noparse][[/noparse]x++]]:=r mult:=mult*10 mult:=mult/10 hobbsvalue:=0 repeat y from 0 to x-1 r:=byte[noparse][[/noparse]@infoin[noparse][[/noparse]y]] val:=(r-48)*mult hobbsvalue:=hobbsvalue+val mult:=mult/10 term.str(string("Read ")) term.dec(hobbsvalue) sdfat.pclose 'waitcnt(clkfreq*4+cnt)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·······
······· "What do you mean, it doesn't have any tubes?"
······· "No such thing as a dumb question" unless it's on the internet
········
If I am simply trying to write the variable as a string, how do I declare it?
Can you give me a very simple example that does this with FSRW...
x=200
y = 400
then print x and y to a card as strings on separate lines.
How do you want to write it? The variable's value could be displayed as a decimal number or a hexadecimal number or a binary number or as a 1 to 4 character string or ...
If you want to treat the variable's value as a decimal number, you could use the Numbers object from the Propeller Object Exchange. This has a variety of routines for converting a numeric value to any of several string representations (decimal, hexadecimal, etc.) contained in a buffer internal to the Numbers object. These routines then return the address of the start of the buffer which you pass to the pputs routine in fsrw like this: sdfat.pputs(address)
If you declare OBJ num : "Numbers" then you'd call sdfat.pputs(num.ToStr(x,num#DEC)) or sdfat.pputs(num.ToStr(y,num#DEC))
To get the numbers on separate lines, you'd need to do sdfat.pputs(string(13,10)) to mark the end of each line.
For the above to work, x and y could be constants or they could be any kind of variable or they could be complex expressions. The net result is the same. ToStr expects a numeric value which would always be a signed 32-bit value. A word or byte value would be converted to a 32-bit value.