AutoMakeQTY.cs 9.1 KB

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