基本信息
源码名称:udp串口助手源码(wpf)
源码大小:0.14M
文件格式:.rar
开发语言:C#
更新时间:2018-09-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
该软件基于WPF编写,工程基于VS2012开发,自己当时初学WPF的做的练手的,如果对你有帮助,可以下载源码,
该软件基于WPF编写,工程基于VS2012开发,自己当时初学WPF的做的练手的,如果对你有帮助,可以下载源码,
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace UDPDemo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { //目标IP和端口号 private static string RemoteIp = "127.0.0.1"; private static int RemotePort = 10028; private UdpClient udpSendClient ; private IPEndPoint localEP ; private IPEndPoint ReceiveEndpoint = new IPEndPoint(IPAddress.Any, 0); Int32 RecCount = 0; Int32 SendCount = 0; //接收线程 Thread ReceiveThread; /// <summary> /// 判断端口是否被占用 /// </summary> /// <param name="port"></param> /// <returns></returns> private bool PortInUse(int port) { bool inUse = false; IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipEndPoints = ipProperties.GetActiveUdpListeners(); foreach (IPEndPoint endPoint in ipEndPoints) { if (endPoint.Port == port) { inUse = true; break; } } return inUse; } private void GetIP() { System.Net.IPAddress[] addressList = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress ip in addressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { comboxLocalIp.Items.Add(ip.ToString()) ; } } } public MainWindow() { InitializeComponent(); } private void BtnConnect_Click(object sender, RoutedEventArgs e) { if (PortInUse(Convert.ToInt32(txtLocalPort.Text)) == true) { MessageBox.Show("该PORT被占用,请更换其他PORT后再试!", "提示"); return; } if (comboxLocalIp.SelectedItem == null) { MessageBox.Show("本机无可用IP!", "提示"); return; } try { localEP = new IPEndPoint(IPAddress.Parse(comboxLocalIp.SelectedItem.ToString()), Convert.ToInt32(txtLocalPort.Text)); if (localEP != null) { udpSendClient = new UdpClient(localEP); } if (udpSendClient != null) { labUDPState.Content = "就绪"; BtnClose.Visibility = Visibility.Visible; BtnConnect.Visibility = Visibility.Hidden; comboxLocalIp.IsEnabled = false; txtLocalPort.IsEnabled = false; BtnSend.IsEnabled = true; //开一线程 ReceiveThread = new Thread(new ThreadStart(listen)); //设置为后台线程 ReceiveThread.IsBackground = true; ReceiveThread.Start(); } } catch (Exception ex) { MessageBox.Show("错误:" ex.Message, "提示"); } } private void BtnClose_Click(object sender, RoutedEventArgs e) { try { localEP = null; udpSendClient.Close(); udpSendClient = null; labUDPState.Content = "未就绪"; if (ReceiveThread.ThreadState == ThreadState.Running) { ReceiveThread.Abort(); } BtnConnect.Visibility = Visibility.Visible; BtnClose.Visibility = Visibility.Hidden; comboxLocalIp.IsEnabled = true; txtLocalPort.IsEnabled = true; BtnSend.IsEnabled = false; } catch (Exception ex) { MessageBox.Show("错误:" ex.Message, "提示"); } } /// <summary> /// 网口数据接收事件 /// </summary> private void listen() { try { while (true) { byte[] UdpRecBuf = udpSendClient.Receive(ref ReceiveEndpoint); RecCount = UdpRecBuf.Length; this.Dispatcher.Invoke(new Action(() => { //txtRec.Text = "From[" ReceiveEndpoint.Address ":" ReceiveEndpoint.Port "]:" BitConverter.ToString(UdpRecBuf, 0) "\r\n"; txtRec.Text = "From[" ReceiveEndpoint.Address ":" ReceiveEndpoint.Port "]:" Encoding.UTF8.GetString(UdpRecBuf, 0, UdpRecBuf.Length) "\r\n"; labRecCount.Content = RecCount.ToString(); })); GC.Collect(); } } catch (Exception ex) { } } private void Window_Loaded(object sender, RoutedEventArgs e) { GetIP(); if (comboxLocalIp.Items.Count != 0) { comboxLocalIp.SelectedIndex = 0; } txtLocalPort.Text = "10000"; txtRemoteIp.Text = comboxLocalIp.SelectedItem.ToString(); txtRemotePort.Text = "10002"; } private void BtnSend_Click(object sender, RoutedEventArgs e) { try { byte[] tempbuf = Encoding.UTF8.GetBytes(txtSend.Text); SendCount = tempbuf.Length; udpSendClient.Send(tempbuf, tempbuf.Length, txtRemoteIp.Text, Convert.ToInt32(txtRemotePort.Text)); labSendCount.Content = SendCount.ToString(); } catch (Exception ex) { MessageBox.Show("错误:" ex.Message, "提示"); } } private void BtnClearCount_Click(object sender, RoutedEventArgs e) { SendCount = 0; RecCount = 0; labSendCount.Content = 0; labRecCount.Content = 0; } private void BtnClearRec_Click(object sender, RoutedEventArgs e) { txtRec.Text = string.Empty; } private void BtnClearSend_Click(object sender, RoutedEventArgs e) { txtSend.Text = string.Empty; } } }