20-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 加入数据通信
生活随笔
收集整理的這篇文章主要介紹了
20-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 加入数据通信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://www.cnblogs.com/yangfengwu/p/11192594.html
?
自行調整頁面
?
?
?
?
?連接上以后主動發個數據
namespace TCPClient {public partial class Form1 : Form{private TcpClient myTcpClient = null;// TcpClient Thread ConnectThread;//連接線程string ipAddress;//記錄ip地址int Port = 0;//端口號private NetworkStream networkstrem = null;//獲取網絡數據用private Thread ReceiveThread;//接收消息線程public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){getIPAddress();//剛才寫的那個函數.獲取電腦IP,并顯示在下拉框 }/// <獲取本機 IP 地址>/// /// </summary>/// <returns></returns>private void getIPAddress(){IPAddress[] hostipspool = Dns.GetHostAddresses("");//獲取本機所以IPcomboBox1.Items.Clear();//清除下拉框里面的內容foreach (IPAddress ipa in hostipspool){if (ipa.AddressFamily == AddressFamily.InterNetwork){comboBox1.Items.Add(ipa.ToString());//下拉框加入IP數據comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//顯示第一個 }}}private void ConnectMethod(){myTcpClient = new TcpClient(); //實例化myTcpClienttry{myTcpClient.Connect(ipAddress, Port);//連接服務器 networkstrem = myTcpClient.GetStream();//獲取數據流操作實例.(給的方法就是這個......)try{networkstrem.Write(new byte[] { 0x31, 0x32, 0x33 }, 0,3);//發送數據,"123" }catch{}//連接上以后往下執行Invoke((new Action(() => {button1.Text = "斷開";})));}catch (Exception){//異常處理函數Invoke((new Action(() =>{button1.Text = "連接";})));try { myTcpClient.Close(); }catch { } //關閉連接 }}//連接和斷開按鈕private void button1_Click(object sender, EventArgs e){if (button1.Text == "連接"){ipAddress = comboBox1.Text.ToString();//獲取IP地址Port = Convert.ToInt32(textBox1.Text.ToString());//獲取端口號 ConnectThread = new Thread(ConnectMethod);//創建任務ConnectThread.Start();//啟動任務 }else{try { myTcpClient.Close(); } catch { } //關閉連接Invoke((new Action(() =>{button1.Text = "連接";})));}}private void comboBox1_DropDown(object sender, EventArgs e){getIPAddress();//剛才寫的那個函數 }} }
?
?
?測試
?
?
?
?現在寫接收數據,然后顯示
?
?
?
?
?
/// <接收消息線程>/// /// </summary>private void ReceiveDataMethod(){int RecvCnt = 0;byte[] recvBytes = new byte[1024];while (true){try{RecvCnt = networkstrem.Read(recvBytes, 0, recvBytes.Length);//獲取數據Invoke((new Action(() =>{//new ASCIIEncoding().GetString(recvBytes, 0, RecvCnt)//byte轉化為字符串textBox2.AppendText(new ASCIIEncoding().GetString(recvBytes, 0, RecvCnt));//追加顯示 })));}catch (Exception ex){//異常處理函數Invoke((new Action(() =>{button1.Text = "連接";})));try { ReceiveThread.Abort(); }//銷毀任務catch { }try { networkstrem.Dispose(); }//釋放資源catch { }try { myTcpClient.Close(); }//關閉TCPcatch { }}}}?
測試
?
?
?
關閉窗體的時候,關閉下TCP
?
?
?
?
再優化一下,,檢測服務器主動斷開
?
?
?
//檢測服務器是主動斷開if ((myTcpClient.Client.Poll(20, SelectMode.SelectRead)) && (myTcpClient.Client.Available == 0)){myTcpClient.Close();//關閉以后,后面程序會引發異常}?
?
測試
?
?
?
?
?
?
?
?
?現在寫發送,自行雙擊那個按鈕,,,讓軟件添加上那個按鈕的點擊事件
?
?
byte[] sendbyte = Encoding.Default.GetBytes(textBox3.Text.ToString());//獲取發送的數據,轉為byteif (sendbyte.Length > 0){try { networkstrem.Write(sendbyte, 0, sendbyte.Length); }//發送數據catch (Exception) { MessageBox.Show("請檢查連接", "提示!"); }}else{MessageBox.Show("數據不能為空", "提示!");}?
?
?
?測試
?
?
?然后做個 ?Hex發送
/// <字符串轉16進制格式,不夠自動前面補零>/// "0054FF" ==> 16進制 0x00 0x54 0xFF/// </summary>/// <param name="hexString"></param>/// <returns></returns>private static byte[] strToToHexByte(String hexString){int i;bool Flag = false;hexString = hexString.Replace(" ", "");//清除空格if ((hexString.Length % 2) != 0){Flag = true;}if (Flag == true){byte[] returnBytes = new byte[(hexString.Length + 1) / 2];try{for (i = 0; i < (hexString.Length - 1) / 2; i++){returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);}returnBytes[returnBytes.Length - 1] = Convert.ToByte(hexString.Substring(hexString.Length - 1, 1).PadLeft(2, '0'), 16);}catch{for (i = 0; i < returnBytes.Length; i++){returnBytes[i] = 0;}MessageBox.Show("超過16進制范圍A-F,已初始化為0", "提示");}return returnBytes;}else{byte[] returnBytes = new byte[(hexString.Length) / 2];try{for (i = 0; i < returnBytes.Length; i++){returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);}}catch{for (i = 0; i < returnBytes.Length; i++){returnBytes[i] = 0;}MessageBox.Show("超過16進制范圍A-F,已初始化為0", "提示");}return returnBytes;}}?
?
?
?
?測試
好了,hex顯示 ?和清除 大家自己搞定哈,,,
?
?
?
https://www.cnblogs.com/yangfengwu/p/11192618.html
?
轉載于:https://www.cnblogs.com/yangfengwu/p/11192603.html
總結
以上是生活随笔為你收集整理的20-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 加入数据通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Prebuilt binaries of
- 下一篇: 过年