AutoMakeQTY.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using System.Text;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Text.RegularExpressions;
  13. using System.IO.Ports;
  14. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  15. using System.Windows.Media.Media3D;
  16. using System.Runtime.InteropServices;
  17. namespace FileWatcher
  18. {
  19. public partial class AutoMakeQTY : Form
  20. {
  21. DataHelper dh;
  22. DataTable dt;
  23. DataTable DB;
  24. /// <summary>
  25. /// 用户编号
  26. /// </summary>
  27. string iusercode;
  28. /// <summary>
  29. /// 岗位资源
  30. /// </summary>
  31. string isource;
  32. Thread InitDB;
  33. /// 当前工序
  34. /// </summary>
  35. string istepcode;
  36. StringBuilder sql = new StringBuilder();
  37. /// <summary>
  38. /// 缓存的文件
  39. /// </summary>
  40. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  41. /// <summary>
  42. /// 缓存的文件夹
  43. /// </summary>
  44. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  45. ftpOperater ftp = new ftpOperater();
  46. public AutoMakeQTY()
  47. {
  48. InitializeComponent();
  49. StartPosition = FormStartPosition.CenterScreen;
  50. }
  51. string IPAddress = "";
  52. private void Form1_Load(object sender, EventArgs e)
  53. {
  54. CheckForIllegalCrossThreadCalls = false;
  55. FormBorderStyle = FormBorderStyle.FixedSingle;
  56. serialPort1.DataReceived += SerialPort1_DataReceived;
  57. InitDB = new Thread(ConnectDB);
  58. //添加监控事件
  59. XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  60. //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  61. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  62. stw.StartPosition = FormStartPosition.CenterScreen;
  63. stw.ShowDialog();
  64. List<string> CacheInf = new List<string>();
  65. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  66. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  67. {
  68. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  69. IPAddress = IpEntry.AddressList[i].ToString();
  70. }
  71. //获取缓存信息
  72. try
  73. {
  74. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  75. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  76. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  77. }
  78. catch (Exception ex) { MessageBox.Show(ex.Message); }
  79. }
  80. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  81. {
  82. int len = serialPort1.BytesToRead;
  83. Byte[] readBuffer = new Byte[len];
  84. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  85. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  86. double Num1 = double.Parse(data.Substring(8, 2)) * 10000;
  87. double Num2 = double.Parse(data.Substring(10, 2)) * 1000;
  88. double Num3 = double.Parse(data.Substring(12, 2)) * 100;
  89. double Num4 = double.Parse(data.Substring(14, 2)) * 10;
  90. double Num5 = double.Parse(data.Substring(16, 2));
  91. double qty = Num1 + Num2 + Num3 + Num4 + Num5;
  92. NowQTY.Text = qty.ToString();
  93. }
  94. private void ConnectDB()
  95. {
  96. dh = new DataHelper();
  97. }
  98. SerialPort serialPort1 = new SerialPort();
  99. private void StartWatch_Click(object sender, EventArgs e)
  100. {
  101. BaseUtil.SetCacheData("Type", Type.Text);
  102. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  103. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  104. //设置按钮不可点击
  105. StartWatch.Enabled = false;
  106. ma_code.Enabled = false;
  107. StopWatch.Enabled = true;
  108. try
  109. {
  110. serialPort1.PortName = ComPort.Text;
  111. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  112. serialPort1.Open();
  113. }
  114. catch (Exception mes)
  115. {
  116. if (BaudRate.Text == "" || BaudRate.Text == "")
  117. OperateResult.AppendText(">>请先维护波特率和串口\n");
  118. else
  119. OperateResult.AppendText(">>" + mes.Message + "\n");
  120. }
  121. OperateResult.AppendText("开始执行监控\n");
  122. Timer.Start();
  123. }
  124. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  125. {
  126. OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
  127. TxtHandleProcess(e.FullPath);
  128. }
  129. string nextLine;
  130. private void TxtHandleProcess(string FileName)
  131. {
  132. List<string> badcode = new List<string>();
  133. List<string> badlocation = new List<string>();
  134. List<string> badprod = new List<string>();
  135. }
  136. private void StopWatch_Click(object sender, EventArgs e)
  137. {
  138. XmlWatcher.EnableRaisingEvents = false;
  139. StartWatch.Enabled = true;
  140. ma_code.Enabled = true;
  141. StopWatch.Enabled = false;
  142. OperateResult.AppendText("停止执行监控\n");
  143. }
  144. private void Clean_Click(object sender, EventArgs e)
  145. {
  146. OperateResult.Clear();
  147. }
  148. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  149. {
  150. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  151. if (ExitConfirm != "Yes")
  152. {
  153. WindowState = FormWindowState.Minimized;
  154. e.Cancel = true;
  155. }
  156. }
  157. private void Timer_Tick(object sender, EventArgs e)
  158. {
  159. byte[] data = HexStringToBytes("01 03 00 01 00 06 94 08");
  160. serialPort1.Write(data, 0, data.Length);
  161. }
  162. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  163. {
  164. SetAutoRun();
  165. }
  166. private void SetAutoRun()
  167. {
  168. if (AutoStart.Checked) //设置开机自启动
  169. {
  170. string path = Application.ExecutablePath;
  171. RegistryKey rk = Registry.LocalMachine;
  172. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  173. rk2.SetValue("FileWatcher.exe", path);
  174. rk2.Close();
  175. rk.Close();
  176. }
  177. else //取消开机自启动
  178. {
  179. string path = Application.ExecutablePath;
  180. RegistryKey rk = Registry.LocalMachine;
  181. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  182. rk2.DeleteValue("FileWatcher.exe", false);
  183. rk2.Close();
  184. rk.Close();
  185. }
  186. }
  187. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  188. {
  189. string a = hs.Replace(" ", "");
  190. int bytelength = 0;
  191. if (a.Length % 2 == 0)
  192. {
  193. bytelength = a.Length / 2;
  194. }
  195. else
  196. {
  197. bytelength = a.Length / 2 + 1;
  198. }
  199. byte[] b = new byte[bytelength];
  200. //逐个字符变为16进制字节数据
  201. for (int i = 0; i < bytelength; i++)
  202. {
  203. if (i == bytelength - 1)
  204. {
  205. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  206. }
  207. else
  208. {
  209. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  210. }
  211. }
  212. //按照指定编码将字节数组变为字符串
  213. return b;
  214. }
  215. }
  216. }