Shop OBEX P1 Docs P2 Docs Learn Events
Writing values to SD card — Parallax Forums

Writing values to SD card

mosquito56mosquito56 Posts: 387
edited 2012-04-04 20:29 in Propeller 1
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

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-03 18:05
    What's value? How's it declared? It looks like you might have declared it as a LONG or maybe a BYTE array?
    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?
  • mosquito56mosquito56 Posts: 387
    edited 2008-03-03 18:32
    Value was declared as long. Changed to byte[noparse][[/noparse]8] and will try again.
    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
    ········
  • rokickirokicki Posts: 1,000
    edited 2008-03-03 18:36
    Howdy!

    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
  • deSilvadeSilva Posts: 2,967
    edited 2008-03-03 18:45
    You still output the trailing zero, as Mike pointed out! Most likely unwanted...
  • mosquito56mosquito56 Posts: 387
    edited 2008-03-03 19:02
    Rokici, I think I've found· the problem.
    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
    ········
  • frankwillisfrankwillis Posts: 6
    edited 2012-04-04 19:15
    Question:
    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-04 20:29
    "write the variable as a string" ...

    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.
Sign In or Register to comment.