SystemSetting_ScaleTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = this.ComList.Text;
  51. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  52. BaseUtil.SetCacheData("PortName", this.ComList.Text);
  53. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  54. serialPort1.Open();
  55. thread.Start();
  56. }
  57. catch (Exception mes) { MessageBox.Show(mes.Message); }
  58. }
  59. private void GetSerialData()
  60. {
  61. if (serialPort1.IsOpen)
  62. {
  63. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  64. {
  65. SystemInf.OpenPort.Add(serialPort1.PortName);
  66. try
  67. {
  68. while (ReadData)
  69. {
  70. try
  71. {
  72. int len = serialPort1.BytesToRead;
  73. Byte[] readBuffer = new Byte[len];
  74. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  75. string weigh = Encoding.Default.GetString(readBuffer);
  76. Remark.AppendText(weigh+"\n");
  77. if (weigh != "")
  78. {
  79. Weight.Text = weigh;
  80. }
  81. }
  82. catch (Exception)
  83. {
  84. ReadData = false;
  85. }
  86. }
  87. }
  88. catch (IOException ex) { MessageBox.Show(ex.Message); }
  89. }
  90. else
  91. MessageBox.Show("端口已被占用,请关闭其他窗口");
  92. }
  93. }
  94. private void SystemSetting_ScaleTest_FormClosing(object sender, FormClosingEventArgs e)
  95. {
  96. if (serialPort1.IsOpen)
  97. {
  98. ReadData = false;
  99. serialPort1.Close();
  100. SystemInf.OpenPort.Remove(serialPort1.PortName);
  101. thread.Interrupt();
  102. }
  103. //thread.Abort();
  104. }
  105. private void StopTest_Click(object sender, EventArgs e)
  106. {
  107. if (serialPort1.IsOpen)
  108. {
  109. ReadData = false;
  110. serialPort1.Close();
  111. SystemInf.OpenPort.Remove(serialPort1.PortName);
  112. thread.Abort();
  113. }
  114. }
  115. }
  116. }