就是一个简单的循环,后续再增加超时检测
当服务端点击断开时,单片机会提示断线重连,5s左右重连,继续发送数据
单片机提示:
单片机提示:
cc3200关键代码:
char IP_192_168_3_47[20]="192.168.3.47";
char IP_192_168_3_50[20]="192.168.3.50";
long UserInput()
{
int iInput = 0;
char acCmdStore[50];
int lRetVal;
int iRightInput = 0;
unsigned long ulUserInputData = 0;
do
{
UART_PRINT("\r\n当前设置: \r\nSSID Name: %s\r\nPORT = %d\r\nPacket Count = %d\r\n"
"Destination IP: %d.%d.%d.%d\n\r",
SSID_NAME, g_uiPortNum, g_ulPacketCount,
SL_IPV4_BYTE(g_ulDestinationIp,3),
SL_IPV4_BYTE(g_ulDestinationIp,2),
SL_IPV4_BYTE(g_ulDestinationIp,1),
SL_IPV4_BYTE(g_ulDestinationIp,0));
UART_PRINT("\r\n\r\n请选择:\r\n1. 客户端.\r\n2. 服务端.\r\n3. 设置.\r\n4. exit(0)\r\n");
UART_PRINT("输入数字后加回车发送: ");
lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(lRetVal == 0)
{
//
// No input. Just an enter pressed probably. Display a prompt.
//
//UART_PRINT("\n\n\rEnter Valid Input.");
}
else
{
iInput = (int)strtoul(acCmdStore,0,10);
if(iInput == 1)
{
// UART_PRINT("Run iperf command \"iperf.exe -s -i 1 -t 100\" and press "
// "Enter\n\r");
//
// Wait to receive a character over UART
//
MAP_UARTCharGet(CONSOLE);
UART_PRINT("进入客户端模式...\n\r");
// Before proceeding, please make sure to have a server waiting on
// PORT_NUM
while(1){
lRetVal = BsdTcpClient(g_uiPortNum);
UART_PRINT("断线重连中...\n\r");
MAP_UtilsDelay(50001000);
if(0)break;//一个简单的重连机制,后续增加超时检测
}
}
else if(iInput == 2)
{
UART_PRINT("Press Enter and run iperf command \"iperf.exe -c "
"%d.%d.%d.%d -i 1 -t 100000\" \n\r",
SL_IPV4_BYTE(g_ulIpAddr,3),
SL_IPV4_BYTE(g_ulIpAddr,2),
SL_IPV4_BYTE(g_ulIpAddr,1),
SL_IPV4_BYTE(g_ulIpAddr,0));
//
// Wait to receive a character over UART
//
MAP_UARTCharGet(CONSOLE);
UART_PRINT("Receiving TCP packets...\n\r");
// After calling this function, you can start sending data to
// CC3200 IP address on PORT_NUM
lRetVal = BsdTcpServer(g_uiPortNum);
}
else if(iInput == 3)
{
iRightInput = 0;
do
{
// get input for PORT/ IP/ Packet count
UART_PRINT("\r\n\r\n请选择要设置的内容:\r\n1. 端口\r\n2. 发送包的数量(目前无效)\r\n"
"3. 服务端IP\r\n4. 返回上一层菜单\r\n");
UART_PRINT("输入数字后添加回车发送: ");
lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(lRetVal == 0)
{
//
// No input. Just an enter pressed probably. Display a prompt.
//
//UART_PRINT("\n\n\rEnter Valid Input.");
}
else
{
iInput = (int)strtoul(acCmdStore,0,10);
//SettingInput(iInput);
switch(iInput)
{
case 1:
do
{
UART_PRINT("输入新的端口: ");
lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(lRetVal == 0)
{
//
// No input. Just an enter pressed probably.
// Display a prompt.
//
//UART_PRINT("\n\rEnter Valid Input.");
iRightInput = 0;
}
else
{
ulUserInputData = (int)strtoul(acCmdStore,0,10);
if(ulUserInputData <= 0 || ulUserInputData > 65535)
{
UART_PRINT("\n\rWrong Input");
iRightInput = 0;
}
else
{
g_uiPortNum = ulUserInputData;
iRightInput = 1;
UART_PRINT("\r\n设置成功");
}
}
UART_PRINT("\r\n");
}while(!iRightInput);
iRightInput = 0;
break;
case 2:
do
{
UART_PRINT("Enter Packet Count: ");
lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(lRetVal == 0)
{
//
// No input. Just an enter pressed probably.
// Display a prompt.
//
UART_PRINT("\n\rEnter Valid Input.");
iRightInput = 0;
}
else
{
ulUserInputData = (int)strtoul(acCmdStore,0,10);
if(ulUserInputData <= 0 || ulUserInputData > 9999999)
{
UART_PRINT("\n\rWrong Input");
iRightInput = 0;
}
else
{
g_ulPacketCount = ulUserInputData;
iRightInput = 1;
UART_PRINT("\r\n设置成功");
}
}
UART_PRINT("\r\n");
}while(!iRightInput);
iRightInput = 0;
break;
case 3:
do
{
UART_PRINT("\r\n选择服务端IP: \r\n1,192.168.3.47"
"\r\n2,192.168.3.50\r\n3,手动输入\r\n");
lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(lRetVal == 0)
{
//
// No input. Just an enter pressed probably.
// Display a prompt.
//
//UART_PRINT("\n\rEnter Valid Input.");
iRightInput = 0;
}
else
{
//根据输入的数字判断是哪个IP
iInput = (int)strtoul(acCmdStore,0,10);
if(iInput==1){strcpy(acCmdStore,IP_192_168_3_47);}
else if(iInput==2){strcpy(acCmdStore,IP_192_168_3_50);}
else if(iInput==3){
while(1){
//等待手动输入
lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(lRetVal == 0)
{
//
// No input. Just an enter pressed probably.
// Display a prompt.
//
//UART_PRINT("\n\rEnter Valid Input.");
iRightInput = 0;
}else{
UART_PRINT("\r\n输入的是:%s",acCmdStore);
break;
}
}
}else{
//选择IP,输入值无效
UART_PRINT("\r\n选择IP选项,输入值无效");
}
//将输入的IP设置到板子上
if(IpAddressParser(acCmdStore) < 0)
{
UART_PRINT("\n\rWrong Input");
iRightInput = 0;
}
else
{
iRightInput = 1;
UART_PRINT("\r\n设置成功");
}
}
UART_PRINT("\r\n");
}while(!iRightInput);
iRightInput = 0;
break;
case 4:
iRightInput = 1;
break;
}
}
}while(!iRightInput);
}
else if(iInput == 4)
{
break;
}
else
{
UART_PRINT("\n\n\rWrong Input");
}
}
UART_PRINT("\n\r");
}while(1);
return SUCCESS;
}