7-51單片機ESP8266學習-AT指令(8266TCP服務器,編寫自己的C#TCP客戶端發信息給單片機控制小燈的亮滅)


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

自己都是現做現寫,如果想知道最終實現的功能,請看最后

 

先把源碼和資料鏈接放到這里

鏈接: https://pan.baidu.com/s/1jpHZjW_7pQKNfN9G4B6ZjA     密碼:nhn3 

 

 

先說一下哈,不要嫌界面不好看,自己是為了程序盡量的簡單,可以通過調整顏色或者通過重繪來使使界面好看,,,,,,,,咱先學會走.....

 

 

 

因為咱們會用到圖片所以先把圖片資源加載上來,為了

 

 

 

 

 

 

 

 

 

 

 調整的好看一點

 

 

 

現在設置,切換圖片

 

 

其實呢導入圖片應該先建一個資源文件更合理,后期再說

現在是讓按鈕狀態改變了

  

 

也修改一下燈的

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

 

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM