Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

155 Zeilen
3.9 KiB

  1. using ModbusDemo.Uitls;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. namespace ModbusDemo.VIewModel
  12. {
  13. class SettingsUCViewModel : BindableBase
  14. {
  15. public DelegateCommand DisConnectionCmm { get; set; }
  16. //链接窗口的命令
  17. public DelegateCommand ConnectionCmm { get; set; }
  18. //获取串口
  19. private SerialPort _serialPort;
  20. //设置串口名字
  21. private string _portName = "COM1";
  22. public string PortName
  23. {
  24. get { return _portName; }
  25. set
  26. {
  27. _portName = value;
  28. RaisePropertyChanged();
  29. }
  30. }
  31. //设置波特率
  32. private string _baudRate = "19200";
  33. public string BaudRate
  34. {
  35. get { return _baudRate; }
  36. set
  37. {
  38. _baudRate = value;
  39. RaisePropertyChanged();
  40. }
  41. }
  42. //设置数据位
  43. private string _dataBits = "8";
  44. public string DataBits
  45. {
  46. get { return _dataBits; }
  47. set
  48. {
  49. _dataBits = value;
  50. RaisePropertyChanged();
  51. }
  52. }
  53. //设置校验位
  54. private Parity _parity = Parity.Even;
  55. public Parity Parity
  56. {
  57. get { return _parity; }
  58. set
  59. {
  60. _parity = value;
  61. RaisePropertyChanged();
  62. }
  63. }
  64. //设置停止位
  65. private StopBits _stopBits = StopBits.One;
  66. public StopBits StopBits
  67. {
  68. get { return _stopBits; }
  69. set
  70. {
  71. _stopBits = value;
  72. RaisePropertyChanged();
  73. }
  74. }
  75. public SettingsUCViewModel()
  76. {
  77. }
  78. /// <summary>
  79. /// 从容器中获取创建的窗口
  80. /// </summary>
  81. /// <param name="serialPort"></param>
  82. public SettingsUCViewModel(SerialPort serialPort)
  83. {
  84. _serialPort = serialPort;
  85. ConnectionCmm = new DelegateCommand(Connection);
  86. DisConnectionCmm = new DelegateCommand(DisConnection);
  87. }
  88. private void DisConnection()
  89. {
  90. if (_serialPort.IsOpen)
  91. {
  92. _serialPort.Close();
  93. }
  94. }
  95. /// <summary>
  96. /// 串口参数设置
  97. /// </summary>
  98. private void Connection()
  99. {
  100. //todo,做数据的判空处理
  101. if (!_serialPort.IsOpen)
  102. {
  103. try
  104. {
  105. _serialPort.PortName = GetComboBoxItemValue(this.PortName);
  106. _serialPort.BaudRate = int.Parse(GetComboBoxItemValue(this.BaudRate));
  107. _serialPort.Parity = this.Parity;
  108. _serialPort.StopBits = this.StopBits;
  109. _serialPort.DataBits = int.Parse(GetComboBoxItemValue(this.DataBits));
  110. _serialPort.Open();
  111. MessageBox.Show("串口链接成功");
  112. }
  113. catch (Exception)
  114. {
  115. MessageBox.Show("串口已经链接,请先断开链接在尝试");
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 处理下拉框的选择信息
  121. /// </summary>
  122. /// <param name="ComboBoxItem"></param>
  123. /// <returns></returns>
  124. private string GetComboBoxItemValue(string ComboBoxItem)
  125. {
  126. string displayText = ComboBoxItem.ToString();
  127. string cleanText = displayText.Replace("System.Windows.Controls.ComboBoxItem: ", "");
  128. return cleanText;
  129. }
  130. }
  131. }