SystemSetting_ScaleTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using NPOI.SS.Formula.Functions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.IO.Ports;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. using UAS_MES_NEW.DataOperate;
  15. using UAS_MES_NEW.Entity;
  16. using UAS_MES_NEW.PublicMethod;
  17. namespace UAS_MES_NEW.SystemSetting
  18. {
  19. public partial class SystemSetting_ScaleTest : Form
  20. {
  21. Thread thread;
  22. bool ReadData = true;
  23. SerialPort serialPort1 = new SerialPort();
  24. Regex re = new Regex("\\d+.\\d+");
  25. public SystemSetting_ScaleTest()
  26. {
  27. InitializeComponent();
  28. }
  29. private void SystemSetting_ScaleTest_Load(object sender, EventArgs e)
  30. {
  31. CheckForIllegalCrossThreadCalls = false;
  32. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  33. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  34. }
  35. private void StartTest_Click(object sender, EventArgs e)
  36. {
  37. if (this.ComList.Text == "")
  38. {
  39. MessageBox.Show("端口号不可为空");
  40. return;
  41. }
  42. if (this.BaudRate.Text == "")
  43. {
  44. MessageBox.Show("波特率不可为空");
  45. return;
  46. }
  47. thread = new Thread(GetSerialData);
  48. try
  49. {
  50. ReadData = true;
  51. serialPort1.PortName = this.ComList.Text;
  52. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  53. BaseUtil.SetCacheData("PortName", this.ComList.Text);
  54. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  55. serialPort1.Open();
  56. thread.Start();
  57. }
  58. catch (Exception mes) { MessageBox.Show(mes.Message); }
  59. }
  60. private void GetSerialData()
  61. {
  62. if (serialPort1.IsOpen)
  63. {
  64. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  65. {
  66. SystemInf.OpenPort.Add(serialPort1.PortName);
  67. try
  68. {
  69. while (ReadData)
  70. {
  71. try
  72. {
  73. int len = serialPort1.BytesToRead;
  74. Byte[] readBuffer = new Byte[len];
  75. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  76. string weigh = Encoding.Default.GetString(readBuffer);
  77. Remark.AppendText(weigh+"\n");
  78. if (weigh != "")
  79. {
  80. Weight.Text = re.Match(weigh).Groups[0].Value;
  81. }
  82. }
  83. catch (Exception)
  84. {
  85. ReadData = false;
  86. }
  87. }
  88. }
  89. catch (IOException ex) { MessageBox.Show(ex.Message); }
  90. }
  91. else
  92. MessageBox.Show("端口已被占用,请关闭其他窗口");
  93. }
  94. }
  95. private void SystemSetting_ScaleTest_FormClosing(object sender, FormClosingEventArgs e)
  96. {
  97. if (serialPort1.IsOpen)
  98. {
  99. ReadData = false;
  100. serialPort1.Close();
  101. SystemInf.OpenPort.Remove(serialPort1.PortName);
  102. thread.Interrupt();
  103. }
  104. //thread.Abort();
  105. }
  106. private void StopTest_Click(object sender, EventArgs e)
  107. {
  108. if (serialPort1.IsOpen)
  109. {
  110. ReadData = false;
  111. serialPort1.Close();
  112. SystemInf.OpenPort.Remove(serialPort1.PortName);
  113. thread.Abort();
  114. }
  115. }
  116. }
  117. }