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

 

 

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

 

目录
相关文章
|
3月前
|
存储 安全 Java
程序与技术分享:C#值类型和引用类型的区别
程序与技术分享:C#值类型和引用类型的区别
28 0
|
24天前
51单片机用汇编语言实现独立按键检测,每个按键有不同功能,包含按键消抖程序
51单片机用汇编语言实现独立按键检测,每个按键有不同功能,包含按键消抖程序
|
23天前
|
人工智能 开发者 芯片
【51单片机】单片机开发者的福音: 让AI看电路图帮你编写程序(使用ChatGPT 中训练好的单片机工程师模型)
使用AI大语言模型编写 单片机程序. 使用的是 OpenAI公司发布的 ChatGPT .在ChatGPT上有别人训练好的 单片机工程师 with Keil uVision 5 - C Code Explainer模型, 可以上传电路图改模型可以通过这个用户所给的电路图进行编程.
【51单片机】单片机开发者的福音: 让AI看电路图帮你编写程序(使用ChatGPT 中训练好的单片机工程师模型)
|
1月前
|
存储 缓存 安全
学习服务器硬件基础知识
服务器是指一种高性能计算机,提供计算、存储和通信服务。通常运行在网络环境中,为计算机、设备或用户提供资源共享、数据存储和处理等服务。服务器可以是专门设计的硬件设备,也可以是在普通计算机上运行的特定软件。
43 6
|
14天前
|
缓存 NoSQL Redis
【Azure Redis 缓存】C#程序是否有对应的方式来优化并缩短由于 Redis 维护造成的不可访问的时间
【Azure Redis 缓存】C#程序是否有对应的方式来优化并缩短由于 Redis 维护造成的不可访问的时间
|
20天前
|
安全 C# 开发者
【C# 多线程编程陷阱揭秘】:小心!那些让你的程序瞬间崩溃的多线程数据同步异常问题,看完这篇你就能轻松应对!
【8月更文挑战第18天】多线程编程对现代软件开发至关重要,特别是在追求高性能和响应性方面。然而,它也带来了数据同步异常等挑战。本文通过一个简单的计数器示例展示了当多个线程无序地访问共享资源时可能出现的问题,并介绍了如何使用 `lock` 语句来确保线程安全。此外,还提到了其他同步工具如 `Monitor` 和 `Semaphore`,帮助开发者实现更高效的数据同步策略,以达到既保证数据一致性又维持良好性能的目标。
24 0
|
1月前
|
C#
WPF/C#:程序关闭的三种模式
WPF/C#:程序关闭的三种模式
27 0
|
2月前
|
开发框架 JSON 前端开发
基于ABP框架的SignalR,使用Winform程序进行功能测试
基于ABP框架的SignalR,使用Winform程序进行功能测试
|
3月前
|
开发框架 .NET 编译器
程序与技术分享:C#基础知识梳理系列三:C#类成员:常量、字段、属性
程序与技术分享:C#基础知识梳理系列三:C#类成员:常量、字段、属性
23 2
下一篇
DDNS