I’m experiencing several persistent issues with reading and writing files to the SD card on my OpenMV RT1062 camera. I’ve encountered multiple error types that seem related to storage reliability, and I’m hoping someone can help identify the root cause.
Issue 1: File Not Found Immediately After Saving
I’m saving a raw image to the SD card and then immediately trying to read it back, but I get a “Could not find the file” error. Here’s the relevant code:
```
try:
raw_path = f"{MY_IMAGE_DIR}/{my_addr}_{event_epoch_ms}_raw.jpg"
logger.debug(f"Saving raw image to {raw_path} : imbytesize = {len(img.bytearray())}")
img.save(raw_path)
utime.sleep_ms(500)
logger.info(f"Saved raw image: {raw_path}: raw size = {len(img.bytearray())} bytes")
except Exception as e:
logger.warning(f"[PIR] Failed to save raw image: {e}")
continue
# read raw file
try:
img = image.Image(raw_path)
imgbytes = img.bytearray()
logger.info(f"[PIR] Captured image, size: {len(imgbytes)} bytes")
except Exception as e:
logger.error(f"[PIR] Failed read image file: {e}")
continue
```
The logs show:
```
19:54:13,489 - INFO - [PIR] Motion detected - (interrupt) capturing image...
19:54:13,665 - INFO - Saved raw image: /sdcard/myimages/221_1381780453000_raw.jpg: raw size = 1843200 bytes
19:54:13,670 - ERROR - [PIR] Failed read image file: Could not find the file
The file appears to save successfully (1.8MB), but reading it back immediately fails. I’ve added a 500ms delay, but it doesn’t help.
Issue 2: EIO Error When Reading Encrypted Files
When trying to read previously saved encrypted files, I sometimes get an EIO (Input/Output Error):
```
try:
logger.info(f"[IMG] Reading encrypted image of creator: {creator}, file: {enc_filepath}")
with open(enc_filepath, "rb") as f:
enc_msgbytes = f.read()
logger.info(f"[IMG] Read encrypted image of creator: {creator}, file: {len(enc_msgbytes)} bytes")
except Exception as e:
logger.error(f"[IMG] Failed to read encrypted image from file, image re-queued {enc_filepath}, e: {e}")
imgpaths_to_send.append(img_entry)
break
```
Log output:
```
20:10:16,897 - INFO - [IMG] Reading encrypted image from file: /sdcard/myimages/221_1381780661000.enc
20:10:16,917 - ERROR - [IMG] Failed to read encrypted image from file, image re-queued /sdcard/myimages/221_1381780661000.enc, e: [Errno 5] EIO
```
**Issue 3: ENOENT Error (File Not Found)**
Sometimes the same code produces ENOENT errors for files that should exist:
```
10:40:39,240 - INFO - [IMG] Reading encrypted image of creator: 221, file: /sdcard/myimages/221_1381833132000.enc
10:40:39,247 - ERROR - [IMG] Failed to read encrypted image from file, image re-queued /sdcard/myimages/221_1381833132000.enc, e: [Errno 2] ENOENT
**Issue 4: Corrupted Log Output**
Occasionally, my log messages become severely corrupted with repeated special characters, making debugging difficult. Here's an example:
```
20:21:10,616 - INFO - [⋙ sending....] dest=65535, msg_typ:N, len:1 bytes, single packet
20:21:10,770 - INFO - [⮕ SENT to 65535] [*] 9 bytes, MSG_UID = b'N\xdd�������������������������... [thousands of corrupted characters continue]
