Shop OBEX P1 Docs P2 Docs Learn Events
Opening multiple files with fsrw 2.6 — Parallax Forums

Opening multiple files with fsrw 2.6

I have been using fsrw 2.6 for many years, but I have often had issues with multiple files. I am trying to do the following.

Use one instance to traverse the root directory.
Use a second instance to open an 'in' file for data input.
Use a third instance to open an 'out' file for data output.

I don't know what filenames will be on the SD card, so I use the first instance to find each 'in' file, ignore any previous 'out' files, and be able to write to new 'out' files one at a time until there are no more files to be read.

The 'in' filename begins with "VEC_".
The 'out' filename begins with "OUT_".
The last 4 characters remain the same.

There doesn't seem to be any problems with 2 instance of fsrw2.6. As soon as the third instance is introduced, strange things happen to unrelated variables in the program (not shown).

Is there something about fsrw2.6 that I need to do to preserve the integrity of memory when more than 2 instances are used?

In this code snippet, characters are read from one file and written to another to simplify the example.
CON
  dir         = 0                  
  in          = 1                   ' InputFile         
  out         = 2                   ' OutputFile        

OBJ             
  sd[3]         : "fsrw26"

PRI Main | c
  sd[dir].mount(SDCard_DO)
  sd[dir].opendir
  repeat while NextFile
    repeat
      c := sd[in].pgetc
      if c <> -1
        sd[out].pputc(c)
    until c == -1
  sd[in].pclose
  sd[out].pclose
  sd[dir].unmount                       

PRI NextFile | i                                        
  repeat                                                ' Ignore filenames starting with "SIM"
    i := sd[dir].NextFile(@InputFile)                   ' Return the next filename in the directory
    if i == -1                                                                                                           
      return false
    bytemove(@st,@InputFile,4)                          
  until strcomp(@st,string("VEC_"))==true               
  i := sd[in].popen(@InputFile,"r")                     ' Close any open file and open new file
  if i == -1
    return false
  bytemove(@OutputFile+4,@InputFile+4,4)                
  i := sd[out].popen(@OutputFile,"w")                    ' Close any open file and open new file
  if i == -1
    return false
  return true

DAT
  InputFile  byte  "            .TXT",0      
  OutputFile byte  "OUT_    .TXT",0
  st         byte  "    ",0                             ' Comparison buffer

Comments

  • dbpage wrote: »
    I have been using fsrw 2.6 for many years, but I have often had issues with multiple files.
    fsrw 2.6 Copyright 2009  Tomas Rokicki and Jonathan Dummer
    '
    '   See end of file for terms of use.
    '
    '   This object provides FAT16/32 file read/write access on a block device.
    '   Only one file open at a time.  Open modes are 'r' (read), 'a' (append),
    '   'w' (write), and 'd' (delete).  Only the root directory is supported.
    '   No long filenames are supported.  We also support traversing the
    '   root directory.
    

    Per the above, you can open only one file at a time.
    I use SD-MMC_FATEngine to handle up to 3 open files (1 read, 2 write) at a time without issues.

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2018-12-19 19:37
    @wmosscrop,
    Multiple instances, not multiple files per instance. Good comments in that linked thread, from the authors of fsrw.

    About the issue at hand, I feel it is probably not fsrw per se at fault with, "strange things happen to unrelated variables in the program (not shown)."

  • Could it be that unrelated byte variables in other objects could cause a misalignment with fsrw2.6 byte buffers?
    CON
    
    '  Buffering:  two sector buffers.  These two buffers must be longword
    '  aligned!  To ensure this, make sure they are the first byte variables
    '  defined in this object.
    '
       byte buf[SECTORSIZE] ' main data buffer
    
    DAT
      buf2 byte 0[SECTORSIZE]  ' main metadata buffer
    
  • Someone says magic word fsrw 2.6. I made a data acq system a while back but could never figure out how to insert time and date. Examples had time and date hard coded in file buffer which worked fine BUT if those were changed things didn't work in various ways. Is there perhaps some form of checksum being done somewhere?
  • Problem solved. It was a personal problem. fsrw26 was not at fault.

    I accidentally deleted an instruction to initialize a variable in a method outside of fsrw26. Multiple file access using multiple instances of fsrw16 as described in the first post works perfectly.

    sidecar-racer,
    TTA created fsrw26B to enable time and date functions. I don't know who TTA is, and I can't find it in OBEX. Find it embedded in Tracy Allen's reply at https://forums.parallax.com/discussion/164472/fsrw-modified-date-not-working. This is the version I am using.
            v2.6A  TTA  formatting, comments moved under method, some edited.  Methods capitalized
            v2.6B TTA nextfile method captures the create and modified datetimes as longs,
                      methods GetCreated and GetModified return those datetimes as longs.
                      Popen(s, mode) updates the created date location only when file is opened as mode = "w".
                      The "modified" date/time location is updated on either mode="a" or mode="w".
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2018-12-20 21:24
    TTA is me. (Thomas Tracy, I go by Tracy, unless I'm traveling in Spanish speaking countries, in which case I become Tomás, so as not to be confused with trece, which is the number 13.)

    Sidecar-racer, I'm not exactly sure what you mean by, "figure out how to insert time and date". That might refer to the creation and modification date as they exist in the file directory, which is what I implemented in the link dbpage is referring to.


  • Tracy Allen,
    Thank you for all your help to forum members over the years, including myself. In particular, fsrw26B has been great a help to me.
    Dennis
  • Thumbs up--Glad to hear it!
  • Tracy, thank you much, I may not have been using lastest modules, could have been 2.6 (earlier), I'll have to pull system out of box and look. As far as time and date, I was using GPS data with formatting to match what I saw in 2.6 code. Even if I entered any change of date or time stamp in example the write failed. The whole project; code and all are on here somewhere, but I do have it on my systems as well. I'm guessing search for sidecar; Merry Christmas all.
  • fsrw26B uses time/date in FAT format. FAT has a resolution of 2 seconds.

    In spin, here is how I convert time/date into FAT format:
    FATdate := ((s >> 1) | (m << 5) | (h << 11)) | (dd << 16 + (mm << 21) + ((yy+20) << 25))
    where:
    s = seconds
    m = minutes
    h = hours
    dd = day
    mm = month
    yy = year
    
Sign In or Register to comment.