2017-11-25 4 views
-1

imはstm32f4のfatfsで苦労しています。何の問題もなく、私は、マウント・ファイルを作成し、でそれを書くことができます:char my_data[]="hello world"や窓にファイルが、私は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

コードに少なくとも2つの問題があります。

数字の文字列が短すぎます。 C文字列はnullバイトで終了します。したがって、presur_1は少なくとも7バイト(数字の場合は6、ヌルバイトの場合は1)である必要があります。 6バイトしかないので、sprintfは割り当てられた長さを越えて書き込みを行い、他のいくつかのデータを破棄します。

改行の文字列は、2文字の文字列(nullバイトに加えて)で初期化されます。ただし、ファイルに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); 
} 
関連する問題