SystemSetting_ScaleTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if (this.ComList.Text == "")
  37. {
  38. MessageBox.Show("端口号不可为空");
  39. return;
  40. }
  41. if (this.BaudRate.Text == "")
  42. {
  43. MessageBox.Show("波特率不可为空");
  44. return;
  45. }
  46. thread = new Thread(GetSerialData);
  47. try
  48. {
  49. ReadData = true;
  50. serialPort1.PortName = "COM3"; // 确保是存在的端口
  51. serialPort1.BaudRate = 9600; // 必须与设备一致
  52. serialPort1.DataBits = 8; // 通常为8
  53. serialPort1.Parity = Parity.None; // 通常为None
  54. serialPort1.StopBits = StopBits.One; // 通常为1
  55. serialPort1.Handshake = Handshake.None; // 通常为None
  56. BaseUtil.SetCacheData("PortName", this.ComList.Text);
  57. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  58. serialPort1.Open();
  59. thread.Start();
  60. }
  61. catch (Exception mes) { MessageBox.Show(mes.Message + mes.StackTrace); }
  62. }
  63. private void GetSerialData()
  64. {
  65. if (serialPort1.IsOpen)
  66. {
  67. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  68. {
  69. SystemInf.OpenPort.Add(serialPort1.PortName);
  70. try
  71. {
  72. while (ReadData)
  73. {
  74. try
  75. {
  76. Weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value.Replace("kg","");
  77. }
  78. catch (Exception)
  79. {
  80. ReadData = false;
  81. }
  82. }
  83. }
  84. catch (IOException ex) { MessageBox.Show(ex.Message); }
  85. }
  86. else
  87. MessageBox.Show("端口已被占用,请关闭其他窗口");
  88. }
  89. }
  90. private void SystemSetting_ScaleTest_FormClosing(object sender, FormClosingEventArgs e)
  91. {
  92. if (serialPort1.IsOpen)
  93. {
  94. ReadData = false;
  95. serialPort1.Close();
  96. SystemInf.OpenPort.Remove(serialPort1.PortName);
  97. thread.Interrupt();
  98. }
  99. //thread.Abort();
  100. }
  101. private void StopTest_Click(object sender, EventArgs e)
  102. {
  103. if (serialPort1.IsOpen)
  104. {
  105. ReadData = false;
  106. serialPort1.Close();
  107. SystemInf.OpenPort.Remove(serialPort1.PortName);
  108. thread.Abort();
  109. }
  110. }
  111. }
  112. }