2017-11-25 2 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 문자열은 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); 
}