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