|
想问问大佬们就是我的电路板在U盘插上去的时候会写文件进去,之后就不写了,我想实现把U盘插上去一直写,或者隔一段时间再写
我的usb状态代码是这样的,我应该怎么改才能让他一直写啊
void UsbTest(void)
{
switch(Appli_state)
{
case APPLICATION_READY:
MSC_Application();
Appli_state = APPLICATION_DISCONNECT;
break;
case APPLICATION_DISCONNECT:
f_mount(NULL, (const TCHAR*)"",0);
break;
default:
break;
}
}
这个 UsbTest()调用在main函数里面的while循环,MSC_Application函数是这样写的
static void MSC_Application(void) {
FRESULT res;
uint32_t byteswritten; //file write/read counts
char TimeStr[20];
char dateStr[20];
RTC_TimeTypeDef time;
RTC_DateTypeDef date;
// 获取当前时间
HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
sprintf(TimeStr,"%02d-%02d-%02d-%02d-%02d-%02d",
date.Year,
date.Month,
date.Date,
time.Hours,
time.Minutes,
time.Seconds);
res = f_mount(&USBHFatFS,(const TCHAR*)USBHPath,1);//挂载U盘
if(res != FR_OK)
{//挂载失败
Error_Handler();
printf("U盘挂载失败!!!\r\n");
}
else
{
strcpy(filename, TimeStr);
strcat(filename, ".csv"); // 以时间为名的文件创建
res=f_open(&USBHFile,filename,FA_CREATE_ALWAYS | FA_WRITE);//创建文件
if(res != FR_OK)
{
Error_Handler();
printf("创建文件失败!!!\r\n");
}
else
{
res= f_lseek(&USBHFile,f_size(&USBHFile));
res = f_write(&USBHFile,"日期,当前时间,堆电压,堆电流\n", sizeof("日期,当前时间,堆电压,堆电流\n"), &byteswritten);
if(res != FR_OK)
{
Error_Handler();
printf(" write file error : %d\r\n",res);
}
res =f_close(&USBHFile);
// f_mount(NULL, USBHPath, 0);///* 卸载文件系统*/
}
}
}
|
|