AutoMakeQTY.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. namespace FileWatcher
  14. {
  15. public partial class AutoMakeQTY : Form
  16. {
  17. DataHelper dh;
  18. DataTable dt;
  19. DataTable DB;
  20. /// <summary>
  21. /// 用户编号
  22. /// </summary>
  23. string iusercode;
  24. /// <summary>
  25. /// 岗位资源
  26. /// </summary>
  27. string isource;
  28. Thread InitDB;
  29. /// 当前工序
  30. /// </summary>
  31. string istepcode;
  32. StringBuilder sql = new StringBuilder();
  33. /// <summary>
  34. /// 缓存的文件
  35. /// </summary>
  36. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  37. /// <summary>
  38. /// 缓存的文件夹
  39. /// </summary>
  40. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  41. ftpOperater ftp = new ftpOperater();
  42. public AutoMakeQTY()
  43. {
  44. InitializeComponent();
  45. StartPosition = FormStartPosition.CenterScreen;
  46. }
  47. DataTable Dbfind;
  48. string IPAddress = "";
  49. private void nr_rule_DBChange(object sender, EventArgs e)
  50. {
  51. Dbfind = li_code.ReturnData;
  52. BaseUtil.SetFormValue(this.Controls, Dbfind);
  53. }
  54. private int Val(char Expression)
  55. {
  56. if (Expression >= '1' && Expression <= '9')
  57. {
  58. return unchecked((int)Expression) - 48;
  59. }
  60. return 0;
  61. }
  62. private void Form1_Load(object sender, EventArgs e)
  63. {
  64. byte[] b2 = new byte[4];
  65. float f2;
  66. string tmp_b2 = "43666AEC";
  67. tmp_b2 = tmp_b2.Replace(" ", "");//删除字符串中间的空格
  68. if (tmp_b2.Length != 8)
  69. {
  70. //判断输入字符串的长度是否合适
  71. MessageBox.Show("输入Byte长度错误,为连续8位4个字节,高位为0的补齐0");
  72. }
  73. else
  74. {
  75. //将收到的字符串分成4个字节,这4个字节是字符串的形式
  76. string tmp_char1 = tmp_b2.Substring(0, 2);
  77. string tmp_char2 = tmp_b2.Substring(2, 2);
  78. string tmp_char3 = tmp_b2.Substring(4, 2);
  79. string tmp_char4 = tmp_b2.Substring(6, 2);
  80. //将4字节的字符串格式转换成16进制的byte格式
  81. b2[3] = Convert.ToByte(tmp_char1, 16);
  82. b2[2] = Convert.ToByte(tmp_char2, 16);
  83. b2[1] = Convert.ToByte(tmp_char3, 16);
  84. b2[0] = Convert.ToByte(tmp_char4, 16);
  85. //将16进制byte转换成浮点数格式
  86. f2 = BitConverter.ToSingle(b2, 0);
  87. Console.WriteLine(Convert.ToString(f2)); ;
  88. }
  89. CheckForIllegalCrossThreadCalls = false;
  90. FormBorderStyle = FormBorderStyle.FixedSingle;
  91. serialPort1.DataReceived += SerialPort1_DataReceived;
  92. InitDB = new Thread(ConnectDB);
  93. //添加监控事件
  94. XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  95. //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  96. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  97. stw.StartPosition = FormStartPosition.CenterScreen;
  98. stw.ShowDialog();
  99. List<string> CacheInf = new List<string>();
  100. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  101. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  102. {
  103. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  104. IPAddress = IpEntry.AddressList[i].ToString();
  105. }
  106. //获取缓存信息
  107. try
  108. {
  109. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  110. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  111. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  112. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  113. }
  114. catch (Exception ex) { MessageBox.Show(ex.Message); }
  115. }
  116. double lastqty = 0;
  117. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  118. {
  119. int len = serialPort1.BytesToRead;
  120. Byte[] readBuffer = new Byte[len];
  121. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  122. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  123. //Convert.ToInt32(data.Substring(6, 8), 16);
  124. //Console.WriteLine(data.Substring(6, 2));
  125. //double Num1 = double.Parse(data.Substring(8, 2)) * 10000;
  126. //Console.WriteLine(data.Substring(8, 2));
  127. //double Num2 = double.Parse(data.Substring(10, 2)) * 1000;
  128. //Console.WriteLine(data.Substring(10, 2));
  129. //double Num3 = double.Parse(data.Substring(12, 2)) * 100;
  130. //Console.WriteLine(data.Substring(12, 2));
  131. //double Num4 = double.Parse(data.Substring(14, 2)) * 10;
  132. //Console.WriteLine(data.Substring(14, 2));
  133. //double Num5 = double.Parse(data.Substring(16, 2));
  134. if (data != "")
  135. {
  136. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  137. if (lastqty > qty)
  138. {
  139. lastqty = 0;
  140. }
  141. NowQTY.Text = qty.ToString();
  142. if (Type.Text == "投入")
  143. {
  144. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  145. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,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)" +
  147. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "')", "insert");
  148. }
  149. else
  150. {
  151. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  152. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  153. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode)" +
  154. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "')", "insert");
  155. }
  156. lastqty = qty;
  157. }
  158. }
  159. private void ConnectDB()
  160. {
  161. dh = new DataHelper();
  162. SystemInf.dh = dh;
  163. ma_code.TableName = "make";
  164. ma_code.SelectField = "ma_code # 工单号";
  165. ma_code.FormName = Name;
  166. ma_code.DBTitle = "工单查询";
  167. ma_code.SetValueField = new string[] { "ma_code" };
  168. ma_code.Condition = "";
  169. ma_code.DbChange += nr_rule_DBChange;
  170. li_code.TableName = "line";
  171. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  172. li_code.FormName = Name;
  173. li_code.DBTitle = "线别查询";
  174. li_code.SetValueField = new string[] { "li_code" };
  175. li_code.Condition = "";
  176. li_code.DbChange += nr_rule_DBChange;
  177. }
  178. SerialPort serialPort1 = new SerialPort();
  179. private void StartWatch_Click(object sender, EventArgs e)
  180. {
  181. BaseUtil.SetCacheData("Type", Type.Text);
  182. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  183. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  184. BaseUtil.SetCacheData("Line", li_code.Text);
  185. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  186. {
  187. OperateResult.AppendText("工单号不能为空\n");
  188. return;
  189. }
  190. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  191. {
  192. OperateResult.AppendText("线别不能为空\n");
  193. return;
  194. }
  195. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  196. if (ExitConfirm != "Yes")
  197. {
  198. return;
  199. }
  200. //设置按钮不可点击
  201. StartWatch.Enabled = false;
  202. ma_code.Enabled = false;
  203. StopWatch.Enabled = true;
  204. try
  205. {
  206. serialPort1.PortName = ComPort.Text;
  207. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  208. serialPort1.Open();
  209. Timer.Start();
  210. }
  211. catch (Exception mes)
  212. {
  213. if (BaudRate.Text == "" || BaudRate.Text == "")
  214. OperateResult.AppendText(">>请先维护波特率和串口\n");
  215. else
  216. OperateResult.AppendText(">>" + mes.Message + "\n");
  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. }
  315. }