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

在上一篇文章中讲了“如何通过MCU配置S2E为UDP模式”,相信小伙伴们已经了解了S2E的基本功能,在这一篇中,我们再接再厉,继续学习一下“如何通过MCU配置S2E为TCP Server的工作模式”,无论是配置UDP模式还是配置TCP模式,都是通过串口转以太网的方式实现的:

1、接线方式

2、例程说明

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

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

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

1 /****************************************************

2 函数名:    TCP_Server_Mode

3 形参:     无

4 返回值:    无

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

6 ****************************************************/

7 volatile uint8_t SendFlag=0;

8

9 void TCP_Server_Mode(void)

10 {

11     uint8_t RecvFlag=1;

12     char *state;

13

14     switch (SendFlag) {

15     case 0: {

16         Usart_Send(USART2,”AT\r\n”);

17                    //终端检测命令

18         while (RecvFlag) {

19             if (RX2_Point & FRAME_LEN) {

20                 //如果接收到数据

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

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

23                 if (state!=NULL) {

24                     //有

25                     RX2_Point=0;

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

27                     RecvFlag=0;

28                              //接收标致位置零

29                     SendFlag=1;

30                              //发送标志位置零

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

32                     memset(RecvBuff,0,RECV_LEN);

33                            //接收缓存清零

34                 } else {                                      //无

35                     SendFlag=100;

36                              //配置失败

37                     RecvFlag=0;

38                 }

39             }

40         }

41     }

42     break;

43     case 1: {

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

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

46         RecvFlag=1;

47         while (RecvFlag) {

48             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

50                 if (state!=NULL) {

51                     RX2_Point=0;

52                     RecvFlag=0;

53                     SendFlag=2;

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

55                     memset(RecvBuff,0,RECV_LEN);

56                 } else {

57                     SendFlag=100;

58                     RecvFlag=0;

59                 }

60             }

61         }

62     }

63     break;

64     case 2: {

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

66                    Server模式命令

67         RecvFlag=1;

68         while (RecvFlag) {

69             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

71                 if (state!=NULL) {

72                     RX2_Point=0;

73                     RecvFlag=0;

74                     SendFlag=3;     //状态标志位置零

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

76                     memset(RecvBuff,0,RECV_LEN);

77                 } else {

78                     SendFlag=100;

79                     RecvFlag=0;

80                 }

81             }

82         }

83     }

84     break;

85     case 3: {

86         Usart_Send(USART2,”AT+IP_MODE=0\r\n”);

87                    //配置为静态IP模式

88         RecvFlag=1;

89         while (RecvFlag) {

90             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

92                 if (state!=NULL) {

93                     RX2_Point=0;

94                     RecvFlag=0;

95                     SendFlag=4;     //状态标志位置零

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

97                     memset(RecvBuff,0,RECV_LEN);

98                 } else {

99                     SendFlag=100;

100                     RecvFlag=0;

101                 }

102             }

103         }

104     }

105     break;

106     case 4: {

107         Usart_Send(USART2,”AT+IP=192.168.1.88\r\n”);  //配置本地IP

108         RecvFlag=1;

109         while (RecvFlag) {

110             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

112                 if (state!=NULL) {

113                     RX2_Point=0;

114                     RecvFlag=0;

115                     SendFlag=5;     //状态标志位置零

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

117                     memset(RecvBuff,0,RECV_LEN);

118                 } else {

119                     SendFlag=100;

120                     RecvFlag=0;

121                 }

122             }

123         }

124     }

125     break;

126     case 5: {

127         Usart_Send(USART2,”AT+MARK=255.255.255.0\r\n”);

128                    //配置本地IP

129         RecvFlag=1;

130         while (RecvFlag) {

131             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

133                 if (state!=NULL) {

134                     RX2_Point=0;

135                     RecvFlag=0;

136                     SendFlag=6;     //状态标志位置零

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

138                     memset(RecvBuff,0,RECV_LEN);

139                 } else {

140                     SendFlag=100;

141                     RecvFlag=0;

142                 }

143             }

144         }

145     }

146     break;

147     case 6: {

148         Usart_Send(USART2,”AT+GATEWAY=192.168.1.1\r\n”);

149                    //配置本地IP

150         RecvFlag=1;

151         while (RecvFlag) {

152             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

154                 if (state!=NULL) {

155                     RX2_Point=0;

156                     RecvFlag=0;

157                     SendFlag=7;     //状态标志位置零

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

159                     memset(RecvBuff,0,RECV_LEN);

160                 } else {

161                     SendFlag=100;

162                     RecvFlag=0;

163                 }

164             }

165         }

166     }

167     break;

168     case 7: {

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

170                    //配置本地端口号

171         RecvFlag=1;

172         while (RecvFlag) {

173             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

175                 if (state!=NULL) {

176                     RX2_Point=0;

177                     RecvFlag=0;

178                     SendFlag=8;     //状态标志位置零

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

180                     memset(RecvBuff,0,RECV_LEN);

181                 } else {

182                     SendFlag=100;

183                     RecvFlag=0;

184                 }

185             }

186         }

187     }

188     break;

189     case 8: {

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

191                    //配置启动模式(0–AT模式,1–

192                    数据模式)

193         RecvFlag=1;

194         while (RecvFlag) {

195             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

197                 if (state!=NULL) {

198                     RX2_Point=0;

199                     RecvFlag=0;

200                     SendFlag=9;     //状态标志位置零

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

202                     memset(RecvBuff,0,RECV_LEN);

203                 } else {

204                     SendFlag=100;

205                     RecvFlag=0;

206                 }

207             }

208         }

209     }

210     break;

211     case 9: {

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

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

214         RecvFlag=1;

215         while (RecvFlag) {

216             if (RX2_Point & FRAME_LEN) { //如果接收到数据

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

218                 if (state!=NULL) {

219                     RX2_Point=0;

220                     RecvFlag=0;

221                     SendFlag=99;     //状态标志位置零

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

223                     memset(RecvBuff,0,RECV_LEN);

224                 } else {

225                     SendFlag=100;

226                     RecvFlag=0;

227                 }

228             }

229         }

230     }

231     break;

232     case 99: {

233         printf(“TCP Server Config Success!\r\n”);

234         Config_OK=1;

235     }

236     default:

237         RecvFlag=100;

238         break;

239     case 100: {

240         printf(“TCP Server Config Fail!\r\n”);

241         Config_OK=1;

242     }

243     break;

244     }

245 }

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

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

 

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

 

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