Weigher.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.IO.Ports;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using 优软MES.CustomControl;
  9. namespace 优软MES
  10. {
  11. public partial class Weigher : Form
  12. {
  13. [DllImport("user32.dll")]
  14. public static extern bool ReleaseCapture();
  15. [DllImport("user32.dll")]
  16. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  17. public const int WM_SYSCOMMAND = 0x0112;
  18. public const int SC_MOVE = 0xF010;
  19. public const int HTCAPTION = 0x0002;
  20. string LastData="";
  21. //实例化一个串口
  22. SerialPort serialPort1 = new SerialPort();
  23. Thread thread;
  24. bool getData;
  25. public Weigher()
  26. {
  27. InitializeComponent();
  28. CheckForIllegalCrossThreadCalls = false;
  29. GetComList();
  30. }
  31. private void Weigher_Load(object sender, EventArgs e)
  32. {
  33. this.headBar1.MouseDown += new MouseEventHandler(headBar_MouseDown);
  34. }
  35. private void GetSeriadlData_Click(object sender, EventArgs e)
  36. {
  37. getData = true;
  38. thread = new Thread(getSerialData);
  39. thread.Start();
  40. }
  41. private void StopGetSeriadlData_Click(object sender, EventArgs e)
  42. {
  43. getData = false;
  44. Thread.Sleep(1000);
  45. serialPort1.Close();
  46. }
  47. //开始接受串口数据
  48. private void getSerialData(){
  49. if (!serialPort1.IsOpen)
  50. {
  51. serialPort1.PortName = this.comboBox1.Text;
  52. serialPort1.BaudRate =int.Parse(BaudRate.Text);
  53. serialPort1.Open();
  54. DataFlush.Text = "开始接受数据\n";
  55. while (getData)
  56. {
  57. try { ShowData(serialPort1.ReadLine() + "\n"); }
  58. catch (IOException ex) { MessageBox.Show(ex.Message); }
  59. }
  60. }
  61. else {
  62. MessageBox.Show("请勿重复打开端口", "警告");
  63. }
  64. }
  65. // 获取串口列表
  66. private void GetComList()
  67. {
  68. RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
  69. if (keyCom != null)
  70. {
  71. string[] sSubKeys = keyCom.GetValueNames();
  72. //获取串口的名称
  73. //for (int i = 0; i < sSubKeys.Length; i++)
  74. //{
  75. // string sValue = (string)(sSubKeys[i]);
  76. // comboBox1.Items.Add(sValue);
  77. // comboBox1.SelectedIndex = i;
  78. //}
  79. //获取串口号
  80. for (int i = 0; i < sSubKeys.Length; i++)
  81. {
  82. string sValue = (string)keyCom.GetValue(sSubKeys[i]);
  83. comboBox1.Items.Add(sValue);
  84. comboBox1.SelectedIndex = i;
  85. }
  86. }
  87. }
  88. private void headBar_MouseDown(object sender, MouseEventArgs e)
  89. {
  90. ReleaseCapture();
  91. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  92. }
  93. private void ShowData(string Data) {
  94. //判断数据是否稳定,将稳定值展示到称量结果
  95. if (Data.Substring(0, 1) == "S")
  96. {
  97. WeigherData.Text = Data;
  98. }
  99. //判断数据和上一条是否相等
  100. if (!(LastData == Data)) {
  101. DataFlush.AppendText(Data);
  102. }
  103. LastData = Data;
  104. }
  105. private void Weigher_FormClosing(object sender, FormClosingEventArgs e)
  106. {
  107. if (serialPort1.IsOpen)
  108. {
  109. MessageBoxButtons messButton = MessageBoxButtons.YesNo;
  110. string logout_confirm = MessageBox.Show(this.ParentForm, "端口尚未关闭,仍要关闭窗口?", "警告!", messButton).ToString();
  111. if (!(logout_confirm == "Yes"))
  112. {
  113. e.Cancel = true;
  114. }
  115. }
  116. }
  117. }
  118. }