博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 串口编程 — MVVM MVVM Light 实例
阅读量:4337 次
发布时间:2019-06-07

本文共 7756 字,大约阅读时间需要 25 分钟。

  最近在学习WPF的MVVM模式,在公司用的串口调试工具太大(主要功能太强大,有很多其他功能,但是我用不到),所以闲着没事,自己写了一个串口调试工具,还是使用的WPF的MVVM模式,发现自从对它有些了解后,我已经迷上了这种模式

主要:下拉框加载本地所有串口,绿色代表串口已经连接,输入信息后,点发送信息,就会按发送频率对串口循环发送

前台页面
public partial class MainWindow : Window    {        MainViewModel viewmodel;        public MainWindow()        {            InitializeComponent();            viewmodel = new MainViewModel();            this.DataContext = viewmodel;        }     }
后台代码

 

public class MainViewModel : ViewModelBase    {        ///         /// Initializes a new instance of the MainViewModel class.        ///         public MainViewModel()        {            Cominit();//初始化端口            this.OpenPort = new RelayCommand(OpenportAction);            SendMsg = new RelayCommand(Sendmessage);        }        #region 属性        private string currentPortName;        ///         /// 当前端口名称        ///         public string CurrentPortName        {            get { return currentPortName; }            set            {                if (this.CurrenPort != null)                {                    if (this.CurrenPort.IsOpen)                    {                        this.CurrenPort.Close();                    }                }                currentPortName = value;                RaisePropertyChanged("CurrentPortName");                this.CurrenPort = new SerialPort();                this.ValueColor = "Red";            }        }        private string valueColor = "Red";        ///         /// 状态显示颜色        ///         public string ValueColor        {            get { return valueColor; }            set            {                valueColor = value;                RaisePropertyChanged("ValueColor");            }        }        private ObservableCollection
coms; ///
/// com端口集合 /// public ObservableCollection
Coms { get { return coms; } set { coms = value; RaisePropertyChanged("Coms"); } } private SerialPort port; ///
/// 当前操作端口 /// public SerialPort CurrenPort { get { return port; } set { port = value; } } private string msg1; ///
/// 消息1 /// public string Msg1 { get { return msg1; } set { msg1 = value; RaisePropertyChanged("Msg1"); } } private string msg2; ///
/// 消息2 /// public string Msg2 { get { return msg2; } set { msg2 = value; RaisePropertyChanged("Msg2"); } } private string msg3; ///
/// 消息3 /// public string Msg3 { get { return msg3; } set { msg3 = value; RaisePropertyChanged("Msg3"); } } private string msg4; ///
/// 消息4 /// public string Msg4 { get { return msg4; } set { msg4 = value; RaisePropertyChanged("Msg4"); } } private string msg5; ///
/// 消息5 /// public string Msg5 { get { return msg5; } set { msg5 = value; RaisePropertyChanged("Msg5"); } } private bool isFor = false; ///
/// 是否循环发送 /// public bool IsFor { get { return isFor; } set { isFor = value; RaisePropertyChanged("IsFor"); } } private string btnName = "发送信息"; ///
/// 按钮名称: 发送信息 : 停止发送 /// public string BtnName { get { return btnName; } set { btnName = value; RaisePropertyChanged("BtnName"); } } private int pl = 500; ///
/// 发送频率 /// public int PL { get { return pl; } set { pl = value; RaisePropertyChanged("PL"); } } #endregion ///
/// 打开端口命令 /// public RelayCommand OpenPort { get; set; } ///
/// 发送信息命令 /// public RelayCommand SendMsg { get; set; } ///
/// 打开端口 /// public void OpenportAction() { try { if (CurrenPort != null && CurrenPort.PortName == CurrentPortName && CurrenPort.IsOpen) { return; } this.CurrenPort = new SerialPort(); CurrenPort.PortName = CurrentPortName; this.CurrenPort.Open(); this.ValueColor = "Lime"; } catch (System.Exception ex) { this.ValueColor = "Red"; } } ///
/// 初始化端口 /// public void Cominit() { Coms = new ObservableCollection
(); Microsoft.VisualBasic.Devices.Computer computer = new Microsoft.VisualBasic.Devices.Computer(); Coms.Add("虚拟端口"); foreach (string str in computer.Ports.SerialPortNames) { Coms.Add(str); } } List
msgs;// 消息列表 bool isopen = false;// 标记按钮当前状态 Thread thread; //线程实例 public void Sendmessage() { if (!isopen) { isopen = true; BtnName = "停止发送"; msgs = new List
(); if (!string.IsNullOrEmpty(Msg1)) { msgs.Add(Msg1); } if (!string.IsNullOrEmpty(Msg2)) { msgs.Add(Msg2); } if (!string.IsNullOrEmpty(Msg3)) { msgs.Add(Msg3); } if (!string.IsNullOrEmpty(Msg4)) { msgs.Add(Msg4); } if (!string.IsNullOrEmpty(Msg5)) { msgs.Add(Msg5); } thread = new Thread(new ThreadStart(ThreadSend)); thread.Start(); } else { thread.Abort(); isopen = false; BtnName = "发送信息"; } } public void ThreadSend() { bool value = true; while (value) { foreach (string str in msgs) { Thread.Sleep(PL); // 发送频率 线程挂起 CurrenPort.WriteLine(str); } if (!IsFor)//是否循环发送 { value = false; } } isopen = false; BtnName = "发送信息"; thread.Abort(); } }
MainViewModel

 

 

 

转载于:https://www.cnblogs.com/yuanaibeilei/p/3286479.html

你可能感兴趣的文章
微信-商城商品的图文/商品链接分享(后台数据合成图片+二维码生成)
查看>>
jquery tab mouseover 特效
查看>>
日本語を勉強するの日記(四)
查看>>
Python环境的安装
查看>>
Python简介以及入门
查看>>
Combination-Sum II
查看>>
Next Permutation
查看>>
《算法导论》CLRS算法C++实现(九)P109 选择数组中第i小(大)的数 顺序统计量...
查看>>
Template基础
查看>>
vue项目如何打包扔向服务器
查看>>
Observer(观察者)
查看>>
nodejs vinyl-fs 处理文件时输入问题
查看>>
HDU - 2602 Bone Collector
查看>>
虚拟机静态IP配置
查看>>
今天遇到了一个问题,将应用程序从服务器读取到的电话号码存储到通讯录中,必须在真机上跑,有点小激动。...
查看>>
python不换行输出
查看>>
Jexus部署Asp.Net Core项目
查看>>
itunes connect提交app教程
查看>>
C#面试题整理(1)
查看>>
怎样判断有没有SQL注入?
查看>>