SystemSetting_ScaleTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. using UAS_MES_NEW.PublicMethod;
  16. namespace UAS_MES_NEW.SystemSetting
  17. {
  18. public partial class SystemSetting_ScaleTest : Form
  19. {
  20. Thread thread;
  21. bool ReadData = true;
  22. SerialPort serialPort1 = new SerialPort();
  23. Regex re = new Regex("\\d+.\\d+\\w+");
  24. public SystemSetting_ScaleTest()
  25. {
  26. InitializeComponent();
  27. }
  28. private void SystemSetting_ScaleTest_Load(object sender, EventArgs e)
  29. {
  30. CheckForIllegalCrossThreadCalls = false;
  31. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  32. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  33. }
  34. private void StartTest_Click(object sender, EventArgs e)
  35. {
  36. using (SerialPort port = new SerialPort(ComList.Text, 9600, Parity.None, 8, StopBits.One))
  37. {
  38. try
  39. {
  40. port.Open();
  41. MessageBox.Show("串口打开成功!");
  42. port.Close();
  43. }
  44. catch (Exception ex)
  45. {
  46. MessageBox.Show($"失败: {ex.Message}");
  47. }
  48. }
  49. //if (this.ComList.Text == "")
  50. //{
  51. // MessageBox.Show("端口号不可为空");
  52. // return;
  53. //}
  54. //if (this.BaudRate.Text == "")
  55. //{
  56. // MessageBox.Show("波特率不可为空");
  57. // return;
  58. //}
  59. //thread = new Thread(GetSerialData);
  60. //try
  61. //{
  62. // ReadData = true;
  63. // serialPort1.PortName = "COM3"; // 确保是存在的端口
  64. // serialPort1.BaudRate = 9600; // 必须与设备一致
  65. // serialPort1.DataBits = 8; // 通常为8
  66. // serialPort1.Parity = Parity.None; // 通常为None
  67. // serialPort1.StopBits = StopBits.One; // 通常为1
  68. // serialPort1.Handshake = Handshake.None; // 通常为None
  69. // BaseUtil.SetCacheData("PortName", this.ComList.Text);
  70. // BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  71. // serialPort1.Open();
  72. // thread.Start();
  73. //}
  74. //catch (Exception mes) { MessageBox.Show(mes.Message + mes.StackTrace); }
  75. }
  76. private void GetSerialData()
  77. {
  78. if (serialPort1.IsOpen)
  79. {
  80. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  81. {
  82. SystemInf.OpenPort.Add(serialPort1.PortName);
  83. try
  84. {
  85. while (ReadData)
  86. {
  87. try
  88. {
  89. Weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value.Replace("kg","");
  90. }
  91. catch (Exception)
  92. {
  93. ReadData = false;
  94. }
  95. }
  96. }
  97. catch (IOException ex) { MessageBox.Show(ex.Message); }
  98. }
  99. else
  100. MessageBox.Show("端口已被占用,请关闭其他窗口");
  101. }
  102. }
  103. private void SystemSetting_ScaleTest_FormClosing(object sender, FormClosingEventArgs e)
  104. {
  105. if (serialPort1.IsOpen)
  106. {
  107. ReadData = false;
  108. serialPort1.Close();
  109. SystemInf.OpenPort.Remove(serialPort1.PortName);
  110. thread.Interrupt();
  111. }
  112. //thread.Abort();
  113. }
  114. private void StopTest_Click(object sender, EventArgs e)
  115. {
  116. if (serialPort1.IsOpen)
  117. {
  118. ReadData = false;
  119. serialPort1.Close();
  120. SystemInf.OpenPort.Remove(serialPort1.PortName);
  121. thread.Abort();
  122. }
  123. }
  124. }
  125. }