LM3S6965 移植的无操作系统的lwip协议栈,做客户端用,接收到上层tcp助手发送tcp命令到单片机对sd卡进行操作,将文件信息读取通过tcp发送回上层tcp助手,为什么tcp连接不稳定,一会儿就断开,ping不通了,而直接就是通过tcp发送信息给上层就不会断呢,向各位大侠求解
void TCP_Client_Init(void)
{
IP4_ADDR(&ipaddr,192,168,1,100);
Clipcb = tcp_new(); // 建立通信的TCP控制块(Clipcb)
tcp_bind(Clipcb,IP_ADDR_ANY,1026); // 绑定本地IP地址和端口号
tcp_connect(Clipcb,&ipaddr,1026,TcpCli_Connected);
tcp_recv(Clipcb,tcp_Client_recv);
}
static err_t tcp_Client_recv(void *arg, struct tcp_pcb *pcb,struct pbuf *p,err_t err)
{ //unsigned int len;
char RecivePcBuf[1024];
strncpy(RecivePcBuf,p->payload,p->len);
RecivePcBuf[p->len]='\0';
tcp_recved (pcb,p->len);
if(p != NULL)
{
// tcp_write(pcb,p->payload,p->len,0);
deal_rpcdata_new(RecivePcBuf,p->len,pcb);
pbuf_free(p); /* 释放该TCP段 */
tcp_recved (pcb,p->len);
}
else
{
tcp_write(pcb,"empty command!\r\n",strlen("empty command!\r\n"),0);
}
// tcp_close(pcb); /* 关闭这个连接 */
err = ERR_OK;
return err;
}
tBoolean deal_rpcdata_new(char *p,int len,struct tcp_pcb *pcb)
{
char RecivePcBuf[1024];
char File_Name[30];
char temp_data[30];
char *i;
unsigned short usBytesRead;
char g_cTmpBuf[PATH_BUF_SIZE];
// int flag_Cmd;
char SendPc[100];
FRESULT fresult;
unsigned char FROK;
SendPc[0]='\0';
File_Name[0]='\0';
temp_data[0]='\0';
strncpy(RecivePcBuf,p,len);
RecivePcBuf[len]='\0';
// when the command is "ls" or "cd"
//
if ((RecivePcBuf[0]=='l')&&(RecivePcBuf[1]=='s'))
{
//tcp_write(pcb,RecivePcBuf,strlen(RecivePcBuf),0);
fresult = f_opendir(&g_sDirObject, g_cCwdBuf);
//
// Check for error and return if there is a problem.
//
if(fresult != FR_OK)
{
UARTprintf("openirfail!");
return(fresult);
}
for(;;)
{
//
// Read an entry from the directory.
//
fresult = f_readdir(&g_sDirObject, &g_sFileInfo);
//
// Check for error and return if there is a problem.
//
if(fresult != FR_OK)
{
UARTprintf("readdirfail!");
return(fresult);
}
//
// If the file name is blank, then this is the end of the
// listing.
//
if(!g_sFileInfo.fname[0])
{
UARTprintf("blankname!");
break;
}
//
// If the attribue is directory, then increment the directory count.
//
if(g_sFileInfo.fattrib & AM_DIR)
{
strcat(SendPc,"D---- ");
}
else
{
if (g_sFileInfo.fattrib & AM_ARC)
{
strcat(SendPc,"----A ");
}
}
strcat(SendPc,g_sFileInfo.fname);
strcat(SendPc,"\r\n");
}
FROK=tcp_write(pcb,SendPc,strlen(SendPc),0);
if (FROK!=ERR_OK)
{
UARTprintf("lstcpfail");
return 0;
}
FROK=tcp_output(pcb);
if (FROK!=ERR_OK)
{
return 0;
}
}
else
{
if((RecivePcBuf[0]=='c')&&(RecivePcBuf[1]=='a')&&(RecivePcBuf[2]=='t'))
{
// tcp_write(pcb,RecivePcBuf,strlen(RecivePcBuf),0);
File_Name[0]=0;
strncpy(File_Name,p+3,len-3);
File_Name[len-3]='\0';
g_cTmpBuf[0]='\0';
//
// Copy the current path to the temporary buffer so it can be manipulated.
//
// strcpy(g_cTmpBuf, g_cCwdBuf);
//
// If not already at the root level, then append a separator.
//
// if(strcmp("/", g_cCwdBuf))
// {
// strcat(g_cTmpBuf, "/");
// }
strcpy(g_cTmpBuf,"/");
// strcat(g_cTmpBuf,"/");
strcat(g_cTmpBuf, File_Name);
strcat(g_cTmpBuf, ".txt");
//
// Now finally, append the file name to result in a fully specified file.
//
//
// Open the file for reading.
//
fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_READ);
//
// If there was some problem opening the file, then return
// an error.
//
if(fresult != FR_OK)
{
UARTprintf("fileopenfail!");
FROK=tcp_write(pcb,i,strlen(i),0);
if (FROK!=ERR_OK)
{
return 0;
}
FROK=tcp_output(pcb);
if (FROK!=ERR_OK)
{
return 0;
}
return(fresult);
}
//f_lseek(&g_sFileObject,0);
//
// Enter a loop to repeatedly read data from the file and display it,
// until the end of the file is reached.
//
do
{
// Read a block of data from the file. Read as much as can fit
// in the temporary buffer, including a space for the trailing null.
//
fresult = f_read(&g_sFileObject, temp_data,strlen(temp_data)-1,&usBytesRead);
//
// If there was an error reading, then print a newline and
// return the error to the user.
//
if(fresult != FR_OK)
{
UARTprintf("filereadfail");
FROK=tcp_write(pcb,"fail to read!\r\n",strlen("fail to read!\r\n"),0);
if (FROK!=ERR_OK)
{
return 0;
}
FROK=tcp_output(pcb);
if (FROK!=ERR_OK)
{
return 0;
}
return(fresult);
}
//
// Null terminate the last block that was read to make it a
// null terminated string that can be used with printf.
//
temp_data[usBytesRead] = 0;
//
FROK=tcp_write(pcb,temp_data,strlen(temp_data),0);
if (FROK!=ERR_OK)
{
UARTprintf("cattcpfail!");
return 0;
}
FROK=tcp_output(pcb);
if (FROK!=ERR_OK)
{
return 0;
}
// Continue reading until less than the full number of bytes are
// read. That means the end of the buffer was reached.
//
}
while(usBytesRead == strlen(g_cTmpBuf)-1);
f_close(&g_sFileObject);
}
else
{
UARTprintf("wrongcommand");
FROK=tcp_write(pcb,"wrong command!\r\n",strlen("wrong command!\r\n"),0);
if (FROK!=ERR_OK)
{
return 0;
}
FROK=tcp_output(pcb);
if (FROK!=ERR_OK)
{
return 0;
}
}
}
return (1);
}