这里用的f_write是例程中自带的,应该没有错吧。可是不知道为什么就是写不进去。
例如试验如下:
A.txt原有内容是zyz
在超级终端输入命令:
write a.txt china
显示是写成功的,可是打开a.txt。里面的内容却不是china,而是原来的数据。哪位前辈帮帮忙忙看看?
int
Cmd_write(int argc, char *argv[])
{
FRESULT fresult;
unsigned short usBytesWrite=0;
if(strlen(g_cCwdBuf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cTmpBuf))
{
UARTprintf("Resulting path name is too long
");
return(0);
}
//这一句的作用是判定将要写的文件添加到当前工作路径后,路径长度会不会超过缓冲区大小。
strcpy(g_cTmpBuf, g_cCwdBuf);
//将当前路径拷贝到缓冲区。
if(strcmp("/", g_cCwdBuf))
{
strcat(g_cTmpBuf, "/");
}
//在路径后面加上一个 / 分隔符
strcat(g_cTmpBuf, argv[1]);
//将要写的文件名添加到当前工作路径后面
fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_WRITE);
//打开文件,用来写。
if(fresult != FR_OK)
{ UARTprintf("open error
");
return(fresult);
}
//如果打开过程中有错,就返回一个错误。
strcpy(g_cTmpBuf, argv[2]);
//将要写的内容拷贝到缓冲区
do
{
fresult = f_write(&g_sFileObject,g_cTmpBuf,sizeof(g_cTmpBuf) - 1,
&usBytesWrite);
//写文件
if(fresult != FR_OK)
{
UARTprintf("write error
");
return(fresult);
}
//
// Null terminate the last block that was read to make it a
// null terminated string that can be used with printf.
//
}while(usBytesWrite!=sizeof(g_cTmpBuf) - 1);
//循环写入,直到写完。
//
// Print the last chunk of the file that was received.
//
UARTprintf("%s", g_cTmpBuf);
//
// Continue reading until less than the full number of bytes are
// read. That means the end of the buffer was reached/
//
// Return success.
//
return(0);
}
此帖出自
小平头技术问答
void USART2_IRQHandler(void)
{
char t;
//处理接收到的数据
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* Clear the USART1 Receive interrupt */
t=USART_ReceiveData(USART2);
//com2_buffer[0]= 0x22;
// i=i+1;
f_mount(0,&fs);
res = f_open( &fsrc , "0:/Demo.TXT" , FA_CREATE_NEW | FA_WRITE);
if ( res == FR_OK )
{
/* Write buffer to file */
res = f_write(&fsrc, com2_buffer, sizeof(com2_buffer), &br);
// printf("Demo.TXT successfully created ");
/*close file */
f_close(&fsrc);
}
else if ( res == FR_EXIST )
{
f_open( &fsrc , "0:/Demo.TXT" ,FA_WRITE | FA_READ);
f_lseek(&fsrc,20);
f_write(&fsrc, com2_buffer, sizeof(com2_buffer), &br);
f_close(&fsrc);
// printf("Demo.TXT created in the disk ");
}
// scan_files(path);
// SD_TotalSize();
USART_SendData(USART1,t);
// USART_SendData(USART1,com2_buffer[1]);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
if(USART_GetITStatus(USART2, USART_IT_RXNE) !=RESET)
{ USART_ClearITPendingBit(USART2,USART_IT_RXNE); }//清中断标识
/* com2_buff[com2_receive_count++]= USART_ReceiveData(USART2);
for(i=0;i<=com2_receive_count;i++)
USART_SendData(USART1, com2_buff);
com2_buff[com2_receive_count]=0;
//串口中断查询位,由此位可以知道串口2中有没有数据
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){}
printf(" "); *?
/* com2_bit=1;
if(com2_receive_count>523) //最大值处理,要与定义的一样才行
com2_receive_count=0; */
//溢出-如果发生溢出需要先读SR,再读DR寄存器 则可清除不断入中断的问题
if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART2,USART_FLAG_ORE); //读SR
USART_ReceiveData(USART2); //读DR
}
}
注意:else if ( res == FR_EXIST ) 这一段 总是执行不了,只能在reset后才能执行以下。弄了好几天了 ,希望老师也能指导下,是不是open的模式不对啊?
一周热门 更多>