| 
 实现的功能
 C#TCP客户端连接8266的TCP服务器,网络控制开发板继电器 
 1.控制继电器吸合 
 
 
 
 
 
 
 
 
 
 
 
 
 
 2.控制继电器断开 
 
 
 前言
 1.用户在看这一节之前请先学习 
 2.控制继电器引脚 
 协议规定
 C#TCP客户端发送给ESP8266TCP服务器控制继电器吸合指令: 0xaa  0x55  0x01 0x01 ESP8266执行以后回复给C#TCP客户端: 0x55  0xaa  0x01 0x01     C#TCP客户端发送给ESP8266TCP服务器控制继电器断开指令: 0xaa  0x55  0x01 0x00 ESP8266执行以后回复给C#TCP客户端: 0x55  0xaa  0x01 0x00 ESP8266程序编写
 1.处理程序在这节的基础上修改添加 
 2.配置GPIO5为普通引脚 
 
 /*设置GPIO5为普通引脚*/
 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U , FUNC_GPIO5);
 
 
 
 
 3.定义用于返回继电器状态的数组 
 
 /*用于返回继电器的状态*/
 u8 RelayOn[4]={0x55,0xaa,0x01,0x01};//继电器吸合
 u8 RelayOff[4]={0x55,0xaa,0x01,0x00};//继电器断开
 
 
 
 
 4.编写TCP接收处理程序 
 
 
 
 /**
 * @brief   TCP接收数据
 * @param   arg:tcp_arg函数传入的参数
 * * @param   p:接收的数据缓存
 * @param   err:错误信息
 * @param   None
 * @retval  None
 * @warning None
 * @example
 **/
 static err_t net_tcp_recv_cb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
 struct pbuf *q;
 u32 length = 0,i=0;
 tcp_pcb_server = tpcb;
 tcp_pcb_server_state = 1;
 if (!p || err!=ERR_OK) {
 if(p){
 pbuf_free(p);
 }
 tcp_pcb_server_state = 0;
 tcp_close(tcp_pcb_server);//关闭连接
 return ERR_CLSD;
 }
 //接收TCP数据(固定)
 for(q=p;q!=NULL;q=q->next){
 if(q->len > (TcpServerBuffLen-length))//接收的数据个数大于了数组可以接收的数据个数
 memcpy(TcpServerBuff+length,q->payload,(TcpServerBuffLen-length));//只接收数组可以接收的数据个数
 else
 memcpy(TcpServerBuff+length,q->payload,q->len);//接收TCP所有数据
 length += q->len;
 if(length > TcpServerBuffLen) break;
 }
 
 
 if(TcpServerBuff[0] == 0xaa && TcpServerBuff[1] == 0x55){
 if(TcpServerBuff[2] == 0x01){
 if(TcpServerBuff[3] == 0x01){
 GPIO_OUTPUT_SET(5, 1);//设置GPIO5输出高电平
 tcp_write(tcp_pcb_server, RelayOn, 4, TCP_WRITE_FLAG_COPY);//TCP_WRITE_FLAG_COPY:拷贝到发送缓存
 tcp_output(tcp_pcb_server);//立即发送
 }
 else if(TcpServerBuff[3] == 0x00){
 GPIO_OUTPUT_SET(5, 0);//设置GPIO5输出低电平
 tcp_write(tcp_pcb_server, RelayOff, 4, TCP_WRITE_FLAG_COPY);//TCP_WRITE_FLAG_COPY:拷贝到发送缓存
 tcp_output(tcp_pcb_server);//立即发送
 }
 }
 }
 
 
 //固定处理
 tcp_recved(tcp_pcb_server, p->tot_len);/*更新接收,告诉底层可以接着缓存数据了*/
 pbuf_free(p);//释放链表
 return ERR_OK;
 }
 
 
 
 5.先用TCP调试助手测试 
 
 
 
 5.1 电脑连接 8266 的无线 
 
 
 5.2 打开TCP客户端,连接8266 
 
 
 5.3 发送16进制指令测试 控制继电器吸合 aa  55  01 01 
 
 
   
 
 
 
 
 
 控制继电器断开 aa  55  01 00 
 
 
 
 
 
 C#上位机程序编写
 1.处理程序在这节的基础上修改添加 
 2.页面修改如下 3.点击控制继电器按钮 
 
 byte[] SendData = new byte[4];
 SendData[0] = 0xaa;
 SendData[1] = 0x55;
 SendData[2] = 0x01;
 SendData[3] = 0x01;
 if (button2.Text == "吸合")
 {
 SendData[3] = 0x01;
 }
 else if (button2.Text == "断开")
 {
 SendData[3] = 0x00;
 }
 try
 {
 MySocket.BeginSend(SendData, 0, SendData.Length, 0, null, null); //发送数据
 }
 catch (Exception) { }
 
 
 
 4.处理8266返回的数据 
 
 if (TCPBuffer[0] == 0x55 && TCPBuffer[1] == 0xaa)
 {
 if (TCPBuffer[2] == 0x01)
 {
 if (TCPBuffer[3] == 0x01)//继电器吸合
 {
 Invoke((new Action(() => {//C# 3.0以后代替委托的新方法
 label3.Text = "吸合";
 button2.Text = "断开";
 })));
 }
 else if (TCPBuffer[3] == 0x00)//继电器断开
 {
 Invoke((new Action(() => {//C# 3.0以后代替委托的新方法
 label3.Text = "断开";
 button2.Text = "吸合";
 })));
 }
 }
 }
 
 
 
 启动测试
 电脑连接8266无线 
 
 
 客户端连接TCP服务器 
 
 
 
 1.控制继电器吸合 
 
 
 
 
 
 
 
 
 
 
 
 
 
 2.控制继电器断开 
 
 
 
 
 
 
 |