Shop OBEX P1 Docs P2 Docs Learn Events
Problem with sd card read and write. — Parallax Forums

Problem with sd card read and write.

mosquito56mosquito56 Posts: 387
edited 2008-03-19 19:03 in Propeller 1
·· I am writing to a sd file the value of a counter. I am update the file about every 20 secs. As long a the value is less than 6 digits, ie:999999 there seems to be no problem. A 6 digit number is written as 8 characters with the cr,lf at the end. Since the problem seems to be 8bytes, I am wondering if it is a memory problem of some sort.
· I am using fsrw. I have included the code dirty as it is. I don't expect anyone to fix the code just throw out some ideas.
pri hobbswrite|r,y,digits
digits:=x:=1
       repeat while digits=<hobbsvalue
         digits:=digits*10
         x++
digits:=digits/10
r:=sdfat.mount(0)
r := sdfat.popen(string("value.txt"), "w")
 repeat y from 0 to x-2
   r:=hobbsvalue/digits
   if r => ("0") and r =< ("9")
     sdfat.pputc(r+48)
   'term.dec(r)
   byte[noparse][[/noparse]@infoin+y]:=r
   hobbsvalue:=hobbsvalue//digits
   digits:=digits/10
byte[noparse][[/noparse]@infoin[noparse][[/noparse]y+1]]:=$0D
'term.str(string("Infoin "))
'term.out($0D)
'term.str(@infoin)
'term.out($0D)
  repeat y from 0 to strsize(@infoin)
    r:=byte[noparse][[/noparse]@infoin][noparse][[/noparse]y]
     'term.hex(r,2)
     if r => ("0") and r =< ("9")
     sdfat.pputc(r+48)
sdfat.pputc($0D)
   
sdfat.pclose
hobbsread
'term.str(@infoin)
waitcnt(clkfreq*4+cnt)
pri hobbsread |r ,mult,y ,val
'out(0)
r:= sdfat.mount(0)
'term.hex(r,2)
x:=0
r := sdfat.popen(string("value.txt"), "r")
row:=2
col:=1
printstr(string("File is "))
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/100   
hobbsvalue:=0
  repeat y from 0 to x-2 
    r:=byte[noparse][[/noparse]@infoin[noparse][[/noparse]y]]
    val:=(r-48)*mult
    hobbsvalue:=hobbsvalue+val
    mult:=mult/10
 if hobbsvalue<hobbsold
   hobbsvalue:=hobbsold
 else
   hobbsold:=hobbsvalue
'term.out(0)  
'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
········

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-19 17:40
    You need to explain what behavior you're observing.
  • mosquito56mosquito56 Posts: 387
    edited 2008-03-19 18:04
    Basically the read is going back to 10. I let the file rewrite after 20 secs. The way I have it set up, it writes the file and then reads the file to doublecheck the update was correct. The hobbsvalue which is long is going to 0 and the file on the sd card is reading 10$0D in windows. I seem to be having alot of problems with writing 0's.

    hobbsvalue:=1000
    hobbswrite

    The screen prints 1. Is there something special about zeros?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet
    ········
  • RaymanRayman Posts: 14,223
    edited 2008-03-19 18:30
    Maybe this:
    hobbsvalue:=hobbsvalue//digits
    Should be:
    hobbsvalue:=hobbsvalue-r*digits
    Otherwise,
    you might just use this funciton from vga_text:
    PUB dec(value) | i
    '' Print a decimal number
      if value < 0
        -value
        out("-")
      i := 1_000_000_000
      repeat 10
        if value => i
          out(value / i + "0")
          value //= i
          result~~
        elseif result or i == 1
          out("0")
        i /= 10
    

    just replace "out" with "sdfat.pputc"
  • mosquito56mosquito56 Posts: 387
    edited 2008-03-19 18:50
    ·I would prefer not to put code in I don't understand

    ·out(value / i + "0") ?what is the "0" for?
    ····· value //= i······ ?//= in english?


    Can anyone explain these 2 lines?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet
    ········
  • RaymanRayman Posts: 14,223
    edited 2008-03-19 19:03
    "0" means the code for character "0", which is 48 in decimal.
    //= is the modulus operator... This is just the remainder after division (so 15//10=5)
Sign In or Register to comment.