教你如何通过MCU配置S2E为TCP Client的工作模式

在上两篇文章中分别讲述了通过MCU配置S2E为UDP、TCP Server的工作模式”,相信小伙伴们已经对S2E的基本功能了解了一些,现在就让我们再接再厉、更上一层楼吧:

首先我们先了解一下MCU与S2E的接线方式:

  • 1、接线方式:
  • 2、例程说明:
  • 打开“通过MCU配置S2E为TCP Client例程”,主程序中第一部分TIM3_Init();是设定一个帧中断的时间定时器,这是因为该例程MCU的串口是通过帧中断来接收AT命令配置S2E后返回的数据的。

第二部分USARTX_Init();初始化MCU用到的串口,这里用USART1_Config();是printf功能,用于查看调试信息。USART2_Config();用于配置S2E,需要注意的是该串口的配置参数需要同S2E的串口配置参数一致,否则配置失败。

第三部分主循环中的TCP_Client_Mode();用于配置S2E为TCP Client模式。S2E的AT命令列表详见各个S2E型号的用户手册AT命令章节介绍。配置成功,串口打印“TCP Client Config Success!”,配置失败串口打印“TCP Client Config Fail!”。

/****************************************************
函数名:TCP_Client_Mode
形参:无
返回值:无
函数功能:通过串口发送AT命令配置S2E模块
****************************************************/
volatile uint8_t SendFlag = 0;

void TCP_Client_Mode(void)
{
	uint8_t RecvFlag = 1;
	char *state;
	switch (SendFlag)
	{
		case 0:
			{
				Usart_Send(USART2, "AT\r\n");	//终端检测命令
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
					 			//如果接收到数据
						state = strstr((char*) RecvBuff, "OK");
						//判断回复的数据中是否有“OK”
						if (state != NULL)
						{
							//有
							RX2_Point = 0;
							//接收缓冲区指针置零
							RecvFlag = 0;
							//接收标致位置零
							SendFlag = 1;	//发送标志位置零
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
							//接收缓存清零
						}
						else
						{
							//无
							SendFlag = 100;	//配置失败
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 1:
			{
				Usart_Send(USART2, "AT+ECHO=0\r\n");
				//开启(1)/关闭(0)回显命令
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 2;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 2:
			{
				Usart_Send(USART2, "AT+C1_OP=1\r\n");	//配置为TCP
				Client模式命令
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 3;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 3:
			{
				Usart_Send(USART2, "AT+IP_MODE=1\r\n");
				//配置DHCP模式命令
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 4;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 4:
			{
				Usart_Send(USART2, "AT+C1_PORT=5000\r\n");
				//配置S2E的本地端口号
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 5;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 5:
			{
				Usart_Send(USART2, "AT+C1_CLI_IP1=192.168.1.109\r\n");
				//配置远程服务器的IP地址
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 6;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 6:
			{
				Usart_Send(USART2, "AT+C1_CLI_PP1=5000\r\n");
				//配置远程服务器的端口号
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 7;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 7:
			{
				Usart_Send(USART2, "AT+START_MODE=0\r\n");
				//配置下次上电启动模式(0-AT模式,1-
				数据模式)
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 8;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 8:
			{
				Usart_Send(USART2, "AT+EXIT\r\n");
				//保存配置并进入数据模式
				RecvFlag = 1;
				while (RecvFlag)
				{
					if (RX2_Point & FRAME_LEN)
					{
						state = strstr((char*) RecvBuff, "OK");
						if (state != NULL)
						{
							RX2_Point = 0;
							RecvFlag = 0;
							SendFlag = 99;
							printf("Recv:%s\r\n", RecvBuff);
							memset(RecvBuff, 0, RECV_LEN);
						}
						else
						{
							SendFlag = 100;
							RecvFlag = 0;
						}
					}
				}
			}
			break;
		case 99:
			{
				//配置成功!
				printf("TCP Client Config Success!\r\n");
				Config_OK = 1;
			}
		default:
			RecvFlag = 100;
			break;
		case 100:
			{
				//配置失败!
				printf("TCP Client Config Fail!\r\n");
				Config_OK = 1;
			}
			break;
	}
}

W5500S2E-S1是一款工业级串口转以太网模块,支持多种波特率,从1.2Kbps至1.152Mbps。采用了WIZnet公司的硬件TCP/IP协议以太网芯片W5500。这是更快、更稳定、更安全的以太网解决方案。