最近在学习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 ObservableCollectioncoms; /// /// com端口集合 /// public ObservableCollectionComs { 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(); } }