AutoMakeQTY.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. DataTable Dbfind;
  52. string IPAddress = "";
  53. private void nr_rule_DBChange(object sender, EventArgs e)
  54. {
  55. Dbfind = li_code.ReturnData;
  56. BaseUtil.SetFormValue(this.Controls, Dbfind);
  57. }
  58. private void Form1_Load(object sender, EventArgs e)
  59. {
  60. CheckForIllegalCrossThreadCalls = false;
  61. FormBorderStyle = FormBorderStyle.FixedSingle;
  62. serialPort1.DataReceived += SerialPort1_DataReceived;
  63. InitDB = new Thread(ConnectDB);
  64. //添加监控事件
  65. XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  66. //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  67. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  68. stw.StartPosition = FormStartPosition.CenterScreen;
  69. stw.ShowDialog();
  70. List<string> CacheInf = new List<string>();
  71. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  72. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  73. {
  74. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  75. IPAddress = IpEntry.AddressList[i].ToString();
  76. }
  77. //获取缓存信息
  78. try
  79. {
  80. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  81. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  82. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  83. }
  84. catch (Exception ex) { MessageBox.Show(ex.Message); }
  85. }
  86. double lastqty = 0;
  87. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  88. {
  89. int len = serialPort1.BytesToRead;
  90. Byte[] readBuffer = new Byte[len];
  91. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  92. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  93. //Convert.ToInt32(data.Substring(6, 8), 16);
  94. //Console.WriteLine(data.Substring(6, 2));
  95. //double Num1 = double.Parse(data.Substring(8, 2)) * 10000;
  96. //Console.WriteLine(data.Substring(8, 2));
  97. //double Num2 = double.Parse(data.Substring(10, 2)) * 1000;
  98. //Console.WriteLine(data.Substring(10, 2));
  99. //double Num3 = double.Parse(data.Substring(12, 2)) * 100;
  100. //Console.WriteLine(data.Substring(12, 2));
  101. //double Num4 = double.Parse(data.Substring(14, 2)) * 10;
  102. //Console.WriteLine(data.Substring(14, 2));
  103. //double Num5 = double.Parse(data.Substring(16, 2));
  104. if (data != "")
  105. {
  106. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  107. if (lastqty > qty)
  108. {
  109. lastqty = 0;
  110. }
  111. NowQTY.Text = qty.ToString();
  112. if (Type.Text == "投入")
  113. {
  114. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  115. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  116. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode)" +
  117. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "')", "insert");
  118. }
  119. else
  120. {
  121. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  122. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  123. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode)" +
  124. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "'),'" + li_code.Text + "'", "insert");
  125. }
  126. lastqty = qty;
  127. }
  128. }
  129. private void ConnectDB()
  130. {
  131. dh = new DataHelper();
  132. SystemInf.dh = dh;
  133. ma_code.TableName = "make";
  134. ma_code.SelectField = "ma_code # 工单号";
  135. ma_code.FormName = Name;
  136. ma_code.DBTitle = "工单查询";
  137. ma_code.SetValueField = new string[] { "ma_code" };
  138. ma_code.Condition = "";
  139. ma_code.DbChange += nr_rule_DBChange;
  140. li_code.TableName = "line";
  141. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  142. li_code.FormName = Name;
  143. li_code.DBTitle = "线别查询";
  144. li_code.SetValueField = new string[] { "li_code" };
  145. li_code.Condition = "";
  146. li_code.DbChange += nr_rule_DBChange;
  147. }
  148. SerialPort serialPort1 = new SerialPort();
  149. private void StartWatch_Click(object sender, EventArgs e)
  150. {
  151. BaseUtil.SetCacheData("Type", Type.Text);
  152. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  153. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  154. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  155. {
  156. OperateResult.AppendText("工单号不能为空\n");
  157. return;
  158. }
  159. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  160. {
  161. OperateResult.AppendText("线别不能为空\n");
  162. return;
  163. }
  164. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  165. if (ExitConfirm != "Yes")
  166. {
  167. return;
  168. }
  169. //设置按钮不可点击
  170. StartWatch.Enabled = false;
  171. ma_code.Enabled = false;
  172. StopWatch.Enabled = true;
  173. try
  174. {
  175. serialPort1.PortName = ComPort.Text;
  176. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  177. serialPort1.Open();
  178. }
  179. catch (Exception mes)
  180. {
  181. if (BaudRate.Text == "" || BaudRate.Text == "")
  182. OperateResult.AppendText(">>请先维护波特率和串口\n");
  183. else
  184. OperateResult.AppendText(">>" + mes.Message + "\n");
  185. }
  186. OperateResult.AppendText("开始执行监控\n");
  187. Timer.Start();
  188. }
  189. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  190. {
  191. OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
  192. TxtHandleProcess(e.FullPath);
  193. }
  194. string nextLine;
  195. private void TxtHandleProcess(string FileName)
  196. {
  197. List<string> badcode = new List<string>();
  198. List<string> badlocation = new List<string>();
  199. List<string> badprod = new List<string>();
  200. }
  201. private void StopWatch_Click(object sender, EventArgs e)
  202. {
  203. XmlWatcher.EnableRaisingEvents = false;
  204. StartWatch.Enabled = true;
  205. ma_code.Enabled = true;
  206. StopWatch.Enabled = false;
  207. OperateResult.AppendText("停止执行监控\n");
  208. }
  209. private void Clean_Click(object sender, EventArgs e)
  210. {
  211. OperateResult.Clear();
  212. }
  213. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  214. {
  215. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  216. if (ExitConfirm != "Yes")
  217. {
  218. WindowState = FormWindowState.Minimized;
  219. e.Cancel = true;
  220. }
  221. }
  222. private void Timer_Tick(object sender, EventArgs e)
  223. {
  224. byte[] data = HexStringToBytes("01 03 00 01 00 06 94 08");
  225. serialPort1.Write(data, 0, data.Length);
  226. }
  227. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  228. {
  229. SetAutoRun();
  230. }
  231. private void SetAutoRun()
  232. {
  233. if (AutoStart.Checked) //设置开机自启动
  234. {
  235. string path = Application.ExecutablePath;
  236. RegistryKey rk = Registry.LocalMachine;
  237. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  238. rk2.SetValue("FileWatcher.exe", path);
  239. rk2.Close();
  240. rk.Close();
  241. }
  242. else //取消开机自启动
  243. {
  244. string path = Application.ExecutablePath;
  245. RegistryKey rk = Registry.LocalMachine;
  246. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  247. rk2.DeleteValue("FileWatcher.exe", false);
  248. rk2.Close();
  249. rk.Close();
  250. }
  251. }
  252. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  253. {
  254. string a = hs.Replace(" ", "");
  255. int bytelength = 0;
  256. if (a.Length % 2 == 0)
  257. {
  258. bytelength = a.Length / 2;
  259. }
  260. else
  261. {
  262. bytelength = a.Length / 2 + 1;
  263. }
  264. byte[] b = new byte[bytelength];
  265. //逐个字符变为16进制字节数据
  266. for (int i = 0; i < bytelength; i++)
  267. {
  268. if (i == bytelength - 1)
  269. {
  270. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  271. }
  272. else
  273. {
  274. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  275. }
  276. }
  277. //按照指定编码将字节数组变为字符串
  278. return b;
  279. }
  280. }
  281. }