找传奇、传世资源到传世资源站!

串口调试工具(读取温度值)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

服务端 客户端一体化
串口调试工具(读取温度值) C#语言基础-第1张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 NetWork{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int a = 0; int b = 0; int c = 0; IPAddress ip; IPEndPoint point = new IPEndPoint(IPAddress.Any,0); string protocol; Socket aimSocket; Socket mySocket; //IPEndPoint remote; private void Form1_Load(object sender, EventArgs e)//初始化为UDP Server模式 { cobProtocol.SelectedIndex = 0; txtIP.Text = GetAddressIP(); Control.CheckForIllegalCrossThreadCalls = false; } string GetAddressIP() { string AddressIP = ""; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress.ToString(); ip = _IPAddress;//设定全局的IP } } return AddressIP; } private void btnStart_Click(object sender, EventArgs e)//设置目标IP(Client),本地IP(Server) { try { if (protocol == "TCP Server") { mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));//绑定端口号 mySocket.Bind(point);//绑定监听端口 MessageBox.Show("TCP Server绑定成功"); mySocket.Listen(10);//等待连接是一个阻塞过程,创建线程来监听 Thread thReceive = new Thread(TSReceive); thReceive.IsBackground = true; thReceive.Start(); } else if (protocol == "TCP Client") { ip = IPAddress.Parse(txtIP.Text);//目标IP point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));//目标端口 aimSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); aimSocket.Connect(point);//连接服务器 MessageBox.Show("连接成功"); Thread thReceive = new Thread(TCReceive); thReceive.IsBackground = true; thReceive.Start(); } } catch {} } private void cobProtocol_SelectedIndexChanged(object sender, EventArgs e) { // [2] TCP Server [3] TCP Client //此处设置标准位??? 然后为btnConnect做准备? protocol = cobProtocol.SelectedItem.ToString(); if (protocol == "TCP Server") { lblIP.Text = "本地IP地址:"; lblPort.Text = "本地端口号:"; } else if (protocol == "TCP Client") { lblIP.Text = "服务器IP地址:"; lblPort.Text = "服务器端口号:"; } } private void btnSend_Click(object sender, EventArgs e) { try { byte[] buffer = Encoding.Default.GetBytes(txtSend.Text); aimSocket.SendTo(buffer, point); } catch { } } //TCP Server接收线程 void TSReceive() { aimSocket = mySocket.Accept();//服务端监听到的Socket为服务端发送数据的目标socket byte[] buffer = new byte[1024]; while (true) { try { int r = aimSocket.Receive(buffer);//接收到监听的Socket的数据 if (r == 0) { MessageBox.Show("连接断开"); break; } string strRec = Encoding.Default.GetString(buffer, 0, r); txtRec.AppendText(aimSocket.RemoteEndPoint.ToString() ":" strRec "\r\n"); //温度解析 int p = -1; int w = -1; p=strRec.IndexOf("wendu:"); w=strRec.IndexOf("//wendu"); if ((p != -1)&&(w!=-1)) { int length = w - p; wendu.Text = strRec.Substring(p 6, length-6); } //湿度解析 int i = -1; int u = -1; i = strRec.IndexOf("shidu:"); u = strRec.IndexOf("//shidu"); if ((i != -1) && (u != -1)) { int length = u - i; shidu.Text = strRec.Substring(i 6, length - 6); } } catch { } } } //TCP Client接收线程 void TCReceive() { byte[] buffer = new byte[1024]; while (true) { try { int r = aimSocket.Receive(buffer); if (r == 0) { MessageBox.Show("连接断开"); break; } string strRec = Encoding.Default.GetString(buffer, 0, r); txtRec.AppendText(aimSocket.RemoteEndPoint.ToString() ":" strRec "\r\n"); //温度解析 int p = -1; int w = -1; p = strRec.IndexOf("wendu:"); w = strRec.IndexOf("//wendu"); if ((p != -1) && (w != -1)) { int length = w - p; wendu.Text = strRec.Substring(p 6, length - 6); } //湿度解析 int i = -1; int u = -1; i = strRec.IndexOf("shidu:"); u = strRec.IndexOf("//shidu"); if ((i != -1) && (u != -1)) { int length = u - i; shidu.Text = strRec.Substring(i 6, length - 6); } } catch { } } } private void button1_Click(object sender, EventArgs e) { try { if (a % 2 == 0) { byte[] buffer = Encoding.Default.GetBytes("111"); aimSocket.SendTo(buffer, point); button1.BackColor = Color.Blue; txtSend.Text = "111"; } if (a % 2 == 1) { byte[] buffer = Encoding.Default.GetBytes("100"); aimSocket.SendTo(buffer, point); button1.BackColor = Color.DarkGray; txtSend.Text = "100"; } a ; } catch { } } private void button2_Click(object sender, EventArgs e) { try { if (b % 2 == 0) { byte[] buffer = Encoding.Default.GetBytes("222"); aimSocket.SendTo(buffer, point); button2.BackColor = Color.Blue; txtSend.Text = "222"; } if (b % 2 == 1) { byte[] buffer = Encoding.Default.GetBytes("200"); aimSocket.SendTo(buffer, point); button2.BackColor = Color.DarkGray; txtSend.Text = "200"; } b ; } catch { } } private void button3_Click(object sender, EventArgs e) { try { if (c % 2 == 0) { byte[] buffer = Encoding.Default.GetBytes("333"); aimSocket.SendTo(buffer, point); button3.BackColor = Color.Blue; txtSend.Text = "333"; } if (c % 2 == 1) { byte[] buffer = Encoding.Default.GetBytes("300"); aimSocket.SendTo(buffer, point); button3.BackColor = Color.DarkGray; txtSend.Text = "300"; } c ; } catch { } } private void button4_Click(object sender, EventArgs e) { try { byte[] buffer = Encoding.Default.GetBytes("444"); aimSocket.SendTo(buffer, point); txtSend.Text = "444"; } catch { } } private void button5_Click(object sender, EventArgs e) { try { byte[] buffer = Encoding.Default.GetBytes("555"); aimSocket.SendTo(buffer, point); txtSend.Text = "555"; } catch { } } private void button6_Click(object sender, EventArgs e) { try { byte[] buffer = Encoding.Default.GetBytes("666"); aimSocket.SendTo(buffer, point); txtSend.Text = "666"; } catch { } } private void button7_Click(object sender, EventArgs e) { txtRec.Text = null; } private void button8_Click(object sender, EventArgs e) { txtSend.Text = null; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复