9-51单片机ESP8266学习-AT指令(测试TCP服务器--51单片机程序配置8266,C#TCP客户端发信息给单片机控制小灯的亮灭)

简介: http://www.cnblogs.com/yangfengwu/p/8780182.html自己都是现做现写,如果想知道最终实现的功能,请看最后  先把源码和资料链接放到这里 链接:https://pan.

 http://www.cnblogs.com/yangfengwu/p/8780182.html

自己都是现做现写,如果想知道最终实现的功能,请看最后

 

 

先把源码和资料链接放到这里

 

链接:https://pan.baidu.com/s/10MxI8-Q33-M_R2WEHqEi1A 密码:j1sz

 

 

先说一下哈,不要嫌界面不好看,自己是为了程序尽量的简单,可以通过调整颜色或者通过重绘来使使界面好看,,,,,,,,咱先学会走.....

 

 

 

因为咱们会用到图片所以先把图片资源加载上来,为了

 

 

 

 

 

 

 

 

 

 

 调整的好看一点

 

 

 

现在设置,切换图片

 

 

其实呢导入图片应该先建一个资源文件更合理,后期再说

现在是让按钮状态改变了

  

 

也修改一下灯的

private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (LedFlage == false)
            {
                LedFlage = true;
                pictureBox2.BackgroundImage = Properties.Resources.switchon;
                pictureBox3.BackgroundImage = Properties.Resources.ledon;
            }
            else
            {
                LedFlage = false;
                pictureBox2.BackgroundImage = Properties.Resources.switchoff;
                pictureBox3.BackgroundImage = Properties.Resources.ledoff;
            }
        }

 

 现在做连接服务器

先说一下很多初学者会遇到的问题

 

这种情况是你添加了控件的事件函数,然后你又删除了,,,因为我也是经常删.................

 

我刚才在考虑要不要用委托和回调.....后来想了想这篇就不用了,

大家记得自己试一下这个(反正给大家说了,下次自己肯定用委托和回调写,记住不要偷懒,如果你偷懒了,后期的文章你就会无从下手,因为你连基础的都不知道)

http://www.cnblogs.com/yangfengwu/p/5761841.html

因为和android 一样只有主线程才允许操作控件,咱们就

 

现在做连接服务器和断开连接

先在电脑上测试

 

 

 

 

 

先给现在的程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TCPClient
{
    public partial class Form1 : Form
    {
        bool LedFlage = false;
        bool ConncetFlage = false;
        private Thread ThreadConnectService;//连接服务器线程
        private IPAddress ipAddress;//ip地址
        int Port = 0;//端口号
        private TcpClient myTcpClient = null;// TcpClient
        private NetworkStream networkstrem = null;//网络数据流
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//加了这一句
            
            
        }

        /*连接按钮点击事件*/
        private void button1_Click(object sender, EventArgs e)
        {
            if (ConncetFlage == false)
            {
                try{ThreadConnectService.Abort();}//先清除一下以前的
                catch (Exception){}
                ThreadConnectService = new Thread(ConncetService);//把连接服务器的函数加入任务
                ThreadConnectService.Start();//启动任务
            }
            else
            {
                ConncetFlage = false;
                button1.Text = "连接";
                pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                try{  myTcpClient.Close(); }catch (Exception){}//关闭通道
            }
        }

        /*LED灯控制按钮(图片)*/
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (LedFlage == false)
            {
                LedFlage = true;
                pictureBox2.BackgroundImage = Properties.Resources.switchon;
                pictureBox3.BackgroundImage = Properties.Resources.ledon;
            }
            else
            {
                LedFlage = false;
                pictureBox2.BackgroundImage = Properties.Resources.switchoff;
                pictureBox3.BackgroundImage = Properties.Resources.ledoff;
            }
        }

        /*连接服务器函数*/
        private void ConncetService()
        {
            ipAddress = IPAddress.Parse(textBox1.Text);//获取IP地址
            Port = Convert.ToInt32(textBox2.Text);     //获取端口号

            try
            {
                myTcpClient = new TcpClient(); //实例化myTcpClient
                myTcpClient.Connect(ipAddress, Port);//连接服务器
                ConncetFlage = true;
                button1.Text = "断开";
                pictureBox1.BackgroundImage = Properties.Resources.lighton;
            }
            catch (Exception)
            {
                ConncetFlage = false;
                button1.Text = "连接";
                pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                try { myTcpClient.Close(); }
                catch (Exception) { }
            }
        }

    }
}

 

 断开

忘了加一个功能,,,判断服务器是不是断开了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TCPClient
{
    public partial class Form1 : Form
    {
        bool LedFlage = false;
        bool ConncetFlage = false;
        private Thread ThreadConnectService;//连接服务器线程
        private IPAddress ipAddress;//ip地址
        int Port = 0;//端口号
        private TcpClient myTcpClient = null;// TcpClient
        private NetworkStream networkstrem = null;//网络数据流

        private Thread ThreadReadData;//接收消息线程
        private bool ThreadReadDataFlage = false;//接收数据任务循环用

        byte[] ReadBuffer = new byte[1024];//设置缓冲区1024个字节
        int ReadCnt = 0;//获取接收到了几个字节

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//加了这一句
            
            
        }

        /*连接按钮点击事件*/
        private void button1_Click(object sender, EventArgs e)
        {
            if (ConncetFlage == false)
            {
                try{ThreadConnectService.Abort();}//先清除一下以前的
                catch (Exception){}
                ThreadConnectService = new Thread(ConncetService);//把连接服务器的函数加入任务
                ThreadConnectService.Start();//启动任务
            }
            else
            {
                ConncetFlage = false;
                ThreadReadDataFlage = false;
                button1.Text = "连接";
                pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                try{  myTcpClient.Close(); }catch (Exception){}//关闭通道
            }
        }

        /*LED灯控制按钮(图片)*/
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (LedFlage == false)
            {
                LedFlage = true;
                pictureBox2.BackgroundImage = Properties.Resources.switchon;
                pictureBox3.BackgroundImage = Properties.Resources.ledon;
            }
            else
            {
                LedFlage = false;
                pictureBox2.BackgroundImage = Properties.Resources.switchoff;
                pictureBox3.BackgroundImage = Properties.Resources.ledoff;
            }
        }

        /*连接服务器函数*/
        private void ConncetService()
        {
            ipAddress = IPAddress.Parse(textBox1.Text);//获取IP地址
            Port = Convert.ToInt32(textBox2.Text);     //获取端口号

            try
            {
                myTcpClient = new TcpClient(); //实例化myTcpClient
                myTcpClient.Connect(ipAddress, Port);//连接服务器
                ConncetFlage = true;
                button1.Text = "断开";
                pictureBox1.BackgroundImage = Properties.Resources.lighton;

                networkstrem = myTcpClient.GetStream();//获取数据流

                ThreadReadDataFlage = true;
                try { ThreadReadData.Abort(); }//先清除一下以前的
                catch (Exception) { }
                ThreadReadData = new Thread(ReadData);//把接收数据的函数加入任务
                ThreadReadData.Start();
            }
            catch (Exception)
            {
                ConncetFlage = false;
                ThreadReadDataFlage = false;
                button1.Text = "连接";
                pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                try { myTcpClient.Close(); }
                catch (Exception) { }
            }
        }

        /*接收消息函数*/
        private void ReadData()
        {
            while (ThreadReadDataFlage)
            {
                try
                {
                    ReadCnt = networkstrem.Read(ReadBuffer, 0, ReadBuffer.Length);//读取数据
                    if (ReadCnt != 0)//有数据
                    {

                    }
                    else//异常断开
                    {
                        ConncetFlage = false;
                        ThreadReadDataFlage = false;
                        button1.Text = "连接";
                        pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                        try { myTcpClient.Close(); }
                        catch (Exception) { }
                    }
                }
                catch (Exception)
                {
                    ThreadReadDataFlage = false;
                }
            }
        }
    }
}

 

 

 

现在做数据发送部分,和APP那块几乎是一个模子刻出来的

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TCPClient
{
    public partial class Form1 : Form
    {
        bool LedFlage = false;
        bool ConncetFlage = false;
        private Thread ThreadConnectService;//连接服务器线程
        private IPAddress ipAddress;//ip地址
        int Port = 0;//端口号
        private TcpClient myTcpClient = null;// TcpClient
        private NetworkStream networkstrem = null;//网络数据流

        private Thread ThreadReadData;//接收消息线程
        private bool ThreadReadDataFlage = false;//接收数据任务循环用

        private Thread ThreadSendData;//发送消息线程
        private bool ThreadSendDataFlage = false;//发送数据任务循环用

        byte[] ReadBuffer = new byte[1024];//设置缓冲区1024个字节
        int ReadCnt = 0;//获取接收到了几个字节

        byte[] SendBuffer = new byte[1024];//设置发送缓冲区1024个字节
        int SendCnt = 0;//发送的个数

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//加了这一句
            
            
        }

        /*连接按钮点击事件*/
        private void button1_Click(object sender, EventArgs e)
        {
            if (ConncetFlage == false)
            {
                try{ThreadConnectService.Abort();}//先清除一下以前的
                catch (Exception){}
                ThreadConnectService = new Thread(ConncetService);//把连接服务器的函数加入任务
                ThreadConnectService.Start();//启动任务
            }
            else
            {
                ConncetFlage = false;
                ThreadReadDataFlage = false;
                ThreadSendDataFlage = false;
                button1.Text = "连接";
                pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                try{  myTcpClient.Close(); }catch (Exception){}//关闭通道
            }
        }

        /*LED灯控制按钮(图片)*/
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (LedFlage == false)
            {
                SendBuffer[0] = 0xaa;
                SendBuffer[1] = 0x55;
                SendBuffer[2] = 0x02;
                SendBuffer[3] = 0xff;
                SendCnt = 4;
                
                LedFlage = true;
                pictureBox2.BackgroundImage = Properties.Resources.switchon;
                pictureBox3.BackgroundImage = Properties.Resources.ledon;
            }
            else
            {
                SendBuffer[0] = 0xaa;
                SendBuffer[1] = 0x55;
                SendBuffer[2] = 0x02;
                SendBuffer[3] = 0x00;
                SendCnt = 4;

                LedFlage = false;
                pictureBox2.BackgroundImage = Properties.Resources.switchoff;
                pictureBox3.BackgroundImage = Properties.Resources.ledoff;
            }
        }

        /*连接服务器函数*/
        private void ConncetService()
        {
            ipAddress = IPAddress.Parse(textBox1.Text);//获取IP地址
            Port = Convert.ToInt32(textBox2.Text);     //获取端口号

            try
            {
                myTcpClient = new TcpClient(); //实例化myTcpClient
                myTcpClient.Connect(ipAddress, Port);//连接服务器
                ConncetFlage = true;
                button1.Text = "断开";
                pictureBox1.BackgroundImage = Properties.Resources.lighton;

                networkstrem = myTcpClient.GetStream();//获取数据流

                /*接收消息任务*/
                ThreadReadDataFlage = true;
                try { ThreadReadData.Abort(); }//先清除一下以前的
                catch (Exception) { }
                ThreadReadData = new Thread(ReadData);//把接收数据的函数加入任务
                ThreadReadData.Start();

                /*发送消息任务*/
                ThreadSendDataFlage = true;
                try { ThreadSendData.Abort(); }//先清除一下以前的
                catch (Exception) { }
                ThreadSendData = new Thread(SendData);
                ThreadSendData.Start();
            }
            catch (Exception)
            {
                ConncetFlage = false;
                ThreadReadDataFlage = false;
                ThreadSendDataFlage = false;
                button1.Text = "连接";
                pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                try { myTcpClient.Close(); }
                catch (Exception) { }
            }
        }

        /*接收消息函数*/
        private void ReadData()
        {
            while (ThreadReadDataFlage)
            {
                try
                {
                    ReadCnt = networkstrem.Read(ReadBuffer, 0, ReadBuffer.Length);//读取数据
                    if (ReadCnt != 0)//有数据
                    {

                    }
                    else//异常断开
                    {
                        ConncetFlage = false;
                        ThreadReadDataFlage = false;
                        ThreadSendDataFlage = false;
                        button1.Text = "连接";
                        pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                        try { myTcpClient.Close(); }
                        catch (Exception) { }
                    }
                }
                catch (Exception)
                {
                    ThreadReadDataFlage = false;
                }
            }
        }

         /*发送消息函数*/
        private void SendData()
        {
            while (ThreadSendDataFlage)
            {
                try
                {
                    if (SendCnt>0)
                    {
                        networkstrem.Write(SendBuffer, 0, SendCnt);
                        SendCnt = 0;
                    }
                }
                catch (Exception)
                {
                    ConncetFlage = false;
                    ThreadReadDataFlage = false;
                    ThreadSendDataFlage = false;
                    button1.Text = "连接";
                    pictureBox1.BackgroundImage = Properties.Resources.lightoff;
                    try { myTcpClient.Close(); }
                    catch (Exception) { }
                }
            }
        }

    }
}

 

 

 

现在用调试助手试一下

 

 

好了咱现在用8266试一试 

 

 

 

 

 

 C#的源码

 

 

好了.....但是刚才我在软件连接的时候复位了一下芯片发现软件没有检测出来断开..现在如果服务器主动断开

可以检测的到,异常好像不可以,后期再看看....今天太晚了写的匆忙,不知道软件还有没有其它的Bug,慢慢的去发现吧...

突然有想起来单片机程序有个BUG

 

 

 好了,感觉压力还很大...还有好多好多每写的.....................

 

目录
相关文章
|
9月前
|
XML 测试技术 API
利用C#开发ONVIF客户端和集成RTSP播放功能
利用C#开发ONVIF客户端和集成RTSP播放功能
4707 123
|
运维 Prometheus 监控
如何在测试环境中保持操作系统、浏览器版本和服务器配置的稳定性和一致性?
如何在测试环境中保持操作系统、浏览器版本和服务器配置的稳定性和一致性?
|
JavaScript 数据可视化 Docker
简易制作MCP服务器并测试
本文介绍了如何简易制作并测试MCP服务器,包括环境搭建、代码实现及Docker部署。首先通过uv包创建项目,在main.py中定义MCP服务器及其工具和资源函数。接着详细说明了在Windows上安装uv、配置Docker镜像加速、生成requirements.txt文件以及编写Dockerfile的过程。最后,通过构建和运行Docker容器部署MCP服务器,并使用Node.js工具测试其功能,确保服务器正常工作。此教程适合初学者快速上手MCP服务器的开发与部署。
4851 63
|
11月前
HI1105定频测试指令
HI1105模块在无线图传,特别是远距离无线图传领域有广泛应用,产品认证过程需要牵涉到定频测试,分享以下步骤参考! 主要牵涉到:11a测试、11n(H20)测试、11n(H40)测试、802.11ac 20测试、802.11ac 40测试、802.11ax 20测试、802.11ax 40M测试:
|
设计模式 IDE API
C# 一分钟浅谈:GraphQL 客户端调用
本文介绍了如何在C#中调用GraphQL API,涵盖基本步骤、常见问题及解决方案。首先,通过安装`GraphQL.Client`库并创建客户端实例,连接到GraphQL服务器。接着,展示了如何编写查询和突变,以及处理查询语法错误、变量类型不匹配等常见问题。最后,通过具体案例(如管理用户和订单)演示了如何在实际项目中应用这些技术,帮助开发者更高效地利用GraphQL。
323 38
C# 一分钟浅谈:GraphQL 客户端调用
|
设计模式 API 数据处理
C# 一分钟浅谈:GraphQL 客户端调用
本文介绍了如何在C#中使用`GraphQL.Client`库调用GraphQL API,涵盖基本查询、变量使用、批量请求等内容,并详细说明了常见问题及其解决方法,帮助开发者高效利用GraphQL的强大功能。
403 57
|
缓存 Ubuntu Linux
Linux环境下测试服务器的DDR5内存性能
通过使用 `memtester`和 `sysbench`等工具,可以有效地测试Linux环境下服务器的DDR5内存性能。这些工具不仅可以评估内存的读写速度,还可以检测内存中的潜在问题,帮助确保系统的稳定性和性能。通过合理配置和使用这些工具,系统管理员可以深入了解服务器内存的性能状况,为系统优化提供数据支持。
1802 4
|
数据可视化 前端开发 测试技术
接口测试新选择:Postman替代方案全解析
在软件开发中,接口测试工具至关重要。Postman长期占据主导地位,但随着国产工具的崛起,越来越多开发者转向更适合中国市场的替代方案——Apifox。它不仅支持中英文切换、完全免费不限人数,还具备强大的可视化操作、自动生成文档和API调试功能,极大简化了开发流程。
|
Java 测试技术 容器
Jmeter工具使用:HTTP接口性能测试实战
希望这篇文章能够帮助你初步理解如何使用JMeter进行HTTP接口性能测试,有兴趣的话,你可以研究更多关于JMeter的内容。记住,只有理解并掌握了这些工具,你才能充分利用它们发挥其应有的价值。+
1602 23
|
SQL 安全 测试技术
2025接口测试全攻略:高并发、安全防护与六大工具实战指南
本文探讨高并发稳定性验证、安全防护实战及六大工具(Postman、RunnerGo、Apipost、JMeter、SoapUI、Fiddler)选型指南,助力构建未来接口测试体系。接口测试旨在验证数据传输、参数合法性、错误处理能力及性能安全性,其重要性体现在早期发现问题、保障系统稳定和支撑持续集成。常用方法包括功能、性能、安全性及兼容性测试,典型场景涵盖前后端分离开发、第三方服务集成与数据一致性检查。选择合适的工具需综合考虑需求与团队协作等因素。
2130 24

热门文章

最新文章