Usb 在同一逻辑驱动器中使用FATF复制文件

Usb 在同一逻辑驱动器中使用FATF复制文件,usb,stm32,stm32f4discovery,stm32f4,fatfs,Usb,Stm32,Stm32f4discovery,Stm32f4,Fatfs,MCU-stm32f407vgt6IDE-True StudioAdditional-CubeMx 描述-我正在尝试将文件从USB驱动器复制到具有不同名称的同一USB驱动器 源文件-FILE2.txt-此文件存在于驱动器中,大小为3KB 目标文件-file2copy.txt-将在同一驱动器中创建此文件,并复制FILE2.txt的内容 代码- 在这种情况下,res变为true 所以我的问题是如何在同一逻辑驱动器中复制文件,以及在该驱动器中打开目标文件有什么问题 提前感谢。首先,我不知道您的堆栈设置

MCU-stm32f407vgt6IDE-True StudioAdditional-CubeMx

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

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

代码-

在这种情况下,res变为true

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

提前感谢。

首先,我不知道您的堆栈设置,但知道变量FATFS fs0;字节缓冲区[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;
}
检查_FS_LOCK常量的值。
 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;
}