SystemSetting_ScaleTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. SystemSetting_ScaleTest.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. Thread.Sleep(100);
  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. if (weigh != ""&& weigh.Length>10)
  78. {
  79. int firstIndex = weigh.IndexOf("="); // 查找第一个"="的索引
  80. int secondIndex = weigh.IndexOf("=", firstIndex + 1); // 查找第二个"="的索引
  81. string result = weigh.Substring(firstIndex + 1, secondIndex - firstIndex - 1);// 提取第一个"="和第二个"="之间的字符
  82. result = new string(result.ToCharArray().Reverse().ToArray());
  83. int index = 0;
  84. while (index < result.Length && result[index] == '0')
  85. {
  86. index++;
  87. }
  88. result = result.Substring(index);
  89. Remark.AppendText(result+"\n");
  90. Weight.Text = result;
  91. }
  92. }
  93. catch (Exception)
  94. {
  95. ReadData = false;
  96. }
  97. }
  98. }
  99. catch (IOException ex) { MessageBox.Show(ex.Message); }
  100. }
  101. else
  102. MessageBox.Show("端口已被占用,请关闭其他窗口");
  103. }
  104. }
  105. private void SystemSetting_ScaleTest_FormClosing(object sender, FormClosingEventArgs e)
  106. {
  107. if (serialPort1.IsOpen)
  108. {
  109. ReadData = false;
  110. serialPort1.Close();
  111. SystemInf.OpenPort.Remove(serialPort1.PortName);
  112. thread.Interrupt();
  113. }
  114. //thread.Abort();
  115. }
  116. private void StopTest_Click(object sender, EventArgs e)
  117. {
  118. if (serialPort1.IsOpen)
  119. {
  120. ReadData = false;
  121. serialPort1.Close();
  122. SystemInf.OpenPort.Remove(serialPort1.PortName);
  123. thread.Abort();
  124. }
  125. }
  126. }
  127. }