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

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

void TCP_Server_Mode(void) {
  uint8_t RecvFlag = 1;
  char * state;

  switch (SendFlag) {
  case 0: {
    Usart_Send(USART2, "AT\r\n");
    //终端检测命令
    while (RecvFlag) {
      if (RX2_Point & amp; 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 & amp; 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=0\r\n"); //配置为TCP
    Server模式命令
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; 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=0\r\n");
    //配置为静态IP模式
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; 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+IP=192.168.1.88\r\n"); //配置本地IP
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; 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+MARK=255.255.255.0\r\n");
    //配置本地IP
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; 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+GATEWAY=192.168.1.1\r\n");
    //配置本地IP
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; 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+C1_PORT=5000\r\n");
    //配置本地端口号
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; 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+START_MODE=0\r\n");
    //配置启动模式(0--AT模式,1--
    数据模式)
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; FRAME_LEN) { //如果接收到数据
        state = strstr((char * ) RecvBuff, "OK");
        if (state != NULL) {
          RX2_Point = 0;
          RecvFlag = 0;
          SendFlag = 9; //状态标志位置零
          printf("Recv:%s\r\n", RecvBuff);
          memset(RecvBuff, 0, RECV_LEN);
        } else {
          SendFlag = 100;
          RecvFlag = 0;
        }
      }
    }
  }
  break;
  case 9: {
    Usart_Send(USART2, "AT+EXIT\r\n");
    //保存配置并进入数据模式
    RecvFlag = 1;
    while (RecvFlag) {
      if (RX2_Point & amp; 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 Server Config Success!\r\n");
    Config_OK = 1;
  }
  default:
    RecvFlag = 100;
    break;
  case 100: {
    printf("TCP Server Config Fail!\r\n");
    Config_OK = 1;
  }
  break;
  }
}

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