2017-11-25 106 views
-1

Im在stm32f4上与fatfs挣扎。没问题,我可以安装,创建文件,并在其上写:char my_data[]="hello world"和Windows文件通常表明但是当我尝试使用代码loger:stm32f4 fatfs f_write whitemarks

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

回答

0

代码中至少有两个问题:

该数字的字符串太短。 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); 
}