教你如何通过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!”。

01 /****************************************************

02 函数名:TCP_Client_Mode

03 形参:无

04 返回值:无

05 函数功能:通过串口发送AT命令配置S2E模块

06 ****************************************************/

07 volatile uint8_t SendFlag=0;

08

09 void TCP_Client_Mode(void)

10 {

11     uint8_t RecvFlag=1;

12     char *state;

13     switch (SendFlag) {

14     case 0: {

15         Usart_Send(USART2,”AT\r\n”);                                                                 //终端检测命令

16         while (RecvFlag) {

17             if (RX2_Point & FRAME_LEN) {

18                 //如果接收到数据

19                 state=strstr((char *)RecvBuff,”OK”);

20                       //判断回复的数据中是否有“OK”

21                 if (state!=NULL) {                                                                                            //有

22                     RX2_Point=0;

23                               //接收缓冲区指针置零

24                     RecvFlag=0;

25                              //接收标致位置零

26                     SendFlag=1;                                                                                                //发送标志位置零

27                     printf(“Recv:%s\r\n”,RecvBuff);

28                     memset(RecvBuff,0,RECV_LEN);

29                            //接收缓存清零

30                 } else {                                                                                                                            //无

31                     SendFlag=100;                                                                                              //配置失败

32                     RecvFlag=0;

33                 }

34             }

35         }

36     }

37     break;

38     case 1: {

39         Usart_Send(USART2,”AT+ECHO=0\r\n”);

40                    //开启(1)/关闭(0)回显命令

41         RecvFlag=1;

42         while (RecvFlag) {

43             if (RX2_Point & FRAME_LEN) {

44                 state=strstr((char *)RecvBuff,”OK”);

45                 if (state!=NULL) {

46                     RX2_Point=0;

47                     RecvFlag=0;

48                     SendFlag=2;

49                     printf(“Recv:%s\r\n”,RecvBuff);

50                     memset(RecvBuff,0,RECV_LEN);

51                 } else {

52                     SendFlag=100;

53                     RecvFlag=0;

54                 }

55             }

56         }

57     }

58     break;

59     case 2: {

60         Usart_Send(USART2,”AT+C1_OP=1\r\n”);                                       //配置为TCP

61                    Client模式命令

62         RecvFlag=1;

63         while (RecvFlag) {

64             if (RX2_Point & FRAME_LEN) {

65                 state=strstr((char *)RecvBuff,”OK”);

66                 if (state!=NULL) {

67                     RX2_Point=0;

68                     RecvFlag=0;

69                     SendFlag=3;

70                     printf(“Recv:%s\r\n”,RecvBuff);

71                     memset(RecvBuff,0,RECV_LEN);

72                 } else {

73                     SendFlag=100;

74                     RecvFlag=0;

75                 }

76             }

77         }

78     }

79     break;

80     case 3: {

81         Usart_Send(USART2,”AT+IP_MODE=1\r\n”);

82                    //配置DHCP模式命令

83         RecvFlag=1;

84         while (RecvFlag) {

85             if (RX2_Point & FRAME_LEN) {

86                 state=strstr((char *)RecvBuff,”OK”);

87                 if (state!=NULL) {

88                     RX2_Point=0;

89                     RecvFlag=0;

90                     SendFlag=4;

91                     printf(“Recv:%s\r\n”,RecvBuff);

92                     memset(RecvBuff,0,RECV_LEN);

93                 } else {

94                     SendFlag=100;

95                     RecvFlag=0;

96                 }

97             }

98         }

99     }

100     break;

101     case 4: {

102         Usart_Send(USART2,”AT+C1_PORT=5000\r\n”);

103                    //配置S2E的本地端口号

104         RecvFlag=1;

105         while (RecvFlag) {

106             if (RX2_Point & FRAME_LEN) {

107                 state=strstr((char *)RecvBuff,”OK”);

108                 if (state!=NULL) {

109                     RX2_Point=0;

110                     RecvFlag=0;

111                     SendFlag=5;

112                     printf(“Recv:%s\r\n”,RecvBuff);

113                     memset(RecvBuff,0,RECV_LEN);

114                 } else {

115                     SendFlag=100;

116                     RecvFlag=0;

117                 }

118             }

119         }

120     }

121     break;

122     case 5: {

123         Usart_Send(USART2,”AT+C1_CLI_IP1=192.168.1.109\r\n”);

124                    //配置远程服务器的IP地址

125         RecvFlag=1;

126         while (RecvFlag) {

127             if (RX2_Point & FRAME_LEN) {

128                 state=strstr((char *)RecvBuff,”OK”);

129                 if (state!=NULL) {

130                     RX2_Point=0;

131                     RecvFlag=0;

132                     SendFlag=6;

133                     printf(“Recv:%s\r\n”,RecvBuff);

134                     memset(RecvBuff,0,RECV_LEN);

135                 } else {

136                     SendFlag=100;

137                     RecvFlag=0;

138                 }

139             }

140         }

141     }

142     break;

143     case 6: {

144         Usart_Send(USART2,”AT+C1_CLI_PP1=5000\r\n”);

145                    //配置远程服务器的端口号

146         RecvFlag=1;

147         while (RecvFlag) {

148             if (RX2_Point & FRAME_LEN) {

149                 state=strstr((char *)RecvBuff,”OK”);

150                 if (state!=NULL) {

151                     RX2_Point=0;

152                     RecvFlag=0;

153                     SendFlag=7;

154                     printf(“Recv:%s\r\n”,RecvBuff);

155                     memset(RecvBuff,0,RECV_LEN);

156                 } else {

157                     SendFlag=100;

158                     RecvFlag=0;

159                 }

160             }

161         }

162     }

163     break;

164     case 7: {

165         Usart_Send(USART2,”AT+START_MODE=0\r\n”);

166         //配置下次上电启动模式(0-AT模式,1-

167         数据模式)

168         RecvFlag=1;

169         while (RecvFlag) {

170             if (RX2_Point & FRAME_LEN) {

171                 state=strstr((char *)RecvBuff,”OK”);

172                 if (state!=NULL) {

173                     RX2_Point=0;

174                     RecvFlag=0;

175                     SendFlag=8;

176                     printf(“Recv:%s\r\n”,RecvBuff);

177                     memset(RecvBuff,0,RECV_LEN);

178                 } else {

179                     SendFlag=100;

180                     RecvFlag=0;

181                 }

182             }

183         }

184     }

185     break;

186     case 8: {

187         Usart_Send(USART2,”AT+EXIT\r\n”);

188                    //保存配置并进入数据模式

189         RecvFlag=1;

190         while (RecvFlag) {

191             if (RX2_Point & FRAME_LEN) {

192                 state=strstr((char *)RecvBuff,”OK”);

193                 if (state!=NULL) {

194                     RX2_Point=0;

195                     RecvFlag=0;

196                     SendFlag=99;

197                     printf(“Recv:%s\r\n”,RecvBuff);

198                     memset(RecvBuff,0,RECV_LEN);

199                 } else {

200                     SendFlag=100;

201                     RecvFlag=0;

202                 }

203             }

204         }

205     }

206     break;

207     case 99: {                                                                                                                                          //配置成功!

208         printf(“TCP Client Config Success!\r\n”);

209         Config_OK=1;

210     }

211     default:

212         RecvFlag=100;

213         break;

214     case 100: {                                                                                                                                         //配置失败!

215         printf(“TCP Client Config Fail!\r\n”);

216         Config_OK=1;

217     }

218     break;

219     }

220 }

221

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

     

    WIZnet官方网站:http://www.iwiznet.cn/

         WIZnet官方技术服务平台(Q群):595547972

    更多物联网应用,关注WIZnet官方微信号: