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

W5500S2E-S1是一款工业级串口转以太网模块,支持TCP Server,TCP Client和UDP三种工作模式,串口波特率最高可达1.152Mbps,并提供配套的上位机配置软件,也可通过网页或AT命令等方式轻松配置。

W5500S2E-S1模块集成了全硬件TCP / IP协议栈以太网接口芯片W5500,网络通信更加快速,稳定,安全。用户只需根据手册中推荐的参考设计原理图,即可快速完成硬件电路的设计,降低开发难度,节省开发时间。

今天我们就实际的来了解一下W5500S2E-S1基于UDP工作模式的具体操作流程是什么样的,下面我们就来看看吧:

  • 具体操作流程

(1),接线方式:

(2)、例程说明:

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

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

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

/****************************************************
函数名:              UDP_Mode
形参:                       无
返回值:              无
函数功能:    配置S2E为UDP模式
****************************************************/
volatile uint8_t SendFlag = 0;

void UDP_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");
						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=2\r\n");
				//配置为UDP模式命令
				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");
				//配置本地端口号
				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.100\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("UDP Config Success!\r\n");
				Config_OK = 1;
			}
		default:
			RecvFlag = 100;
			break;
		case 100:
			{
				printf("UDP Config Fail!\r\n");
				Config_OK = 1;
			}
			break;
	}
}