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

 

 

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

 

目录
相关文章
|
15天前
|
网络协议 程序员 定位技术
学习网络的第一步:全面解析OSI与TCP/IP模型
**网络基础知识概览:** 探索网络通信的关键模型——OSI七层模型和TCP/IP五层模型。OSI模型(物理、数据链路、网络、传输、会话、表示、应用层)提供理论框架,而TCP/IP模型(物理、数据链路、网络、传输、应用层)更为实际,合并了会话、表示和应用层。两者帮助理解数据在网络中的传输过程,为网络设计和管理提供理论支持。了解这些模型,如同在复杂的网络世界中持有了地图。
26 2
|
1月前
|
开发框架 前端开发 .NET
LIMS(实验室)信息管理系统源码、有哪些应用领域?采用C# ASP.NET dotnet 3.5 开发的一套实验室信息系统源码
集成于VS 2019,EXT.NET前端和ASP.NET后端,搭配MSSQL 2018数据库。系统覆盖样品管理、数据分析、报表和项目管理等实验室全流程。应用广泛,包括生产质检(如石化、制药)、环保监测、试验研究等领域。随着技术发展,现代LIMS还融合了临床、电子实验室笔记本和SaaS等功能,以满足复杂多样的实验室管理需求。
43 3
LIMS(实验室)信息管理系统源码、有哪些应用领域?采用C# ASP.NET dotnet 3.5 开发的一套实验室信息系统源码
|
5天前
|
数据采集 监控 BI
C#实验室检验LIS信息系统源码 微生物检验、质控维护
LIS系统的主要目标是为检验室开展检验工作提供更加有效的系统支持。该系统将尽量减少以人工操作的方式来实现信息转移,减少在接收检验项目、报告结果和保存记录等工作中可能会出现的人为误差,为检验结果查询提供更有效的方法,节省了管理信息所需的琐碎时间和精力。为实验室技术人员提供智能化的运行模式,使处理诸如按照规程审核检验结果、取消检验项目、分析、处理存在重大疑问的检验结果、执行特殊的命令和处理质量控制等问题更轻松自如,这将使检验人员更快地获得准确清晰的检验结果。为临床医护人员提供在线设施,使他们可以及时准确地获得相关实验室信息。确保检验结果的可靠性和准确性,利用实验室管理信息系统的仪器监控和质量控制,
13 0
|
29天前
|
JSON 前端开发 测试技术
从零开始:学习使用 Postman 进行接口测试
在当前,API(应用程序接口)的使用变得越来越普遍。其中,HTTP/HTTPS API 是最常见的一种。无论是开发前端还是后端,测试 API 都是一个关键环节。Postman 是一种流行且强大的 API 测试工具,能够帮助开发人员轻松地进行接口测试和调试。
|
1月前
|
安全 测试技术
软件测试项目式学习二(认识软件测试及软件测试分类与案例分析)
软件测试项目式学习二(认识软件测试及软件测试分类与案例分析)
37 1
|
2月前
|
存储 网络协议 测试技术
【如何学习Python自动化测试】—— Cookie 处理
【如何学习Python自动化测试】—— Cookie 处理
24 1
|
1月前
|
测试技术
软件测试项目式学习三(软件测试原则与基本流程与实际测试用例)
软件测试项目式学习三(软件测试原则与基本流程与实际测试用例)
35 0
|
1月前
|
测试技术 程序员 开发者
软件测试项目式学习一(认识软件生命周期与开发模型及软件质量)
软件测试项目式学习一(认识软件生命周期与开发模型及软件质量)
31 0
|
2月前
|
消息中间件 Kafka 网络安全
JUnit5学习之七:参数化测试(Parameterized Tests)进阶
JUnit5学习之七:参数化测试(Parameterized Tests)进阶
|
2月前
|
Web App开发 测试技术 Python
【如何学习python自动化测试】—— 浏览器驱动的安装 以及 如何更新driver
【如何学习python自动化测试】—— 浏览器驱动的安装 以及 如何更新driver
45 0