Shop OBEX P1 Docs P2 Docs Learn Events
Question about pwrite method in fsrw (the SD card object) — Parallax Forums

Question about pwrite method in fsrw (the SD card object)

ElectricAyeElectricAye Posts: 4,561
edited 2008-09-24 03:26 in Propeller 1
Hi all,

I've been working with the SD card object from the Object Exchange, fsrw.spin, and I have a basic question about how the pwrite method works.· As I understand it, the following bit of code

······· PwriteCheck1 := fsrw.pwrite(Simple_Numbers.dec(Frequency1),7)

would convert Frequency1 into a string, and then the pwrite method would write 7 bytes of that string into an already opened file on the SD card.· If 7 bytes were successfully written, then the value of PwriteCheck would be 7.· All of this would work fine so long as Frequency1 consists of 7 digits.

My question is this: what happens if the value of Frequency1 were to drop below or rise above 7 digits?· Does it write junk to the SD card file if Frequency1 is fewer than 7 digits?· And·what gets·chopped off if Frequency1 goes above 7 digits?· Must I put in some kind of test code that checks the size of Frequency1 so the number of requested "byte writes" matches the number of digits making up Frequency1? or can I just change the 7 into 20 so that I'm always guaranteed that pwrite will write enough bytes?

My question stems from my suspicion that I'm suffering from memory stomp of some kind.· And normally I would simply run countless trials of the above mentioned ideas and reverse engineer how pwrite works, but I've trashed some SD cards in the past with my software, so I'm trying to figure this out as much as I can without actually playing with SD cards.· blush.gif

many thanks,
Mark

Comments

  • Harrison.Harrison. Posts: 484
    edited 2008-09-23 19:27
    That shouldn't be causing any memory 'stomping' issues. sd.pwrite only performs memory reads and simple numbers shouldn't have any buffer overflow problems.

    You should replace your code with the following so you always write the correct amount of characters:
    PRI myMethod | str
      str := Simple_Numbers.dec(Frequency1)
      fsrw.pwrite(str,strsize(str))
    
    
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-09-23 22:54
    Harrison.

    Right you are! And I guess what I meant to say is that I'm worried that if I don't get the "byte write" to match, then perhaps the pwrite method would write some "magic garbage" onto the SD card and cause it to die. Of course, magic garbage by itself would not be enough to kill the SD card, but maybe if magic garbage was combined with another error on my part, it might write garbage into a sensitive part of the SD card that would kill it. I'm clueless about how SD cards work, so it was just a guess.

    I love your technique for getting the bytes to match. Being a very inexperienced clod, I would have treed my code with all sorts of IF statements. You saved me a lot of trouble.

    a thousand thanks!

    Mark smile.gif
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-09-24 03:10
    Harrison,

    I'm confused about what kind of variable str is.· Up until now I thought SPIN did not have string variables.· So would the str be a LONG?· If so, is it limited by how many characters are in the string?·

    baffled,
    confused.gif
    Mark
  • Harrison.Harrison. Posts: 484
    edited 2008-09-24 03:16
    The 'str' variable is a long. It stores the address of the HUB memory location where the string starts. The long does not actually store the string contents.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-09-24 03:26
    Harrison,

    you are truly awesome.· Thanks for explaining that to me and answering so quickly!· WOW!

    smile.gif
    Mark
Sign In or Register to comment.