AutoSMTRXY.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using System.Text;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.IO.Ports;
  11. namespace FileWatcher
  12. {
  13. public partial class AutoSMTRXY : Form
  14. {
  15. DataHelper dh;
  16. DataTable dt;
  17. DataTable DB;
  18. /// <summary>
  19. /// 用户编号
  20. /// </summary>
  21. string iusercode;
  22. /// <summary>
  23. /// 岗位资源
  24. /// </summary>
  25. string isource;
  26. Thread InitDB;
  27. /// 当前工序
  28. /// </summary>
  29. string istepcode;
  30. StringBuilder sql = new StringBuilder();
  31. /// <summary>
  32. /// 缓存的文件
  33. /// </summary>
  34. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  35. /// <summary>
  36. /// 缓存的文件夹
  37. /// </summary>
  38. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  39. ftpOperater ftp = new ftpOperater();
  40. public AutoSMTRXY()
  41. {
  42. InitializeComponent();
  43. StartPosition = FormStartPosition.CenterScreen;
  44. }
  45. DataTable Dbfind;
  46. string IPAddress = "";
  47. private void nr_rule_DBChange(object sender, EventArgs e)
  48. {
  49. Dbfind = li_code.ReturnData;
  50. BaseUtil.SetFormValue(this.Controls, Dbfind);
  51. }
  52. private void Form1_Load(object sender, EventArgs e)
  53. {
  54. CheckForIllegalCrossThreadCalls = false;
  55. FormBorderStyle = FormBorderStyle.FixedSingle;
  56. InitDB = new Thread(ConnectDB);
  57. //添加监控事件
  58. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  59. stw.StartPosition = FormStartPosition.CenterScreen;
  60. stw.ShowDialog();
  61. List<string> CacheInf = new List<string>();
  62. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  63. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  64. {
  65. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  66. IPAddress = IpEntry.AddressList[i].ToString();
  67. }
  68. serialPort1.DataReceived += SerialPort1_DataReceived;
  69. //获取缓存信息
  70. try
  71. {
  72. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  73. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  74. }
  75. catch (Exception ex) { MessageBox.Show(ex.Message); }
  76. RemindTimer.Start();
  77. }
  78. double lastqty = 0;
  79. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  80. {
  81. int len = serialPort1.BytesToRead;
  82. Byte[] readBuffer = new Byte[len];
  83. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  84. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  85. string error = "";
  86. if (data != "")
  87. {
  88. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  89. if (lastqty > qty)
  90. {
  91. lastqty = 0;
  92. }
  93. NowQTY.Text = qty.ToString();
  94. if (Type.Text == "投入")
  95. {
  96. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  97. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  98. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  99. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  100. }
  101. else
  102. {
  103. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  104. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  105. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  106. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  107. }
  108. LogicHandler.CS_SetResult(ma_code.Text, li_code.Text, "", qty.ToString(), li_code.Text, "OK", out error);
  109. lastqty = qty;
  110. }
  111. }
  112. private void ConnectDB()
  113. {
  114. dh = new DataHelper();
  115. SystemInf.dh = dh;
  116. ma_code.TableName = "make";
  117. ma_code.SelectField = "ma_code # 工单号";
  118. ma_code.FormName = Name;
  119. ma_code.DBTitle = "工单查询";
  120. ma_code.SetValueField = new string[] { "ma_code" };
  121. ma_code.Condition = "";
  122. ma_code.DbChange += nr_rule_DBChange;
  123. li_code.TableName = "line";
  124. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  125. li_code.FormName = Name;
  126. li_code.DBTitle = "线别查询";
  127. li_code.SetValueField = new string[] { "li_code" };
  128. li_code.Condition = "";
  129. li_code.DbChange += nr_rule_DBChange;
  130. }
  131. SerialPort serialPort1 = new SerialPort();
  132. string nextLine;
  133. private void Clean_Click(object sender, EventArgs e)
  134. {
  135. OperateResult.Clear();
  136. }
  137. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  138. {
  139. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  140. if (ExitConfirm != "Yes")
  141. {
  142. WindowState = FormWindowState.Minimized;
  143. e.Cancel = true;
  144. }
  145. }
  146. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  147. {
  148. SetAutoRun();
  149. }
  150. //音频提示站位,料号,剩余数量信息
  151. private void Remind()
  152. {
  153. if (RemindVoice.Checked)
  154. {
  155. sql.Clear();
  156. sql.Append("SELECT 总需求数,余数,站位,料号 from(select bar_batchqty,dsl_barcode,dsl_prodcode 料号, " +
  157. "dsl_location 站位,dsl_baseqty 用量,(select sum(case when dsl_getqty=0 then 1 else dsl_getqty end)" +
  158. " from devsmtlocation T where T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null) " +
  159. "上料数, (select max(to_char(dsl_indate ,'MM-DD hh24:mi:ss')) from devsmtlocation T where " +
  160. "T.dsl_location=devsmtlocation.dsl_location and dsl_usable>0)接料时间,dsl_madeqty 料卷产出数, allmadeqty " +
  161. " 站位产出数,DSL_VALIDTIME, dsl_remainqty,(select sum(dsl_remainqty)from devsmtlocation T where " +
  162. "T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null)余数,dsl_needqty 总需求数 " +
  163. " from devsmtlocation left join (select DSL_LINECODE alllincode,Dsl_Location allLocation, " +
  164. " sum(dsl_madeqty)allmadeqty from devsmtlocation group by DSL_LINECODE,Dsl_Location)A on " +
  165. " alllincode=devsmtlocation.dsl_linecode and allLocation=dsl_location left join barcode " +
  166. " on dsl_barcode = bar_code where dsl_status=0 and dsl_usable=1) ");
  167. DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  168. for (int i = 0; i < dt.Rows.Count; i++)
  169. {
  170. string 站位 = dt.Rows[i]["站位"].ToString();
  171. string 余数 = dt.Rows[i]["余数"].ToString();
  172. string 料号 = dt.Rows[i]["料号"].ToString();
  173. BaseUtil.PlaySound("站位" + 站位 + "物料" + 料号 + "数量剩余" + 余数 + "请及时接料");
  174. }
  175. }
  176. }
  177. private void SetAutoRun()
  178. {
  179. if (AutoStart.Checked) //设置开机自启动
  180. {
  181. string path = Application.ExecutablePath;
  182. RegistryKey rk = Registry.LocalMachine;
  183. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  184. rk2.SetValue("FileWatcher.exe", path);
  185. rk2.Close();
  186. rk.Close();
  187. }
  188. else //取消开机自启动
  189. {
  190. string path = Application.ExecutablePath;
  191. RegistryKey rk = Registry.LocalMachine;
  192. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  193. rk2.DeleteValue("FileWatcher.exe", false);
  194. rk2.Close();
  195. rk.Close();
  196. }
  197. }
  198. private void ms_sncode_KeyDown(object sender, KeyEventArgs e)
  199. {
  200. if (e.KeyCode == Keys.Enter)
  201. {
  202. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  203. {
  204. OperateResult.AppendText("工单号不存在\n");
  205. return;
  206. }
  207. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  208. {
  209. OperateResult.AppendText("线别不存在\n");
  210. return;
  211. }
  212. string error = "";
  213. if (SN.Checked)
  214. {
  215. System.Data.DataTable dt = (System.Data.DataTable)dh.ExecuteSql("select MSL_MAINSN from makesnlist where msl_sncode='" + ms_sncode.Text + "'", "select");
  216. if (dt.Rows.Count > 0)
  217. {
  218. string MainSN = dt.Rows[0]["MSL_MAINSN"].ToString();
  219. if (LogicHandler.CS_SetResult(ma_code.Text, li_code.Text, MainSN, "0", li_code.Text, "OK", out error))
  220. {
  221. OperateResult.AppendText("SN:" + MainSN + "采集成功\n");
  222. }
  223. else
  224. {
  225. OperateResult.AppendText(error + "\n");
  226. }
  227. }
  228. }
  229. }
  230. }
  231. private void RemindTimer_Tick(object sender, EventArgs e)
  232. {
  233. Remind();
  234. }
  235. private void StartWatch_Click(object sender, EventArgs e)
  236. {
  237. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  238. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  239. BaseUtil.SetCacheData("Line", li_code.Text);
  240. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  241. {
  242. OperateResult.AppendText("工单号不能为空\n");
  243. return;
  244. }
  245. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  246. {
  247. OperateResult.AppendText("线别不能为空\n");
  248. return;
  249. }
  250. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  251. if (ExitConfirm != "Yes")
  252. {
  253. return;
  254. }
  255. //设置按钮不可点击
  256. StartWatch.Enabled = false;
  257. ma_code.Enabled = false;
  258. StopWatch.Enabled = true;
  259. try
  260. {
  261. serialPort1.PortName = ComPort.Text;
  262. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  263. serialPort1.Open();
  264. Timer.Start();
  265. }
  266. catch (Exception mes)
  267. {
  268. if (BaudRate.Text == "" || BaudRate.Text == "")
  269. OperateResult.AppendText(">>请先维护波特率和串口\n");
  270. else
  271. OperateResult.AppendText(">>" + mes.Message + "\n");
  272. }
  273. OperateResult.AppendText("开始执行监控\n");
  274. }
  275. private void StopWatch_Click(object sender, EventArgs e)
  276. {
  277. StartWatch.Enabled = true;
  278. ma_code.Enabled = true;
  279. StopWatch.Enabled = false;
  280. OperateResult.AppendText("停止执行监控\n");
  281. }
  282. }
  283. }