writing file to /sd from mpy and show it on USB

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
roland_vs
Posts: 89
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

writing file to /sd from mpy and show it on USB

Post by roland_vs » Wed Jan 04, 2017 8:07 pm

I'm probably overlooking something but:

If I'm dropping a file through USB on the disk, I can use it immediately within the mpy environment.

So now I have a program that writes a file 'FILE.TXT' to the '/sd' volume.
Closing , flushing and syncing does not show the file at the other side (USB side).
Also the file is gone after some action.

Did I overlook something?

Code: Select all

# at REPL - open file, write read , close
>>> f=open('/sd/FILE.TXT','rw')
>>> f.write("HELLO\r\n")
7
>>> f.flush()
>>> f.seek(0)
0
>>> f.read()
'HELLO\r\n'
>>> f.close()

# directory "/sd"
>>> os.listdir()
['._.Trashes', 'boot.py', '.Trashes', 'main.py', 'log_3638.csv', 'log_4523.csv', '._boot.py', 'templogger.py', 'cardreader.py', 'datalogger.py', '.Spotlight-V100', '._cardreader.py', '._datalogger.py', '._templogger.py', 'show_pins.py', 'LOGGER.py', 'lib', '._LOGGER.py', '.TemporaryItems', '.fseventsd', 'log_260356.csv', 'FILE.TXT', 'System Volume Information']
>>> 
# file is visible
# add a file "SDCARD" at the host to the PYB USB MASS STORAGE DEVICE (the other side)
>>> 
>>> os.listdir()
['._.Trashes', 'boot.py', '.Trashes', 'main.py', 'log_3638.csv', 'log_4523.csv', '._boot.py', 'templogger.py', 'cardreader.py', 'datalogger.py', '.Spotlight-V100', '._cardreader.py', '._datalogger.py', '._templogger.py', 'show_pins.py', 'LOGGER.py', 'lib', '._LOGGER.py', '.TemporaryItems', '.fseventsd', 'log_260356.csv', 'SDCARD', '._SDCARD', 'System Volume Information']
>>> 
# file is missing

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: writing file to /sd from mpy and show it on USB

Post by dhylands » Thu Jan 05, 2017 3:43 am

It's basically a problem with how USB Mass Storage works.

The host assumes it has exclusive access to the mass storage volume. It can choose to cache information from the volumes.

Now the pyboard comes along and writes data to the volume. The host thinks it has exclusive access, so if it has the directory cached then it won't see the new file, and if the host makes a change to the directory, it may very well flush that directory from its cache back out to the volume wiping out any changes that the pyboard made.

I always turn USB Mass storage off and use rshell to see files. Because rshell goes through the pyboard at the file level (rather than at the block level like USB Mass Storage does) you'll always get a consistent view of the files.

Post Reply