AutoMakeQTY.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.Text;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.IO.Ports;
  12. using System.Linq.Expressions;
  13. using System.Linq;
  14. namespace FileWatcher
  15. {
  16. public partial class AutoMakeQTY : Form
  17. {
  18. DataHelper dh;
  19. DataTable dt;
  20. DataTable DB;
  21. /// <summary>
  22. /// 用户编号
  23. /// </summary>
  24. string iusercode;
  25. /// <summary>
  26. /// 岗位资源
  27. /// </summary>
  28. string isource;
  29. Thread InitDB;
  30. /// 当前工序
  31. /// </summary>
  32. string istepcode;
  33. StringBuilder sql = new StringBuilder();
  34. /// <summary>
  35. /// 缓存的文件
  36. /// </summary>
  37. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  38. /// <summary>
  39. /// 缓存的文件夹
  40. /// </summary>
  41. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  42. ftpOperater ftp = new ftpOperater();
  43. public AutoMakeQTY()
  44. {
  45. InitializeComponent();
  46. StartPosition = FormStartPosition.CenterScreen;
  47. }
  48. DataTable Dbfind;
  49. string IPAddress = "";
  50. private void nr_rule_DBChange(object sender, EventArgs e)
  51. {
  52. Dbfind = li_code.ReturnData;
  53. BaseUtil.SetFormValue(this.Controls, Dbfind);
  54. }
  55. private void Form1_Load(object sender, EventArgs e)
  56. {
  57. byte[] b2 = new byte[4];
  58. float f2;
  59. string tmp_b2 = "43666AEC";
  60. tmp_b2 = tmp_b2.Replace(" ", "");//删除字符串中间的空格
  61. if (tmp_b2.Length != 8)
  62. {
  63. //判断输入字符串的长度是否合适
  64. MessageBox.Show("输入Byte长度错误,为连续8位4个字节,高位为0的补齐0");
  65. }
  66. else
  67. {
  68. //将收到的字符串分成4个字节,这4个字节是字符串的形式
  69. string tmp_char1 = tmp_b2.Substring(0, 2);
  70. string tmp_char2 = tmp_b2.Substring(2, 2);
  71. string tmp_char3 = tmp_b2.Substring(4, 2);
  72. string tmp_char4 = tmp_b2.Substring(6, 2);
  73. //将4字节的字符串格式转换成16进制的byte格式
  74. b2[3] = Convert.ToByte(tmp_char1, 16);
  75. b2[2] = Convert.ToByte(tmp_char2, 16);
  76. b2[1] = Convert.ToByte(tmp_char3, 16);
  77. b2[0] = Convert.ToByte(tmp_char4, 16);
  78. //将16进制byte转换成浮点数格式
  79. f2 = BitConverter.ToSingle(b2, 0);
  80. Console.WriteLine(Convert.ToString(f2)); ;
  81. }
  82. CheckForIllegalCrossThreadCalls = false;
  83. FormBorderStyle = FormBorderStyle.FixedSingle;
  84. serialPort1.DataReceived += SerialPort1_DataReceived;
  85. InitDB = new Thread(ConnectDB);
  86. //添加监控事件
  87. XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  88. //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  89. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  90. stw.StartPosition = FormStartPosition.CenterScreen;
  91. stw.ShowDialog();
  92. List<string> CacheInf = new List<string>();
  93. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  94. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  95. {
  96. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  97. IPAddress = IpEntry.AddressList[i].ToString();
  98. }
  99. //获取缓存信息
  100. try
  101. {
  102. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  103. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  104. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  105. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  106. }
  107. catch (Exception ex) { MessageBox.Show(ex.Message); }
  108. }
  109. double lastqty = 0;
  110. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  111. {
  112. int len = serialPort1.BytesToRead;
  113. Byte[] readBuffer = new Byte[len];
  114. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  115. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  116. //Convert.ToInt32(data.Substring(6, 8), 16);
  117. //Console.WriteLine(data.Substring(6, 2));
  118. //double Num1 = double.Parse(data.Substring(8, 2)) * 10000;
  119. //Console.WriteLine(data.Substring(8, 2));
  120. //double Num2 = double.Parse(data.Substring(10, 2)) * 1000;
  121. //Console.WriteLine(data.Substring(10, 2));
  122. //double Num3 = double.Parse(data.Substring(12, 2)) * 100;
  123. //Console.WriteLine(data.Substring(12, 2));
  124. //double Num4 = double.Parse(data.Substring(14, 2)) * 10;
  125. //Console.WriteLine(data.Substring(14, 2));
  126. //double Num5 = double.Parse(data.Substring(16, 2));
  127. if (data != "")
  128. {
  129. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  130. if (lastqty > qty)
  131. {
  132. lastqty = 0;
  133. }
  134. NowQTY.Text = qty.ToString();
  135. if (Type.Text == "投入")
  136. {
  137. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  138. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  139. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  140. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  141. }
  142. else
  143. {
  144. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  145. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  146. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  147. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  148. }
  149. lastqty = qty;
  150. }
  151. }
  152. private void ConnectDB()
  153. {
  154. dh = new DataHelper();
  155. SystemInf.dh = dh;
  156. ma_code.TableName = "make";
  157. ma_code.SelectField = "ma_code # 工单号";
  158. ma_code.FormName = Name;
  159. ma_code.DBTitle = "工单查询";
  160. ma_code.SetValueField = new string[] { "ma_code" };
  161. ma_code.Condition = "";
  162. ma_code.DbChange += nr_rule_DBChange;
  163. li_code.TableName = "line";
  164. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  165. li_code.FormName = Name;
  166. li_code.DBTitle = "线别查询";
  167. li_code.SetValueField = new string[] { "li_code" };
  168. li_code.Condition = "";
  169. li_code.DbChange += nr_rule_DBChange;
  170. }
  171. SerialPort serialPort1 = new SerialPort();
  172. private void StartWatch_Click(object sender, EventArgs e)
  173. {
  174. BaseUtil.SetCacheData("Type", Type.Text);
  175. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  176. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  177. BaseUtil.SetCacheData("Line", li_code.Text);
  178. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  179. {
  180. OperateResult.AppendText("工单号不能为空\n");
  181. return;
  182. }
  183. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  184. {
  185. OperateResult.AppendText("线别不能为空\n");
  186. return;
  187. }
  188. if (Type.Text == "印刷机")
  189. {
  190. Timer.Start();
  191. }
  192. else
  193. {
  194. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  195. if (ExitConfirm != "Yes")
  196. {
  197. return;
  198. }
  199. //设置按钮不可点击
  200. StartWatch.Enabled = false;
  201. ma_code.Enabled = false;
  202. StopWatch.Enabled = true;
  203. try
  204. {
  205. serialPort1.PortName = ComPort.Text;
  206. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  207. serialPort1.Open();
  208. Timer.Start();
  209. }
  210. catch (Exception mes)
  211. {
  212. if (BaudRate.Text == "" || BaudRate.Text == "")
  213. OperateResult.AppendText(">>请先维护波特率和串口\n");
  214. else
  215. OperateResult.AppendText(">>" + mes.Message + "\n");
  216. }
  217. }
  218. OperateResult.AppendText("开始执行监控\n");
  219. }
  220. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  221. {
  222. OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
  223. TxtHandleProcess(e.FullPath);
  224. }
  225. string nextLine;
  226. private void TxtHandleProcess(string FileName)
  227. {
  228. List<string> badcode = new List<string>();
  229. List<string> badlocation = new List<string>();
  230. List<string> badprod = new List<string>();
  231. }
  232. private void StopWatch_Click(object sender, EventArgs e)
  233. {
  234. XmlWatcher.EnableRaisingEvents = false;
  235. StartWatch.Enabled = true;
  236. ma_code.Enabled = true;
  237. StopWatch.Enabled = false;
  238. OperateResult.AppendText("停止执行监控\n");
  239. }
  240. private void Clean_Click(object sender, EventArgs e)
  241. {
  242. OperateResult.Clear();
  243. }
  244. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  245. {
  246. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  247. if (ExitConfirm != "Yes")
  248. {
  249. WindowState = FormWindowState.Minimized;
  250. e.Cancel = true;
  251. }
  252. }
  253. private void Timer_Tick(object sender, EventArgs e)
  254. {
  255. if (serialPort1.IsOpen)
  256. {
  257. byte[] data = HexStringToBytes("01 03 00 01 00 06 94 08");
  258. serialPort1.Write(data, 0, data.Length);
  259. }
  260. }
  261. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  262. {
  263. SetAutoRun();
  264. }
  265. private void SetAutoRun()
  266. {
  267. if (AutoStart.Checked) //设置开机自启动
  268. {
  269. string path = Application.ExecutablePath;
  270. RegistryKey rk = Registry.LocalMachine;
  271. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  272. rk2.SetValue("FileWatcher.exe", path);
  273. rk2.Close();
  274. rk.Close();
  275. }
  276. else //取消开机自启动
  277. {
  278. string path = Application.ExecutablePath;
  279. RegistryKey rk = Registry.LocalMachine;
  280. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  281. rk2.DeleteValue("FileWatcher.exe", false);
  282. rk2.Close();
  283. rk.Close();
  284. }
  285. }
  286. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  287. {
  288. string a = hs.Replace(" ", "");
  289. int bytelength = 0;
  290. if (a.Length % 2 == 0)
  291. {
  292. bytelength = a.Length / 2;
  293. }
  294. else
  295. {
  296. bytelength = a.Length / 2 + 1;
  297. }
  298. byte[] b = new byte[bytelength];
  299. //逐个字符变为16进制字节数据
  300. for (int i = 0; i < bytelength; i++)
  301. {
  302. if (i == bytelength - 1)
  303. {
  304. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  305. }
  306. else
  307. {
  308. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  309. }
  310. }
  311. //按照指定编码将字节数组变为字符串
  312. return b;
  313. }
  314. private void ChooseFolder_Click(object sender, EventArgs e)
  315. {
  316. FolderBrowserDialog folder = new FolderBrowserDialog();
  317. folder.Description = "选择监控文件夹";
  318. DialogResult result = folder.ShowDialog();
  319. if (result == DialogResult.OK)
  320. {
  321. }
  322. }
  323. }
  324. }