SD Card becoming corrupted

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
kbrenner
Posts: 46
Joined: Mon Jan 20, 2020 8:05 pm

SD Card becoming corrupted

Post by kbrenner » Wed Jun 02, 2021 3:11 pm

Hi,

I have an SD card mounted to the PYBD-SF6W and am writing data to it (.DAP files, .txt files, and .json files); however, it is getting corrupted quite often. Here is a picture of what the corrupted files look like:
Corrupted SD Card.PNG
Corrupted SD Card.PNG (13.8 KiB) Viewed 9203 times
The steps I am taking are as follows:

In my boot script, I am writing:
sd = pyb.SDCard()
os.mount(sd, '/sd')
pyb.usb_mode('VCP+MSC',msc=(pyb.Flash(), pyb.SDCard()))

In my main script, I am writing:
f = open('<file_name>.txt','w')
f.write(<string contents>)
f.close()

Then when I want to go read the contents, I am writing:
f = open('<file_name>.txt','r')
var = f.read()
f.close()

Does anyone have an explanation for the file corruption? I am ensuring that I properly/safely eject both the Pyboard and SD card before unplugging from my PC.

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

Re: SD Card becoming corrupted

Post by dhylands » Wed Jun 02, 2021 6:38 pm

If you have MSC enabled AND you write to a volume that is MSC mounted, then you will see corruptions. This is because the PC with the MSC volume mounted thinks that it has exclusive access to the volume and will happily overwrite any changes that micropython tries to write.

I disable MSC and use rshell to copy files in and out, and then I don't see any corruptions.

Post Reply