AutoMakeQTYRZ.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 AutoMakeQTYRZ : 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 AutoMakeQTYRZ()
  47. {
  48. InitializeComponent();
  49. StartPosition = FormStartPosition.CenterScreen;
  50. }
  51. DataTable Dbfind;
  52. string IPAddress = "";
  53. bool writedata = true;
  54. private void nr_rule_DBChange(object sender, EventArgs e)
  55. {
  56. Dbfind = li_code.ReturnData;
  57. BaseUtil.SetFormValue(this.Controls, Dbfind);
  58. }
  59. private void Form1_Load(object sender, EventArgs e)
  60. {
  61. CheckForIllegalCrossThreadCalls = false;
  62. InitDB = new Thread(ConnectDB);
  63. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  64. stw.StartPosition = FormStartPosition.CenterScreen;
  65. stw.ShowDialog();
  66. //string[] str = new string[] { "AA001097796643D67FE43D4185C74185EB47429A63783F64",
  67. // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
  68. // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
  69. // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
  70. // "AA0010685B664300000000BE2BC74185EB47423655783F1E",
  71. // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
  72. // "AA001097796643D67FE43D4185C74185EB47429A63783F64"};
  73. //for (int i = 0; i < str.Length; i++)
  74. //{
  75. // float 电压数据 = getdata(str[i].Substring(6, 8));
  76. // float 电流数据 = getdata(str[i].Substring(14, 8));
  77. // float 功率数据 = getdata(str[i].Substring(22, 8));
  78. // float 频率数据 = getdata(str[i].Substring(30, 8));
  79. // float 功率因素 = getdata(str[i].Substring(38, 8));
  80. // OperateResult.AppendText(str[i]);
  81. // OperateResult.AppendText(电压数据.ToString() + "\n");
  82. // OperateResult.AppendText(电流数据.ToString() + "\n");
  83. // OperateResult.AppendText(功率数据.ToString() + "\n");
  84. // OperateResult.AppendText(频率数据.ToString() + "\n");
  85. // OperateResult.AppendText(功率因素.ToString() + "\n");
  86. // //如果电流是0,则下次重新获取数据
  87. // if (电流数据 == 0)
  88. // {
  89. // writedata = false;
  90. // if (电流数据 == 0 && !writedata) {
  91. // writedata = true;
  92. // }
  93. // }
  94. // if (writedata)
  95. // {
  96. // if (电流数据 != 0) {
  97. // dh.ExecuteSql("insert into currenttest(CT_ID, CT_MAKECODE, CT_VOLTAGE, CT_CURRENT, CT_POWER, CT_FREQUENCY, CT_POWER1)" +
  98. // "VALUES(currenttest_SEQ.NEXTVAL,'" + ma_code.Text + "','" + 电压数据 + "','" + 电流数据 + "','" + 功率数据 + "','" + 频率数据 + "','" + 功率因素 + "')", "insert");
  99. // writedata = false;
  100. // }
  101. // }
  102. //}
  103. FormBorderStyle = FormBorderStyle.FixedSingle;
  104. serialPort1.DataReceived += SerialPort1_DataReceived;
  105. //添加监控事件
  106. XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  107. //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  108. List<string> CacheInf = new List<string>();
  109. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  110. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  111. {
  112. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  113. IPAddress = IpEntry.AddressList[i].ToString();
  114. }
  115. //获取缓存信息
  116. try
  117. {
  118. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  119. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  120. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  121. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  122. }
  123. catch (Exception ex) { MessageBox.Show(ex.Message); }
  124. }
  125. private float getdata(string tmp_b2)
  126. {
  127. byte[] b2 = new byte[4];
  128. float f2 = 0;
  129. tmp_b2 = tmp_b2.Replace(" ", "");//删除字符串中间的空格
  130. if (tmp_b2.Length != 8)
  131. {
  132. //判断输入字符串的长度是否合适
  133. MessageBox.Show("输入Byte长度错误,为连续8位4个字节,高位为0的补齐0");
  134. }
  135. else
  136. {
  137. //将收到的字符串分成4个字节,这4个字节是字符串的形式
  138. string tmp_char1 = tmp_b2.Substring(6, 2);
  139. string tmp_char2 = tmp_b2.Substring(4, 2);
  140. string tmp_char3 = tmp_b2.Substring(2, 2);
  141. string tmp_char4 = tmp_b2.Substring(0, 2);
  142. //将4字节的字符串格式转换成16进制的byte格式
  143. b2[3] = Convert.ToByte(tmp_char1, 16);
  144. b2[2] = Convert.ToByte(tmp_char2, 16);
  145. b2[1] = Convert.ToByte(tmp_char3, 16);
  146. b2[0] = Convert.ToByte(tmp_char4, 16);
  147. //将16进制byte转换成浮点数格式
  148. f2 = BitConverter.ToSingle(b2, 0);
  149. }
  150. return f2;
  151. }
  152. double lastqty = 0;
  153. string data = "";
  154. bool getSeconddata = false;
  155. bool realwritedata = false;
  156. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  157. {
  158. try
  159. {
  160. int len = serialPort1.BytesToRead;
  161. Byte[] readBuffer = new Byte[len];
  162. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  163. data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  164. if (data.Length > 48)
  165. {
  166. data = data.Substring(0, 48);
  167. }
  168. if (data.Length == 48)
  169. {
  170. float 电压数据 = getdata(data.Substring(6, 8));
  171. float 电流数据 = getdata(data.Substring(14, 8));
  172. float 功率数据 = getdata(data.Substring(22, 8));
  173. float 频率数据 = getdata(data.Substring(30, 8));
  174. float 功率因素 = getdata(data.Substring(38, 8));
  175. //如果电流是0,则下次重新获取数据
  176. if (电流数据 == 0)
  177. {
  178. writedata = false;
  179. if (电流数据 == 0 && !writedata)
  180. {
  181. writedata = true;
  182. }
  183. }
  184. if (writedata)
  185. {
  186. if (电流数据 != 0)
  187. {
  188. realwritedata = true;
  189. writedata = false;
  190. Thread.Sleep(2000);
  191. OperateResult.AppendText("获取数据\n");
  192. byte[] data = HexStringToBytes("55 00 10 65");
  193. serialPort1.Write(data, 0, data.Length);
  194. }
  195. }
  196. if (realwritedata)
  197. {
  198. if (电流数据 > 0)
  199. {
  200. double num;
  201. double num1;
  202. if (double.TryParse(FunctionNum.Text, out num) && double.TryParse(FunctionNum1.Text, out num1))
  203. {
  204. if (功率数据 > num && 功率数据 < num1)
  205. {
  206. OperateResult.AppendText("数据上传成功,功率[" + 功率数据 + "]\n");
  207. dh.ExecuteSql("insert into currenttest(CT_ID,ct_linecode, CT_MAKECODE, CT_VOLTAGE, CT_CURRENT, CT_POWER, CT_FREQUENCY, CT_POWER1)" +
  208. "VALUES(currenttest_SEQ.NEXTVAL,'" + li_code.Text + "','" + ma_code.Text + "','" + 电压数据 + "','" + 电流数据 + "','" + 功率数据 + "','" + 频率数据 + "','" + 功率因素 + "')", "insert");
  209. NowQTY.Text = dh.getRowCount("currenttest", "ct_makecode='" + ma_code.Text + "'").ToString();
  210. realwritedata = false;
  211. }
  212. else
  213. {
  214. OperateResult.AppendText("功率数据异常\n");
  215. }
  216. }
  217. else
  218. {
  219. OperateResult.AppendText("请填写功率标准值\n");
  220. }
  221. }
  222. }
  223. }
  224. else
  225. {
  226. //realwritedata = true;
  227. //writedata = false;
  228. //Thread.Sleep(1000);
  229. //OperateResult.AppendText("获取数据1\n");
  230. //byte[] data = HexStringToBytes("55 00 10 65");
  231. //serialPort1.Write(data, 0, data.Length);
  232. //OperateResult.AppendText(data + "数据格式异常\n");
  233. }
  234. }
  235. catch (Exception ex)
  236. {
  237. OperateResult.AppendText(ex.Message + data + "\n");
  238. }
  239. }
  240. private void ConnectDB()
  241. {
  242. dh = new DataHelper();
  243. SystemInf.dh = dh;
  244. ma_code.TableName = "make";
  245. ma_code.SelectField = "ma_code # 工单号";
  246. ma_code.FormName = Name;
  247. ma_code.DBTitle = "工单查询";
  248. ma_code.SetValueField = new string[] { "ma_code" };
  249. ma_code.Condition = "";
  250. ma_code.DbChange += nr_rule_DBChange;
  251. li_code.TableName = "line";
  252. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  253. li_code.FormName = Name;
  254. li_code.DBTitle = "线别查询";
  255. li_code.SetValueField = new string[] { "li_code" };
  256. li_code.Condition = "";
  257. li_code.DbChange += nr_rule_DBChange;
  258. }
  259. SerialPort serialPort1 = new SerialPort();
  260. private void StartWatch_Click(object sender, EventArgs e)
  261. {
  262. BaseUtil.SetCacheData("Type", Type.Text);
  263. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  264. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  265. BaseUtil.SetCacheData("Line", li_code.Text);
  266. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  267. {
  268. OperateResult.AppendText("工单号不能为空\n");
  269. return;
  270. }
  271. //if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  272. //{
  273. // OperateResult.AppendText("线别不能为空\n");
  274. // return;
  275. //}
  276. //string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  277. //if (ExitConfirm != "Yes")
  278. //{
  279. // return;
  280. //}
  281. //设置按钮不可点击
  282. StartWatch.Enabled = false;
  283. ma_code.Enabled = false;
  284. StopWatch.Enabled = true;
  285. try
  286. {
  287. serialPort1.PortName = ComPort.Text;
  288. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  289. serialPort1.Open();
  290. Timer.Start();
  291. }
  292. catch (Exception mes)
  293. {
  294. if (BaudRate.Text == "" || BaudRate.Text == "")
  295. OperateResult.AppendText(">>请先维护波特率和串口\n");
  296. else
  297. OperateResult.AppendText(">>" + mes.Message + "\n");
  298. }
  299. OperateResult.AppendText("开始执行监控\n");
  300. }
  301. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  302. {
  303. OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
  304. TxtHandleProcess(e.FullPath);
  305. }
  306. string nextLine;
  307. private void TxtHandleProcess(string FileName)
  308. {
  309. List<string> badcode = new List<string>();
  310. List<string> badlocation = new List<string>();
  311. List<string> badprod = new List<string>();
  312. }
  313. private void StopWatch_Click(object sender, EventArgs e)
  314. {
  315. XmlWatcher.EnableRaisingEvents = false;
  316. StartWatch.Enabled = true;
  317. ma_code.Enabled = true;
  318. StopWatch.Enabled = false;
  319. OperateResult.AppendText("停止执行监控\n");
  320. }
  321. private void Clean_Click(object sender, EventArgs e)
  322. {
  323. OperateResult.Clear();
  324. }
  325. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  326. {
  327. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  328. if (ExitConfirm != "Yes")
  329. {
  330. WindowState = FormWindowState.Minimized;
  331. e.Cancel = true;
  332. }
  333. }
  334. private void Timer_Tick(object sender, EventArgs e)
  335. {
  336. if (serialPort1.IsOpen)
  337. {
  338. if (!realwritedata)
  339. {
  340. byte[] data = HexStringToBytes("55 00 10 65");
  341. serialPort1.Write(data, 0, data.Length);
  342. }
  343. }
  344. }
  345. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  346. {
  347. SetAutoRun();
  348. }
  349. private void SetAutoRun()
  350. {
  351. if (AutoStart.Checked) //设置开机自启动
  352. {
  353. string path = Application.ExecutablePath;
  354. RegistryKey rk = Registry.LocalMachine;
  355. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  356. rk2.SetValue("FileWatcher.exe", path);
  357. rk2.Close();
  358. rk.Close();
  359. }
  360. else //取消开机自启动
  361. {
  362. string path = Application.ExecutablePath;
  363. RegistryKey rk = Registry.LocalMachine;
  364. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  365. rk2.DeleteValue("FileWatcher.exe", false);
  366. rk2.Close();
  367. rk.Close();
  368. }
  369. }
  370. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  371. {
  372. string a = hs.Replace(" ", "");
  373. int bytelength = 0;
  374. if (a.Length % 2 == 0)
  375. {
  376. bytelength = a.Length / 2;
  377. }
  378. else
  379. {
  380. bytelength = a.Length / 2 + 1;
  381. }
  382. byte[] b = new byte[bytelength];
  383. //逐个字符变为16进制字节数据
  384. for (int i = 0; i < bytelength; i++)
  385. {
  386. if (i == bytelength - 1)
  387. {
  388. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  389. }
  390. else
  391. {
  392. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  393. }
  394. }
  395. //按照指定编码将字节数组变为字符串
  396. return b;
  397. }
  398. private void GetData_Click(object sender, EventArgs e)
  399. {
  400. }
  401. }
  402. }