初级会员

- 积分
- 100
- 金钱
- 100
- 注册时间
- 2019-5-5
- 在线时间
- 16 小时
|
5金钱
在FatFs文件系统移植后,为了添加对中文的支持,我这里用的是R0.13c最新版的版本。首先更改文件ffconf.h,宏定义FF_USE_LFN修改为3,其实这里更改为1也可以,如果修改为3的话,需要实现ffsystem.c文件中ff_memalloc()和ff_memfree()两个函数。然后宏定义FF_CODE_PAGE修改为936。按理说,到了这一步,应该没有什么问题了。编译也成功通过。main中代码如下:- SDcard_Memory = (FATFS*)mymalloc(SRAMIN,sizeof(FATFS));
- //挂载SD卡
- res = f_mount(SDcard_Memory,"0:",1);
- LCD_ShowString(50,190,400,30,24,"SD Card is ready!");
- res = f_open(&file_1,"0:中文测试.txt",FA_CREATE_ALWAYS|FA_READ|FA_WRITE);
- if(res)
- {
- LCD_ShowNum(350,220,res,2,24);
- LCD_ShowString(50,220,400,30,24,"File Open Failed");
- }
- res = f_write(&file_1,str,sizeof(str),&bw);
- if(res)
- {
- LCD_ShowNum(350,250,res,2,24);
- LCD_ShowString(50,250,400,30,24,"File Write Failed");
- }
- f_lseek(&file_1,0);
- res = f_read(&file_1,buff,sizeof(str),&br);
- if(res)
- {
- LCD_ShowNum(350,280,res,2,24);
- LCD_ShowString(50,280,400,30,24,"File Read Failed");
- }
- LCD_ShowString(50,310,400,30,24,buff);
- f_close(&file_1);
- Scan_files("0:",fl,&cnt);
- for(i=0;i<cnt;i++)
- {
- LCD_ShowNum(350,310,cnt,3,24);
- LCD_ShowString(50,340+i*20,400,20,16,fl[i].fname);
- }
复制代码 执行结果,第一个返回错误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文件部分代码如下:
- /*---------------------------------------------------------------------------/
- / Locale and Namespace Configurations
- /---------------------------------------------------------------------------*/
- #define FF_CODE_PAGE 936
- /* This option specifies the OEM code page to be used on the target system.
- / Incorrect code page setting can cause a file open failure.
- /
- / 437 - U.S.
- / 720 - Arabic
- / 737 - Greek
- / 771 - KBL
- / 775 - Baltic
- / 850 - Latin 1
- / 852 - Latin 2
- / 855 - Cyrillic
- / 857 - Turkish
- / 860 - Portuguese
- / 861 - Icelandic
- / 862 - Hebrew
- / 863 - Canadian French
- / 864 - Arabic
- / 865 - Nordic
- / 866 - Russian
- / 869 - Greek 2
- / 932 - Japanese (DBCS)
- / 936 - Simplified Chinese (DBCS)
- / 949 - Korean (DBCS)
- / 950 - Traditional Chinese (DBCS)
- / 0 - Include all code pages above and configured by f_setcp()
- */
- #define FF_USE_LFN 3
- #define FF_MAX_LFN 255
- /* The FF_USE_LFN switches the support for LFN (long file name).
- /
- / 0: Disable LFN. FF_MAX_LFN has no effect.
- / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
- / 2: Enable LFN with dynamic working buffer on the STACK.
- / 3: Enable LFN with dynamic working buffer on the HEAP.
- /
- / To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
- / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
- / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
- / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
- / be in range of 12 to 255. It is recommended to be set 255 to fully support LFN
- / specification.
- / When use stack for the working buffer, take care on stack overflow. When use heap
- / memory for the working buffer, memory management functions, ff_memalloc() and
- / ff_memfree() in ffsystem.c, need to be added to the project. */
- #define FF_LFN_UNICODE 0
- /* This option switches the character encoding on the API when LFN is enabled.
- /
- / 0: ANSI/OEM in current CP (TCHAR = char)
- / 1: Unicode in UTF-16 (TCHAR = WCHAR)
- / 2: Unicode in UTF-8 (TCHAR = char)
- / 3: Unicode in UTF-32 (TCHAR = DWORD)
- /
- / Also behavior of string I/O functions will be affected by this option.
- / When LFN is not enabled, this option has no effect. */
- #define FF_LFN_BUF 255
- #define FF_SFN_BUF 12
- /* This set of options defines size of file name members in the FILINFO structure
- / which is used to read out directory items. These values should be suffcient for
- / the file names to read. The maximum possible length of the read file name depends
- / on character encoding. When LFN is not enabled, these options have no effect. */
- #define FF_STRF_ENCODE 3
- /* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(),
- / f_putc(), f_puts and f_printf() convert the character encoding in it.
- / This option selects assumption of character encoding ON THE FILE to be
- / read/written via those functions.
- /
- / 0: ANSI/OEM in current CP
- / 1: Unicode in UTF-16LE
- / 2: Unicode in UTF-16BE
- / 3: Unicode in UTF-8
- */
- #define FF_FS_RPATH 0
- /* This option configures support for relative path.
- /
- / 0: Disable relative path and remove related functions.
- / 1: Enable relative path. f_chdir() and f_chdrive() are available.
- / 2: f_getcwd() function is available in addition to 1.
- */
复制代码 有过类似情况的朋友和原子哥,能否提供一下解决方案。谢谢
|
最佳答案
查看完整内容[请看2#楼]
经过这两三天的折磨,我利用官方的文件一个一个替换掉我自己的源文件,来回测试,最后替换掉main.c文件就不行了。然后我仔细核对了main.c文件中每一个语句,我都发现我的main和官方的main一字不差,偏偏就是我的调试不成功。最后,重点在这里:我编辑文件用的VScode,编码是UTF-8,官方的编码格式是GB2312也就是GBK格式,偏偏在KEIL里面是看不出来任何异常的。当我拿两份文件到另外一个编辑器中对比,我发现我的文件的中文文字部分 ...
|