FatFs文件中文支持移植出现FR_INVALID_NAME错误问题

2019-07-20 00:28发布

在FatFs文件系统移植后,为了添加对中文的支持,我这里用的是R0.13c最新版的版本。首先更改文件ffconf.h,宏定义FF_USE_LFN修改为3,其实这里更改为1也可以,如果修改为3的话,需要实现ffsystem.c文件中ff_memalloc()和ff_memfree()两个函数。然后宏定义FF_CODE_PAGE修改为936。按理说,到了这一步,应该没有什么问题了。编译也成功通过。main中代码如下:
  1. SDcard_Memory = (FATFS*)mymalloc(SRAMIN,sizeof(FATFS));

  2.         //挂载SD卡
  3.         res = f_mount(SDcard_Memory,"0:",1);
  4.         LCD_ShowString(50,190,400,30,24,"SD Card is ready!");

  5.         res = f_open(&file_1,"0:中文测试.txt",FA_CREATE_ALWAYS|FA_READ|FA_WRITE);
  6.         if(res)
  7.         {
  8.                 LCD_ShowNum(350,220,res,2,24);
  9.                 LCD_ShowString(50,220,400,30,24,"File Open Failed");
  10.         }
  11.         res = f_write(&file_1,str,sizeof(str),&bw);
  12.         if(res)
  13.         {
  14.                 LCD_ShowNum(350,250,res,2,24);
  15.                 LCD_ShowString(50,250,400,30,24,"File Write Failed");
  16.         }
  17.         f_lseek(&file_1,0);
  18.         res = f_read(&file_1,buff,sizeof(str),&br);
  19.         if(res)
  20.         {
  21.                 LCD_ShowNum(350,280,res,2,24);
  22.                 LCD_ShowString(50,280,400,30,24,"File Read Failed");
  23.         }
  24.         LCD_ShowString(50,310,400,30,24,buff);

  25.         f_close(&file_1);

  26.         Scan_files("0:",fl,&cnt);
  27.         for(i=0;i<cnt;i++)
  28.         {
  29.                 LCD_ShowNum(350,310,cnt,3,24);
  30.                 LCD_ShowString(50,340+i*20,400,20,16,fl[i].fname);
  31.         }
复制代码执行结果,第一个返回错误6,也就是FR_INVALID_NAME。后面两个返回9。
调试1:
       f_open(&file_1,"0:测试.txt",FA_CREATE_ALWAYS|FA_READ|FA_WRITE);  这个没有问题,成功执行。
调试2:
       f_open(&file_1,"0:测试中.txt",FA_CREATE_ALWAYS|FA_READ|FA_WRITE);出现错误6,不行
调试3:
       f_open(&file_1,"0:测试1234567890.txt",FA_CREATE_ALWAYS|FA_READ|FA_WRITE);这个也没有问题,成功执行


这里呢,"0:测试.txt"显示大小是char[13],而fname[FF_LFN_BUF + 1 ]中的FF_LFN_BUF+1正好也是13,这是不是巧合。为什么多添加一个汉字都不行。而在后面添加数字或者字母却是可以的。


然后,把SD卡拆下来,拿到电脑上查看,文件确实有,但是显示乱码。


ffconf.h文件部分代码如下:
  1. /*---------------------------------------------------------------------------/
  2. / Locale and Namespace Configurations
  3. /---------------------------------------------------------------------------*/

  4. #define FF_CODE_PAGE        936
  5. /* This option specifies the OEM code page to be used on the target system.
  6. /  Incorrect code page setting can cause a file open failure.
  7. /
  8. /   437 - U.S.
  9. /   720 - Arabic
  10. /   737 - Greek
  11. /   771 - KBL
  12. /   775 - Baltic
  13. /   850 - Latin 1
  14. /   852 - Latin 2
  15. /   855 - Cyrillic
  16. /   857 - Turkish
  17. /   860 - Portuguese
  18. /   861 - Icelandic
  19. /   862 - Hebrew
  20. /   863 - Canadian French
  21. /   864 - Arabic
  22. /   865 - Nordic
  23. /   866 - Russian
  24. /   869 - Greek 2
  25. /   932 - Japanese (DBCS)
  26. /   936 - Simplified Chinese (DBCS)
  27. /   949 - Korean (DBCS)
  28. /   950 - Traditional Chinese (DBCS)
  29. /     0 - Include all code pages above and configured by f_setcp()
  30. */


  31. #define FF_USE_LFN                3
  32. #define FF_MAX_LFN                255
  33. /* The FF_USE_LFN switches the support for LFN (long file name).
  34. /
  35. /   0: Disable LFN. FF_MAX_LFN has no effect.
  36. /   1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
  37. /   2: Enable LFN with dynamic working buffer on the STACK.
  38. /   3: Enable LFN with dynamic working buffer on the HEAP.
  39. /
  40. /  To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
  41. /  requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
  42. /  additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
  43. /  The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
  44. /  be in range of 12 to 255. It is recommended to be set 255 to fully support LFN
  45. /  specification.
  46. /  When use stack for the working buffer, take care on stack overflow. When use heap
  47. /  memory for the working buffer, memory management functions, ff_memalloc() and
  48. /  ff_memfree() in ffsystem.c, need to be added to the project. */


  49. #define FF_LFN_UNICODE        0
  50. /* This option switches the character encoding on the API when LFN is enabled.
  51. /
  52. /   0: ANSI/OEM in current CP (TCHAR = char)
  53. /   1: Unicode in UTF-16 (TCHAR = WCHAR)
  54. /   2: Unicode in UTF-8 (TCHAR = char)
  55. /   3: Unicode in UTF-32 (TCHAR = DWORD)
  56. /
  57. /  Also behavior of string I/O functions will be affected by this option.
  58. /  When LFN is not enabled, this option has no effect. */


  59. #define FF_LFN_BUF                255
  60. #define FF_SFN_BUF                12
  61. /* This set of options defines size of file name members in the FILINFO structure
  62. /  which is used to read out directory items. These values should be suffcient for
  63. /  the file names to read. The maximum possible length of the read file name depends
  64. /  on character encoding. When LFN is not enabled, these options have no effect. */


  65. #define FF_STRF_ENCODE        3
  66. /* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(),
  67. /  f_putc(), f_puts and f_printf() convert the character encoding in it.
  68. /  This option selects assumption of character encoding ON THE FILE to be
  69. /  read/written via those functions.
  70. /
  71. /   0: ANSI/OEM in current CP
  72. /   1: Unicode in UTF-16LE
  73. /   2: Unicode in UTF-16BE
  74. /   3: Unicode in UTF-8
  75. */


  76. #define FF_FS_RPATH                0
  77. /* This option configures support for relative path.
  78. /
  79. /   0: Disable relative path and remove related functions.
  80. /   1: Enable relative path. f_chdir() and f_chdrive() are available.
  81. /   2: f_getcwd() function is available in addition to 1.
  82. */
复制代码有过类似情况的朋友和原子哥,能否提供一下解决方案。谢谢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
4条回答
lyj41801
1楼-- · 2019-07-20 05:08
 精彩回答 2  元偷偷看……
lyj41801
2楼-- · 2019-07-20 09:43
哦,对了,在电脑上读呢,只有文件名是乱码,但是文件内容中文没有乱码。
正点原子
3楼-- · 2019-07-20 15:06
可以参考下我们例程
lyj41801
4楼-- · 2019-07-20 19:29
 精彩回答 2  元偷偷看……

一周热门 更多>