Programmatically detecting the SD card
- This topic has 4 replies, 2 voices, and was last updated 15 years, 4 months ago by .
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- You cannot reply to this topic.
Home › Forums › MultiConnect OCG › Programmatically detecting the SD card
I am trying to find a reliable way to detect when an SD card is present. When I try to write to the SD card and it’s not inserted, it doesn’t cause any errors, which is odd.
Right now, I have to check the available space in the mounted location. Is there a more elegant way to determine whether or not there is an SD card inserted?
It doesn’t cause any errors when you write to /media/card because it is just a regular directory until a device is mounted at that location (which happens automatically when you insert the SD card).
You can see this behavior just by unmounting the filesystem (umount /media/card) and then inspecting the /media/card directory. If you create files in that directory when the SD card isn’t mounted, they will stay there and be visible whenever the card isn’t mounted.
You could read the file /proc/mounts which shows the currently mounted filesystems.
If the SD card is inserted on boot, there is no change in the file when the card is removed.
I’m not sure how to detect the SD card in this case.
When I have my SD inserted in my CDP on boot, the /media/card mount shows up in /proc/mounts. If I remove the card, it disappears from /proc/mounts pretty quickly.
Yours doesn’t do this?
I figured out why my CDP is behaving differently. I had a file open on the SD card, so it wasn’t unmounting properly on removal.
There might not be a good way to detect a removal if the card is currently in use. Under normal use, this shouldn’t ever happen though, so maybe isn’t not worth worrying about.