【问题标题】:stm32f4 fatfs f_write whitemarksstm32f4 fatfs f_write whitemarks
【发布时间】:2017-11-25 20:16:10
【问题描述】:

我正在为 stm32f4 上的 fatfs 苦苦挣扎。没问题我可以挂载,创建文件并通过:char my_data[]="hello world" 在其上写入,并且在 Windows 文件中正常显示,但是当我尝试使用代码作为记录器时:

float bmp180Pressure=1000.1;
char presur_1[6];//bufor znakow do konwersji
sprintf(presur_1,"%0.1f",bmp180Pressure);
char new_line[]="\n\r";

if(f_mount(&myFat, SDPath, 1)== FR_OK)
{
    f_open(&myFile, "dane.txt", FA_READ|FA_WRITE); 
    f_lseek(&myFile, f_size(&myFile));//sets end of data
    f_write(&myFile, presur_1, 6, &byteCount);
    f_write(&myFile, new_line,4, &byteCount);
    f_close(&myFile);
    HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
}

当我从计算机读取时,我有:top : notepad ++ buttom :windows notepad

【问题讨论】:

    标签: stm32 fatfs


    【解决方案1】:

    你的代码至少有两个问题:

    号码的字符串太短。 C 字符串以空字节结束。所以presur_1 至少需要 7 个字节长(6 代表数字,1 代表空字节)。由于它只有 6 个字节长,sprintf 将写入超出分配的长度并破坏其他一些数据。

    换行符的字符串由 2 个字符的字符串(加上空字节)初始化。但是,您将 4 个字符写入文件。所以除了换行符之外,文件中还会有一个 NUL 字符和一个垃圾字节。

    固定代码如下所示:

    float bmp180Pressure = 1000.1;
    char presur_1[20];//bufor znakow do konwersji
    int presur_1_len = sprintf(presur_1,"%0.1f\n\r",bmp180Pressure);
    
    if(f_mount(&myFat, SDPath, 1)== FR_OK)
    {
        f_open(&myFile, "dane.txt", FA_READ|FA_WRITE); 
        f_lseek(&myFile, f_size(&myFile));//sets end of data
        f_write(&myFile, presur_1, presur_1_len, &byteCount);
        f_close(&myFile);
        HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 2020-06-04
      • 2017-03-20
      • 2023-04-08
      • 2020-06-05
      • 2018-12-31
      • 2016-10-20
      • 2019-06-03
      • 2022-08-11
      相关资源
      最近更新 更多