6783|10

84

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

SD写函数写不进去,求助高手~~~~ [复制链接]

这里用的f_write是例程中自带的,应该没有错吧。可是不知道为什么就是写不进去。
例如试验如下:
A.txt原有内容是zyz
在超级终端输入命令:
write a.txt china
显示是写成功的,可是打开a.txt。里面的内容却不是china,而是原来的数据。哪位前辈帮帮忙忙看看?

int
Cmd_write(int argc, char *argv[])
{

    FRESULT fresult;
    unsigned short usBytesWrite=0;

         if(strlen(g_cCwdBuf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cTmpBuf))
    {
        UARTprintf("Resulting path name is too long\n");
        return(0);
    }
//这一句的作用是判定将要写的文件添加到当前工作路径后,路径长度会不会超过缓冲区大小。

        strcpy(g_cTmpBuf, g_cCwdBuf);
       //将当前路径拷贝到缓冲区。


    if(strcmp("/", g_cCwdBuf))
    {
        strcat(g_cTmpBuf, "/");
    }
    //在路径后面加上一个   /   分隔符
  

    strcat(g_cTmpBuf, argv[1]);
  //将要写的文件名添加到当前工作路径后面

    fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_WRITE);
        //打开文件,用来写。
       
    if(fresult != FR_OK)
    {        UARTprintf("open error\n");
        return(fresult);
        }
        //如果打开过程中有错,就返回一个错误。


         strcpy(g_cTmpBuf, argv[2]);       
        //将要写的内容拷贝到缓冲区   
   

     do
         {
     fresult = f_write(&g_sFileObject,g_cTmpBuf,sizeof(g_cTmpBuf) - 1,
                         &usBytesWrite);

        //写文件

        if(fresult != FR_OK)
        {
            UARTprintf("write error\n");
            return(fresult);
        }

        //
        // Null terminate the last block that was read to make it a
        // null terminated string that can be used with printf.
        //
   
                }while(usBytesWrite!=sizeof(g_cTmpBuf) - 1);
     //循环写入,直到写完。
        //
        // Print the last chunk of the file that was received.
        //
        UARTprintf("%s", g_cTmpBuf);


    //
    // Continue reading until less than the full number of bytes are
    // read.  That means the end of the buffer was reached/

    //
    // Return success.
    //
    return(0);

}

最新回复

我往sd卡里写BMP图片时,遇见个情况。就是bmp首部数据写不进去。不写首部数据只写图片数据的话,图片数据可以写进去。这是什么原因啊。不知道,老师了解吗  详情 回复 发表于 2017-10-23 16:43
 
点赞 关注

回复
举报

918

帖子

0

TA的资源

纯净的硅(中级)

沙发
 

回复 楼主 xielijuan 的帖子

我以前遇到过,注意:确定你打开新文件的时候,已经关闭了原来打开的文件,否则有可能出现这种问题。
 
 

回复

849

帖子

0

TA的资源

纯净的硅(高级)

板凳
 
顶。。的确如此,如果学了C语言的文件操作一定会明了的哈。
 
个人签名只有想不到,没有做不到。
 
 

回复

1412

帖子

15

TA的资源

版主

4
 
strcat(g_cTmpBuf, argv[1]);
  //
    // Open the file for reading.
    //
  //  fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_CREATE_NEW|FA_WRITE); //这句话表示创建一个文件
         fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_WRITE|FA_READ);   //这句话表示追加数据,一定要和FA_READ一起使用

    //
    // If there was some problem opening the file, then return
    // an error.
    //
    if(fresult != FR_OK)
    {       
            UARTprintf("open error\n");
        return(fresult);
    }


    fresult =  f_lseek(&g_sFileObject,g_sFileObject.fsize);  //在追加数据的时候,要把文件指针放到最后,也就是文件的末尾

     if(fresult != FR_OK)
    {       
            UARTprintf("lseek error\n");
        return(fresult);
    }


         strcpy(g_cTmpBuf, argv[2]);                  
   

     do
         {
  //   fresult = f_write(&g_sFileObject,"abcabcabcabcabcabcabcabcabcabcabcabc",36,&usBytesWrite);
          fresult = f_write(&g_sFileObject,g_cTmpBuf,sizeof(g_cTmpBuf) - 1,&usBytesWrite);
        //
        // If there was an error reading, then print a newline and
        // return the error to the user.
        //
        if(fresult != FR_OK)
        {
            UARTprintf("write error\n");
            return(fresult);
        }

        //
        // Null terminate the last block that was read to make it a
        // null terminated string that can be used with printf.
        //
        //g_cTmpBuf[usBytesWrite] = 0;
                }while(usBytesWrite!=sizeof(g_cTmpBuf) - 1);
        //
        // Print the last chunk of the file that was received.
        //
        UARTprintf("%s", g_cTmpBuf);


    //
    // Continue reading until less than the full number of bytes are
    // read.  That means the end of the buffer was reached.
    //

    //while(usBytesWrite == sizeof(g_cTmpBuf) - 1);

    //
    // Return success.
    //
        f_close(&g_sFileObject);  //关闭文件
    return(0);
 
个人签名https://bbs.eeworld.com.cn/thread-471646-1-1.html
欢迎加入我的团队
 
 

回复

1412

帖子

15

TA的资源

版主

5
 
注意三个地方:
1.打开文件的时候要用   FA_WRITE|FA_READ 的组合

2.  由于是追加文件,所以要用f_lseek(。。。)把文件指针放到整个文件的最后

3.最后要调用f_close(..)关闭打开的文件。
 
 
 

回复

918

帖子

0

TA的资源

纯净的硅(中级)

6
 

回复 4楼 youki12345 的帖子

谢谢,非常受益!
 
 
 

回复

84

帖子

0

TA的资源

一粒金砂(中级)

7
 

回复 5楼 youki12345 的帖子

啊哈~~谢谢老师~~~;P
 
 
 

回复

23

帖子

0

TA的资源

一粒金砂(中级)

8
 

同样的问题

这位老师分析的很精辟,但是我遇到了相同的问题,我想在串口中断发生的时候,在SD里新建一个txt文档,然后以后串口发生中断的时候,在txt里更新数据,但是始终不行,串口中断程序如下:

void USART2_IRQHandler(void)
{       
        char t;                                                                         
   //处理接收到的数据
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    {         
           /* Clear the USART1 Receive interrupt */


            t=USART_ReceiveData(USART2);
                  //com2_buffer[0]= 0x22;
        //        i=i+1;
                f_mount(0,&fs);       

                res = f_open( &fsrc , "0:/Demo.TXT" , FA_CREATE_NEW | FA_WRITE);               

            if ( res == FR_OK )
            {
      /* Write buffer to file */
                      res = f_write(&fsrc, com2_buffer, sizeof(com2_buffer), &br);     

          //                printf("Demo.TXT successfully created        \r\n");
   
              /*close file */
                      f_close(&fsrc);     
            }
            else if ( res == FR_EXIST )
            {               
                          f_open( &fsrc , "0:/Demo.TXT" ,FA_WRITE | FA_READ);
                          f_lseek(&fsrc,20);
                          f_write(&fsrc, com2_buffer, sizeof(com2_buffer), &br);
                          f_close(&fsrc);               
          //                printf("Demo.TXT created in the disk      \r\n");
            }
//                scan_files(path);
//                SD_TotalSize();
                USART_SendData(USART1,t);
        //        USART_SendData(USART1,com2_buffer[1]);
                   while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  
    }       

        if(USART_GetITStatus(USART2, USART_IT_RXNE) !=RESET)

        {            USART_ClearITPendingBit(USART2,USART_IT_RXNE); }//清中断标识
/*                 com2_buff[com2_receive_count++]= USART_ReceiveData(USART2);
                 for(i=0;i<=com2_receive_count;i++)
                 USART_SendData(USART1, com2_buff);
                 com2_buff[com2_receive_count]=0;
         //串口中断查询位,由此位可以知道串口2中有没有数据
                 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){}
                printf(" \r\n");        *?
        /*          com2_bit=1;
           
                 if(com2_receive_count>523)        //最大值处理,要与定义的一样才行
                          com2_receive_count=0;          */
         
         
       

         //溢出-如果发生溢出需要先读SR,再读DR寄存器 则可清除不断入中断的问题
    if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
    {
        USART_ClearFlag(USART2,USART_FLAG_ORE);    //读SR
        USART_ReceiveData(USART2);                //读DR
    }

}
注意:else if ( res == FR_EXIST ) 这一段 总是执行不了,只能在reset后才能执行以下。弄了好几天了 ,希望老师也能指导下,是不是open的模式不对啊?
 
 
 

回复

23

帖子

0

TA的资源

一粒金砂(中级)

9
 

补充下

我用的107vc,3.5的库0.08文件系统
 
 
 

回复

23

帖子

0

TA的资源

一粒金砂(中级)

10
 

补充以下

补充我的那个f_open文件:

FRESULT f_open (
        FIL *fp,                        /* Pointer to the blank file object */
        const TCHAR *path,        /* Pointer to the file name */
        BYTE mode                        /* Access mode and file open mode flags */
)
{
        FRESULT res;
        DIR dj;
        BYTE *dir;
        DEF_NAMEBUF;


        fp->fs = 0;                        /* Clear file object */

#if !_FS_READONLY
        mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
        res = chk_mounted(&path, &dj.fs, (BYTE)(mode & ~FA_READ));
#else
        mode &= FA_READ;
        res = chk_mounted(&path, &dj.fs, 0);
#endif
        INIT_BUF(dj);
        if (res == FR_OK)
                res = follow_path(&dj, path);        /* Follow the file path */
        dir = dj.dir;

#if !_FS_READONLY        /* R/W configuration */
        if (res == FR_OK) {
                if (!dir)        /* Current dir itself */
                        res = FR_INVALID_NAME;
#if _FS_SHARE
                else
                        res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
#endif
        }
        /* Create or Open a file */
        if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
                DWORD dw, cl;

                if (res != FR_OK) {                                        /* No file, create new */
                        if (res == FR_NO_FILE)                        /* There is no file to open, create a new entry */
#if _FS_SHARE
                                res = enq_lock(dj.fs) ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
#else
                                res = dir_register(&dj);
#endif
                        mode |= FA_CREATE_ALWAYS;                /* File is created */
                        dir = dj.dir;                                        /* New entry */
                }
                else {                                                                /* Any object is already existing */
                        if (mode & FA_CREATE_NEW) {                /* Cannot create new */
                                res = FR_EXIST;
                        } else {
                                if (dir[DIR_Attr] & (AM_RDO | AM_DIR))        /* Cannot overwrite it (R/O or DIR) */
                                        res = FR_DENIED;
                        }
                }
                if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) {        /* Truncate it if overwrite mode */
                        dw = get_fattime();                                        /* Created time */
                        ST_DWORD(dir+DIR_CrtTime, dw);
                        dir[DIR_Attr] = 0;                                        /* Reset attribute */
                        ST_DWORD(dir+DIR_FileSize, 0);                /* size = 0 */
                        cl = LD_CLUST(dir);                                        /* Get start cluster */
                        ST_CLUST(dir, 0);                                        /* cluster = 0 */
                        dj.fs->wflag = 1;
                        if (cl) {                                                        /* Remove the cluster chain if exist */
                                dw = dj.fs->winsect;
                                res = remove_chain(dj.fs, cl);
                                if (res == FR_OK) {
                                        dj.fs->last_clust = cl - 1;        /* Reuse the cluster hole */
                                        res = move_window(dj.fs, dw);
                                }
                        }
                }
        }
        else {        /* Open an existing file */
                if (res == FR_OK) {                                                /* Follow succeeded */
                        if (dir[DIR_Attr] & AM_DIR) {                /* It is a directory */
                                res = FR_NO_FILE;
                        } else {
                                if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
                                        res = FR_DENIED;
                        }
                }
        }
        if (res == FR_OK) {
                if (mode & FA_CREATE_ALWAYS)                        /* Set file change flag if created or overwritten */
                        mode |= FA__WRITTEN;
                fp->dir_sect = dj.fs->winsect;                        /* Pointer to the directory entry */
                fp->dir_ptr = dir;
#if _FS_SHARE
                fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
                if (!fp->lockid) res = FR_INT_ERR;
#endif
        }

#else                                /* R/O configuration */
        if (res == FR_OK) {                                        /* Follow succeeded */
                if (!dir) {                                                /* Current dir itself */
                        res = FR_INVALID_NAME;
                } else {
                        if (dir[DIR_Attr] & AM_DIR)        /* It is a directory */
                                res = FR_NO_FILE;
                }
        }
#endif
        FREE_BUF();

        if (res == FR_OK) {
                fp->flag = mode;                                        /* File access mode */
                fp->org_clust =        LD_CLUST(dir);                /* File start cluster */
                fp->fsize = LD_DWORD(dir+DIR_FileSize);        /* File size */
                fp->fptr = 0;                                                /* File pointer */
                fp->dsect = 0;
#if _USE_FASTSEEK
                fp->cltbl = 0;                                                /* No cluster link map table */
#endif
                fp->fs = dj.fs; fp->id = dj.fs->id;        /* Validate file object */
        }

        LEAVE_FF(dj.fs, res);
}
 
 
 

回复

8

帖子

0

TA的资源

一粒金砂(中级)

11
 
youki12345 发表于 2010-12-9 23:50
strcat(g_cTmpBuf, argv[1]);
  //
    // Open the file for reading.
    //
  //  fresult = f_open ...

我往sd卡里写BMP图片时,遇见个情况。就是bmp首部数据写不进去。不写首部数据只写图片数据的话,图片数据可以写进去。这是什么原因啊。不知道,老师了解吗
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表