SystemSetting_ScaleTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. try { UAS_MES_NEW.I18n.LanguageManager.Localize(this); } catch { }
  29. }
  30. private void SystemSetting_ScaleTest_Load(object sender, EventArgs e)
  31. {
  32. CheckForIllegalCrossThreadCalls = false;
  33. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  34. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  35. }
  36. private void StartTest_Click(object sender, EventArgs e)
  37. {
  38. if (this.ComList.Text == "")
  39. {
  40. MessageBox.Show(UAS_MES_NEW.I18n.LanguageManager.T("端口号不可为空"));
  41. return;
  42. }
  43. if (this.BaudRate.Text == "")
  44. {
  45. MessageBox.Show(UAS_MES_NEW.I18n.LanguageManager.T("波特率不可为空"));
  46. return;
  47. }
  48. thread = new Thread(GetSerialData);
  49. try
  50. {
  51. ReadData = true;
  52. serialPort1.PortName = this.ComList.Text;
  53. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  54. BaseUtil.SetCacheData("PortName", this.ComList.Text);
  55. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  56. serialPort1.Open();
  57. thread.Start();
  58. }
  59. catch (Exception mes) { MessageBox.Show(mes.Message); }
  60. }
  61. private void GetSerialData()
  62. {
  63. if (serialPort1.IsOpen)
  64. {
  65. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  66. {
  67. SystemInf.OpenPort.Add(serialPort1.PortName);
  68. try
  69. {
  70. while (ReadData)
  71. {
  72. try
  73. {
  74. int len = serialPort1.BytesToRead;
  75. Byte[] readBuffer = new Byte[len];
  76. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  77. string weigh = Encoding.Default.GetString(readBuffer);
  78. Remark.AppendText(weigh+"\n");
  79. if (weigh != "")
  80. {
  81. Weight.Text = re.Match(weigh).Groups[0].Value;
  82. }
  83. }
  84. catch (Exception)
  85. {
  86. ReadData = false;
  87. }
  88. }
  89. }
  90. catch (IOException ex) { MessageBox.Show(ex.Message); }
  91. }
  92. else
  93. MessageBox.Show(UAS_MES_NEW.I18n.LanguageManager.T("端口已被占用,请关闭其他窗口"));
  94. }
  95. }
  96. private void SystemSetting_ScaleTest_FormClosing(object sender, FormClosingEventArgs e)
  97. {
  98. if (serialPort1.IsOpen)
  99. {
  100. ReadData = false;
  101. serialPort1.Close();
  102. SystemInf.OpenPort.Remove(serialPort1.PortName);
  103. thread.Interrupt();
  104. }
  105. //thread.Abort();
  106. }
  107. private void StopTest_Click(object sender, EventArgs e)
  108. {
  109. if (serialPort1.IsOpen)
  110. {
  111. ReadData = false;
  112. serialPort1.Close();
  113. SystemInf.OpenPort.Remove(serialPort1.PortName);
  114. thread.Abort();
  115. }
  116. }
  117. }
  118. }