0

MCU - stm32f407vgt6
IDE -True Studio
附加 -CubeMx

说明-
我正在尝试将文件从 USB 驱动器复制到具有不同名称的同一个 USB 驱动器。

源文件 - FILE2.txt- 此文件存在于驱动器中,大小为 3KB
目标文件 - file2copy.txt- 此文件将在同一驱动器中创建,FILE2.txt并将复制的内容。

代码-

int CopyFile(char *srcFile, char *destFile)
{
    FATFS fs0;
    FIL fsrc, fdest;
    BYTE buffer[4096];
    FRESULT res;
    UINT br, bw;

    f_mount(&fs0, USBHPath, 0);

    res = f_open(&fsrc, (const TCHAR*)srcFile, FA_READ | FA_OPEN_EXISTING);
    if (res)    return 0;
    else
    {
        Green_Blink(100);
        res = f_open(&fdest, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
        if (res)   return 0;
        else
        {
            Green_Blink(100);
            for (;;) {
                res = f_read(&fsrc, buffer, sizeof buffer, &br);  /* Read a chunk of source file */
                if (res || br == 0) break; /* error or eof */
                res = f_write(&fdest, buffer, br, &bw);            /* Write it to the destination file */
                if (res || bw < br) break; /* error or disk full */
                f_sync(&fdest);
            }
        }
    }
    f_close(&fsrc);
    f_close(&fdest);

    f_mount(0, USBHPath, 0);

    return 1;
}

错误-
我能够打开源文件,但无法在驱动器中创建目标文件。

res = f_open(&fdest, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
if (res)   return 0;

res 在这种情况下变为真。

所以我的问题是我如何在同一个逻辑驱动器中复制文件以及在该驱动器中打开目标文件有什么问题。

提前致谢。

4

1 回答 1

1

首先,我不知道您的堆栈设置,但变量FATFS fs0;BYTE buffer[4096];堆栈上的位置可能很重。确保堆栈大小超过 4096 +FF_MAX_SS

其次,你得到什么样的错误?这个枚举应该很有用:

 typedef enum {
    FR_OK = 0,              /* (0) Succeeded */
    FR_DISK_ERR,            /* (1) A hard error occurred in the low level disk I/O layer */
    FR_INT_ERR,             /* (2) Assertion failed */
    FR_NOT_READY,           /* (3) The physical drive cannot work */
    FR_NO_FILE,             /* (4) Could not find the file */
    FR_NO_PATH,             /* (5) Could not find the path */
    FR_INVALID_NAME,        /* (6) The path name format is invalid */
    FR_DENIED,              /* (7) Access denied due to prohibited access or directory full */
    FR_EXIST,               /* (8) Access denied due to prohibited access */
    FR_INVALID_OBJECT,      /* (9) The file/directory object is invalid */
    FR_WRITE_PROTECTED,     /* (10) The physical drive is write protected */
    FR_INVALID_DRIVE,       /* (11) The logical drive number is invalid */
    FR_NOT_ENABLED,         /* (12) The volume has no work area */
    FR_NO_FILESYSTEM,       /* (13) There is no valid FAT volume */
    FR_MKFS_ABORTED,        /* (14) The f_mkfs() aborted due to any problem */
    FR_TIMEOUT,             /* (15) Could not get a grant to access the volume within defined period */
    FR_LOCKED,              /* (16) The operation is rejected according to the file sharing policy */
    FR_NOT_ENOUGH_CORE,     /* (17) LFN working buffer could not be allocated */
    FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
    FR_INVALID_PARAMETER    /* (19) Given parameter is invalid */
} FRESULT;

并且,尽量不要同时打开两个文件:

int CopyFile(char *srcFile, char *destFile)
{
    FATFS fs0;
    FIL file;
    BYTE buffer[4096];
    FRESULT res;
    UINT br = 0, bw = 0;

    f_mount(&fs0, USBHPath, 0);

    res = f_open(&file, (const TCHAR*)srcFile, FA_READ | FA_OPEN_EXISTING);
    if (res){
        f_mount(0, USBHPath, 0);
        return 0;
    }

    f_read(&file, buffer, sizeof(buffer), &br);  /* Read a chunk of source file */
    f_close(&file);

    if(br) {
        Green_Blink(100);

        res = f_open(&file, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
        if (res) {
            f_mount(0, USBHPath, 0);
            return 0;
        }

        Green_Blink(100);

        f_write(&file, buffer, br, &bw); /* Write it to the destination file */
        f_close(&file);

        if(!bw) {
            f_mount(0, USBHPath, 0);
            return 0;
        }

    }

    f_mount(0, USBHPath, 0);

    return 1;
}
于 2018-09-09T05:14:03.897 回答