ComConfig.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace GUI
  12. {
  13. public partial class ComConfig : Form, IComAgentDelegate
  14. {
  15. ComAgent agent;
  16. public ComConfig()
  17. {
  18. InitializeComponent();
  19. serialOutput.ScrollBars = ScrollBars.Vertical;
  20. agent = new ComAgent();
  21. agent.agentDelegate = this;
  22. }
  23. void ClearOutput()
  24. {
  25. serialOutput.Text = "";
  26. agent.Close();
  27. CloseButton.Enabled = false;
  28. goodMessageReceived = false;
  29. }
  30. void RefreshPort()
  31. {
  32. ClearOutput();
  33. RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
  34. if (keyCom != null)
  35. {
  36. string[] sSubKeys = keyCom.GetValueNames();
  37. sPort.Items.Clear();
  38. foreach (string sName in sSubKeys)
  39. {
  40. string sValue = (string)keyCom.GetValue(sName);
  41. sPort.Items.Add(sValue);
  42. }
  43. if (sPort.Items.Count > 0)
  44. sPort.SelectedIndex = 0;
  45. }
  46. }
  47. private void ComConfig_Load(object sender, EventArgs e)
  48. {
  49. RefreshPort();
  50. }
  51. private void sPort_SelectedIndexChanged(object sender, EventArgs e)
  52. {
  53. ClearOutput();
  54. agent.PortName = sPort.Text;
  55. }
  56. private void sRate_SelectedIndexChanged(object sender, EventArgs e)
  57. {
  58. ClearOutput();
  59. agent.BaudRate = int.Parse(sRate.Text);
  60. }
  61. private void CloseClick(object sender, EventArgs e)
  62. {
  63. if (!goodMessageReceived)
  64. {
  65. if (MessageBox.Show(
  66. "截止到目前,还没有从该串口接收到一条通过校验的数据,是否仍然继续连接?",
  67. "串口连接提示",
  68. MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
  69. return;
  70. }
  71. agent.agentDelegate = null;
  72. Program.comAgent = agent;
  73. Close();
  74. }
  75. private void RefreshClick(object sender, EventArgs e)
  76. {
  77. RefreshPort();
  78. }
  79. private void TryConnectionClick(object sender, EventArgs e)
  80. {
  81. try
  82. {
  83. agent.Open();
  84. agent.SendFrame();
  85. CloseButton.Enabled = true;
  86. }
  87. catch (Exception ex)
  88. {
  89. WriteLine(ex.Message);
  90. }
  91. }
  92. void WriteLine(string msg)
  93. {
  94. serialOutput.BeginInvoke((Action)(() =>
  95. {
  96. if (serialOutput.Text.Length < 10000)
  97. serialOutput.AppendText(msg + "\r\n");
  98. }));
  99. }
  100. public void ReceiveError(string msg)
  101. {
  102. ClearOutput();
  103. WriteLine(msg);
  104. }
  105. bool goodMessageReceived = false;
  106. public void ReceiveSuccess()
  107. {
  108. WriteLine("Receive good message.");
  109. goodMessageReceived = true;
  110. CloseButton.Enabled = true;
  111. }
  112. }
  113. }