AutoSMTRXY.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. using HslCommunication;
  12. using System.Diagnostics.PerformanceData;
  13. namespace FileWatcher
  14. {
  15. public partial class AutoSMTRXY : 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. HslCommunication.Profinet.Panasonic.PanasonicMewtocol plc;
  43. /// <summary>
  44. /// 参数设置(由“设置参数”窗口配置并写入缓存)
  45. /// </summary>
  46. string ComPort = "";
  47. string BaudRate = "9600";
  48. string Read = "";
  49. string Write = "";
  50. string Stop = "";
  51. string ToZero = "";
  52. string UseScan = "";
  53. string CountType = "";
  54. public AutoSMTRXY()
  55. {
  56. InitializeComponent();
  57. StartPosition = FormStartPosition.CenterScreen;
  58. }
  59. DataTable Dbfind;
  60. string IPAddress = "";
  61. private void nr_rule_DBChange(object sender, EventArgs e)
  62. {
  63. Dbfind = li_code.ReturnData;
  64. BaseUtil.SetFormValue(this.Controls, Dbfind);
  65. }
  66. private void Form1_Load(object sender, EventArgs e)
  67. {
  68. CheckForIllegalCrossThreadCalls = false;
  69. FormBorderStyle = FormBorderStyle.FixedSingle;
  70. InitDB = new Thread(ConnectDB);
  71. //添加监控事件
  72. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  73. stw.StartPosition = FormStartPosition.CenterScreen;
  74. stw.ShowDialog();
  75. List<string> CacheInf = new List<string>();
  76. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  77. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  78. {
  79. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  80. IPAddress = IpEntry.AddressList[i].ToString();
  81. }
  82. serialPort1.DataReceived += SerialPort1_DataReceived;
  83. //获取缓存信息
  84. try
  85. {
  86. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  87. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  88. ReloadSettings();
  89. }
  90. catch (Exception ex) { OperateResult.AppendText(ex.Message); }
  91. RemindTimer.Start();
  92. this.WindowState = FormWindowState.Normal;
  93. this.Size = new System.Drawing.Size(900, 400); // 替换为你需要的尺寸
  94. this.CenterToScreen();
  95. }
  96. double lastqty = 0;
  97. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  98. {
  99. int len;
  100. Byte[] readBuffer;
  101. try
  102. {
  103. len = serialPort1.BytesToRead;
  104. readBuffer = new Byte[len];
  105. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  106. }
  107. catch (Exception)
  108. {
  109. //停止监控关闭串口的瞬间,后台读取可能抛异常,忽略本次数据
  110. return;
  111. }
  112. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  113. string error = "";
  114. if (data != "")
  115. {
  116. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  117. if (lastqty > qty)
  118. {
  119. lastqty = 0;
  120. }
  121. NowQTY.Text = qty.ToString();
  122. if (Type.Text == "投入")
  123. {
  124. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  125. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  126. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount,mhc_nowqty)" +
  127. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "','" + qty + "')", "insert");
  128. }
  129. else
  130. {
  131. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  132. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  133. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount,mhc_nowqty)" +
  134. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "','" + qty + "')", "insert");
  135. }
  136. dt = (DataTable)dh.ExecuteSql("select nvl(sum(MHC_QTY* MHC_PCBCOUNT),0)MHC_QTY,max(nvl(ma_qty,0)+nvl(ma_boutqty,0))ma_qty from makehourcount left join make on ma_code=mhc_macode where mhc_macode='" + ma_code.Text + "' and mhc_qty<10", "select");
  137. if (dt.Rows.Count > 0)
  138. {
  139. int MHC_QTY = int.Parse(dt.Rows[0]["MHC_QTY"].ToString());
  140. int ma_qty = int.Parse(dt.Rows[0]["ma_qty"].ToString());
  141. if (MHC_QTY >= ma_qty)
  142. {
  143. if (Stop != "" && StopData)
  144. {
  145. OperateResult write = plc.Write(Stop, 1);
  146. if (write.IsSuccess)
  147. {
  148. OperateResult.AppendText("超出工单数量,发送指令" + Stop + "写入1\n");
  149. OperateResult.ScrollToCaret();
  150. StopData = false;
  151. ShowAlarm("超出工单数量");
  152. }
  153. else
  154. {
  155. OperateResult.AppendText("写入失败\n");
  156. OperateResult.ScrollToCaret();
  157. }
  158. }
  159. else
  160. {
  161. OperateResult.AppendText("写入失败,未维护参数\n");
  162. }
  163. }
  164. }
  165. if (Counter.Checked)
  166. {
  167. LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, "", ((qty - lastqty)).ToString(), li_code.Text, "OK", out error);
  168. }
  169. lastqty = qty;
  170. }
  171. }
  172. private void ConnectDB()
  173. {
  174. dh = new DataHelper();
  175. SystemInf.dh = dh;
  176. ma_code.TableName = "make";
  177. ma_code.SelectField = "ma_code # 工单号";
  178. ma_code.FormName = Name;
  179. ma_code.DBTitle = "工单查询";
  180. ma_code.SetValueField = new string[] { "ma_code" };
  181. ma_code.Condition = "";
  182. ma_code.DbChange += nr_rule_DBChange;
  183. li_code.TableName = "line";
  184. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  185. li_code.FormName = Name;
  186. li_code.DBTitle = "线别查询";
  187. li_code.SetValueField = new string[] { "li_code" };
  188. li_code.Condition = "li_wccode in ('SMT','AI')";
  189. li_code.DbChange += nr_rule_DBChange;
  190. }
  191. SerialPort serialPort1 = new SerialPort();
  192. string nextLine;
  193. private void Clean_Click(object sender, EventArgs e)
  194. {
  195. OperateResult.Clear();
  196. }
  197. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  198. {
  199. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  200. if (ExitConfirm != "Yes")
  201. {
  202. WindowState = FormWindowState.Minimized;
  203. e.Cancel = true;
  204. }
  205. else
  206. {
  207. //退出程序前释放串口,避免端口被占用
  208. Timer.Stop();
  209. ReleaseSerial();
  210. }
  211. }
  212. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  213. {
  214. SetAutoRun();
  215. }
  216. //音频提示站位,料号,剩余数量信息
  217. private void Remind()
  218. {
  219. if (RemindVoice.Checked)
  220. {
  221. sql.Clear();
  222. sql.Append("SELECT 总需求数,余数,站位,料号 from(select bar_batchqty,dsl_barcode,dsl_prodcode 料号, " +
  223. "dsl_location 站位,dsl_baseqty 用量,(select sum(case when dsl_getqty=0 then 1 else dsl_getqty end)" +
  224. " from devsmtlocation T where T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null) " +
  225. "上料数, (select max(to_char(dsl_indate ,'MM-DD hh24:mi:ss')) from devsmtlocation T where " +
  226. "T.dsl_location=devsmtlocation.dsl_location and dsl_usable>0)接料时间,dsl_madeqty 料卷产出数, allmadeqty " +
  227. " 站位产出数,DSL_VALIDTIME, dsl_remainqty,(select sum(dsl_remainqty)from devsmtlocation T where " +
  228. "T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null)余数,dsl_needqty 总需求数 " +
  229. " from devsmtlocation left join (select DSL_LINECODE alllincode,Dsl_Location allLocation, " +
  230. " sum(dsl_madeqty)allmadeqty from devsmtlocation group by DSL_LINECODE,Dsl_Location)A on " +
  231. " alllincode=devsmtlocation.dsl_linecode and allLocation=dsl_location left join barcode " +
  232. " on dsl_barcode = bar_code where dsl_status=0 and dsl_usable=1 and dsl_linecode='" + li_code.Text + "' and DSL_GETQTY*0.1>(select sum(dsl_remainqty)from devsmtlocation T where T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null))" +
  233. " ");
  234. System.Data.DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  235. for (int i = 0; i < dt.Rows.Count; i++)
  236. {
  237. string 站位 = dt.Rows[i]["站位"].ToString();
  238. string 余数 = dt.Rows[i]["余数"].ToString();
  239. string 料号 = dt.Rows[i]["料号"].ToString();
  240. BaseUtil.PlaySound("站位" + 站位 + "物料" + 料号 + "数量剩余" + 余数 + "请及时接料");
  241. }
  242. if (dt.Rows.Count > 0)
  243. {
  244. if (plc != null && Stop != "")
  245. {
  246. OperateResult write = plc.Write(Stop, 1);
  247. if (write.IsSuccess)
  248. {
  249. OperateResult.AppendText("缺料预警,发送指令" + Stop + "写入1\n");
  250. OperateResult.ScrollToCaret();
  251. StopData = false;
  252. if (!MaterialAlarmShowed)
  253. {
  254. MaterialAlarmShowed = true;
  255. ShowAlarm("缺料预警");
  256. }
  257. }
  258. else
  259. {
  260. OperateResult.AppendText("写入失败\n");
  261. OperateResult.ScrollToCaret();
  262. }
  263. }
  264. }
  265. else
  266. {
  267. //缺料记录已清空,复位预警提示
  268. MaterialAlarmShowed = false;
  269. }
  270. }
  271. }
  272. private void SetAutoRun()
  273. {
  274. if (AutoStart.Checked) //设置开机自启动
  275. {
  276. string path = Application.ExecutablePath;
  277. RegistryKey rk = Registry.LocalMachine;
  278. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  279. rk2.SetValue("FileWatcher.exe", path);
  280. rk2.Close();
  281. rk.Close();
  282. }
  283. else //取消开机自启动
  284. {
  285. string path = Application.ExecutablePath;
  286. RegistryKey rk = Registry.LocalMachine;
  287. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  288. rk2.DeleteValue("FileWatcher.exe", false);
  289. rk2.Close();
  290. rk.Close();
  291. }
  292. }
  293. private void ms_sncode_KeyDown(object sender, KeyEventArgs e)
  294. {
  295. if (e.KeyCode == Keys.Enter)
  296. {
  297. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  298. {
  299. OperateResult.AppendText("工单号不存在\n");
  300. OperateResult.ScrollToCaret();
  301. return;
  302. }
  303. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  304. {
  305. OperateResult.AppendText("线别不存在\n");
  306. OperateResult.ScrollToCaret();
  307. return;
  308. }
  309. string error = "";
  310. if (SN.Checked)
  311. {
  312. OperateResult write = plc.Write(Write, 1);
  313. if (write.IsSuccess)
  314. {
  315. OperateResult.AppendText("通过,发送指令" + Write + "写入1\n");
  316. OperateResult.ScrollToCaret();
  317. }
  318. else
  319. {
  320. OperateResult.AppendText("写入失败\n");
  321. OperateResult.ScrollToCaret();
  322. }
  323. System.Data.DataTable dt = (System.Data.DataTable)dh.ExecuteSql("select MSL_MAINSN from makesnlist where msl_sncode='" + ms_sncode.Text + "'", "select");
  324. if (dt.Rows.Count > 0)
  325. {
  326. string MainSN = dt.Rows[0]["MSL_MAINSN"].ToString();
  327. if (LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, MainSN, "0", li_code.Text, "OK", out error))
  328. {
  329. OperateResult.AppendText("SN:" + MainSN + "采集成功\n");
  330. OperateResult.ScrollToCaret();
  331. ms_sncode.Clear();
  332. }
  333. else
  334. {
  335. OperateResult.AppendText(error + "\n");
  336. OperateResult.ScrollToCaret();
  337. ms_sncode.Clear();
  338. }
  339. }
  340. ms_sncode.Clear();
  341. }
  342. }
  343. }
  344. private void RemindTimer_Tick(object sender, EventArgs e)
  345. {
  346. Remind();
  347. }
  348. private void StartWatch_Click(object sender, EventArgs e)
  349. {
  350. BaseUtil.SetCacheData("Line", li_code.Text);
  351. BaseUtil.SetCacheData("Type", Type.Text);
  352. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  353. {
  354. OperateResult.AppendText("工单号不能为空\n");
  355. return;
  356. }
  357. string DL_STATUSCODE = dh.getFieldDataByCondition("deviceline", "DL_STATUSCODE", "DL_LINECODE='" + li_code.Text + "' and DL_MACODE='" + ma_code.Text + "'").ToString();
  358. if (DL_STATUSCODE != "STARTED")
  359. {
  360. OperateResult.AppendText("机台尚未启动,请在APP上启动机台\n");
  361. return;
  362. }
  363. if (CountType == "")
  364. {
  365. OperateResult.AppendText("请选择计数类型\n");
  366. return;
  367. }
  368. if (ComPort == "")
  369. {
  370. OperateResult.AppendText("请选择串口\n");
  371. return;
  372. }
  373. if (Type.Text == "")
  374. {
  375. OperateResult.AppendText("请选择投入产出类型\n");
  376. return;
  377. }
  378. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  379. {
  380. OperateResult.AppendText("线别不能为空\n");
  381. return;
  382. }
  383. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  384. if (ExitConfirm != "Yes")
  385. {
  386. return;
  387. }
  388. dh.ExecuteSql("update product set pr_pcbacount='" + pr_pcbacount.Value + "' where pr_code=(select ma_prodcode from make where ma_code='" + ma_code.Text + "')", "update");
  389. //设置按钮不可点击
  390. StartWatch.Enabled = false;
  391. ma_code.Enabled = false;
  392. StopWatch.Enabled = true;
  393. try
  394. {
  395. //重新打开前先释放旧的串口资源(PLC管道/计数器串口),避免端口被占用
  396. ReleaseSerial();
  397. if (CountType == "PLC")
  398. {
  399. plc = new HslCommunication.Profinet.Panasonic.PanasonicMewtocol();
  400. plc.Station = 238;
  401. HslCommunication.Core.Pipe.PipeSerialPort pipe = new HslCommunication.Core.Pipe.PipeSerialPort();
  402. pipe.SerialPortInni(ComPort, 9600, 8, StopBits.One, Parity.Odd);
  403. pipe.RtsEnable = false;
  404. pipe.DtrEnable = false;
  405. pipe.SleepTime = 20;
  406. pipe.ReceiveTimeOut = 5000;
  407. plc.CommunicationPipe = pipe;
  408. if (Counter.Checked)
  409. {
  410. OperateResult write = plc.Write(UseScan, 0);
  411. if (write.IsSuccess)
  412. {
  413. OperateResult.AppendText("通过,发送指令" + UseScan + "写入0\n");
  414. }
  415. else
  416. {
  417. OperateResult.AppendText("写入失败\n");
  418. }
  419. write = plc.Write(Stop, 0);
  420. if (write.IsSuccess)
  421. {
  422. OperateResult.AppendText("通过,发送指令" + Stop + "写入0\n");
  423. }
  424. else
  425. {
  426. OperateResult.AppendText("写入失败\n");
  427. }
  428. }
  429. else
  430. {
  431. OperateResult write = plc.Write(UseScan, 2);
  432. if (write.IsSuccess)
  433. {
  434. OperateResult.AppendText("通过,发送指令" + UseScan + "写入2\n");
  435. }
  436. else
  437. {
  438. OperateResult.AppendText("写入失败\n");
  439. }
  440. }
  441. }
  442. else
  443. {
  444. serialPort1.PortName = ComPort;
  445. serialPort1.BaudRate = int.Parse(BaudRate);
  446. serialPort1.Open();
  447. }
  448. //开始新一轮监控,复位停机/预警标记,保证异常消除后能再次提示
  449. StopData = true;
  450. MaterialAlarmShowed = false;
  451. Timer.Start();
  452. }
  453. catch (Exception mes)
  454. {
  455. if (BaudRate == "" || ComPort == "")
  456. OperateResult.AppendText(">>请先维护波特率和串口\n");
  457. else
  458. OperateResult.AppendText(">>" + mes.Message + "\n");
  459. }
  460. OperateResult.AppendText("开始执行监控\n");
  461. }
  462. private void StopWatch_Click(object sender, EventArgs e)
  463. {
  464. StartWatch.Enabled = true;
  465. ma_code.Enabled = true;
  466. StopWatch.Enabled = false;
  467. //先停定时器再释放串口,避免停止后仍在访问
  468. Timer.Stop();
  469. ReleaseSerial();
  470. OperateResult.AppendText("停止执行监控\n");
  471. }
  472. /// <summary>
  473. /// 释放串口资源:计数器模式串口 + PLC通信管道内的串口,防止端口被占用
  474. /// </summary>
  475. private void ReleaseSerial()
  476. {
  477. try
  478. {
  479. if (serialPort1.IsOpen)
  480. {
  481. serialPort1.Close();
  482. OperateResult.AppendText("计数器串口已关闭\n");
  483. }
  484. }
  485. catch (Exception ex)
  486. {
  487. OperateResult.AppendText(">>关闭计数器串口失败:" + ex.Message + "\n");
  488. }
  489. try
  490. {
  491. if (plc != null)
  492. {
  493. plc.Close();
  494. OperateResult.AppendText("PLC通信已关闭\n");
  495. }
  496. }
  497. catch (Exception ex)
  498. {
  499. OperateResult.AppendText(">>关闭PLC通信失败:" + ex.Message + "\n");
  500. }
  501. }
  502. /// <summary>停机指令是否已下发(写入Stop后置false,点“启动”后复位,避免重复写、重复弹窗)</summary>
  503. bool StopData = true;
  504. /// <summary>缺料预警是否已提示过(缺料记录消失后自动复位)</summary>
  505. bool MaterialAlarmShowed = false;
  506. /// <summary>弹窗显示锁,保证同一时刻只存在一个提示框</summary>
  507. int AlarmBoxShowing = 0;
  508. /// <summary>
  509. /// 弹出提示框,同一时刻只弹一个:未点掉之前不会重复弹出(串口事件在后台线程,需防并发)
  510. /// </summary>
  511. private void ShowAlarm(string message)
  512. {
  513. if (System.Threading.Interlocked.CompareExchange(ref AlarmBoxShowing, 1, 0) != 0)
  514. {
  515. return;
  516. }
  517. try
  518. {
  519. MessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  520. }
  521. finally
  522. {
  523. System.Threading.Interlocked.Exchange(ref AlarmBoxShowing, 0);
  524. }
  525. }
  526. private void Timer_Tick(object sender, EventArgs e)
  527. {
  528. if (CountType == "PLC")
  529. {
  530. string error = "";
  531. OperateResult<short> read = plc.ReadInt16(Read);
  532. //OperateResult.AppendText("读取数据\n");
  533. if (read.IsSuccess)
  534. {
  535. if (read.Content != 0)
  536. {
  537. double qty = Convert.ToInt32(read.Content);
  538. if (lastqty > qty)
  539. {
  540. lastqty = 0;
  541. }
  542. //else
  543. //{
  544. // if (qty - lastqty > 0)
  545. // {
  546. // OperateResult write = plc.Write(Write, 1);
  547. // if (write.IsSuccess)
  548. // {
  549. // OperateResult.AppendText("通过,发送指令" + Write + "写入1\n");
  550. // }
  551. // else
  552. // {
  553. // OperateResult.AppendText("写入失败\n");
  554. // }
  555. // }
  556. //}
  557. NowQTY.Text = qty.ToString();
  558. if (Type.Text == "投入")
  559. {
  560. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  561. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  562. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount,mhc_nowqty)" +
  563. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "','" + qty + "')", "insert");
  564. }
  565. else
  566. {
  567. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  568. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  569. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount,mhc_nowqty)" +
  570. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "','" + qty + "')", "insert");
  571. }
  572. if (Counter.Checked)
  573. {
  574. LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, "", (qty - lastqty).ToString(), li_code.Text, "OK", out error);
  575. }
  576. lastqty = qty;
  577. dt = (DataTable)dh.ExecuteSql("select nvl(sum(MHC_QTY* MHC_PCBCOUNT),0)MHC_QTY,max(nvl(ma_qty,0)+nvl(ma_boutqty,0))ma_qty from makehourcount left join make on ma_code=mhc_macode where mhc_macode='" + ma_code.Text + "' and mhc_qty<10", "select");
  578. if (dt.Rows.Count > 0)
  579. {
  580. int MHC_QTY = int.Parse(dt.Rows[0]["MHC_QTY"].ToString());
  581. int ma_qty = int.Parse(dt.Rows[0]["ma_qty"].ToString());
  582. if (MHC_QTY >= ma_qty)
  583. {
  584. if (Stop != "" && StopData)
  585. {
  586. OperateResult write = plc.Write(Stop, 1);
  587. if (write.IsSuccess)
  588. {
  589. OperateResult.AppendText("超出工单数量,发送指令" + Stop + "写入1\n");
  590. OperateResult.ScrollToCaret();
  591. StopData = false;
  592. ShowAlarm("超出工单数量");
  593. }
  594. else
  595. {
  596. OperateResult.AppendText("写入失败\n");
  597. OperateResult.ScrollToCaret();
  598. }
  599. }
  600. else
  601. {
  602. OperateResult.AppendText("写入失败,未维护参数\n");
  603. }
  604. }
  605. }
  606. }
  607. }
  608. else
  609. {
  610. OperateResult.AppendText("Read [D60] failed: " + read.Message);
  611. }
  612. }
  613. else
  614. {
  615. }
  616. }
  617. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  618. {
  619. string a = hs.Replace(" ", "");
  620. int bytelength = 0;
  621. if (a.Length % 2 == 0)
  622. {
  623. bytelength = a.Length / 2;
  624. }
  625. else
  626. {
  627. bytelength = a.Length / 2 + 1;
  628. }
  629. byte[] b = new byte[bytelength];
  630. //逐个字符变为16进制字节数据
  631. for (int i = 0; i < bytelength; i++)
  632. {
  633. if (i == bytelength - 1)
  634. {
  635. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  636. }
  637. else
  638. {
  639. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  640. }
  641. }
  642. //按照指定编码将字节数组变为字符串
  643. return b;
  644. }
  645. private void CleanData_Click(object sender, EventArgs e)
  646. {
  647. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  648. if (ExitConfirm == "Yes")
  649. {
  650. NowQTY.Text = "0";
  651. OperateResult write = plc.Write(ToZero, 0);
  652. if (write.IsSuccess)
  653. {
  654. OperateResult.AppendText("通过,发送指令" + ToZero + "写入0\n");
  655. }
  656. else
  657. {
  658. OperateResult.AppendText("写入失败\n");
  659. }
  660. }
  661. }
  662. private void Setting_Click(object sender, EventArgs e)
  663. {
  664. //打开参数设置窗口,保存后刷新本窗体的参数
  665. AutoSMTRXYSetting settingForm = new AutoSMTRXYSetting();
  666. settingForm.StartPosition = FormStartPosition.CenterParent;
  667. if (settingForm.ShowDialog(this) == DialogResult.OK)
  668. {
  669. ReloadSettings();
  670. OperateResult.AppendText("参数设置已保存\n");
  671. OperateResult.ScrollToCaret();
  672. }
  673. }
  674. /// <summary>
  675. /// 从缓存重新加载参数设置
  676. /// </summary>
  677. private void ReloadSettings()
  678. {
  679. ComPort = GetCacheString("ComPort");
  680. BaudRate = GetCacheString("BaudRate");
  681. Read = GetCacheString("Read");
  682. Write = GetCacheString("Write");
  683. ToZero = GetCacheString("ToZero");
  684. CountType = GetCacheString("CountType");
  685. UseScan = GetCacheString("UseScan");
  686. Stop = GetCacheString("Stop");
  687. if (BaudRate == "")
  688. {
  689. BaudRate = "9600";
  690. }
  691. if (CountType == "")
  692. {
  693. CountType = "PLC";
  694. }
  695. }
  696. private string GetCacheString(string ParamName)
  697. {
  698. object value = BaseUtil.GetCacheData(ParamName);
  699. return value == null ? "" : value.ToString();
  700. }
  701. private void Counter_CheckedChanged(object sender, EventArgs e)
  702. {
  703. if (Counter.Checked)
  704. {
  705. string ExitConfirm = MessageBox.Show(this, "确认切换为计数器?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  706. if (ExitConfirm == "Yes")
  707. {
  708. OperateResult write = plc.Write(UseScan, 0);
  709. if (write.IsSuccess)
  710. {
  711. OperateResult.AppendText("通过,发送指令" + UseScan + "写入0\n");
  712. }
  713. else
  714. {
  715. OperateResult.AppendText("写入失败\n");
  716. }
  717. }
  718. else
  719. {
  720. SN.Checked = false;
  721. }
  722. }
  723. }
  724. private void SN_CheckedChanged(object sender, EventArgs e)
  725. {
  726. if (SN.Checked)
  727. {
  728. string ExitConfirm = MessageBox.Show(this, "确认切换为扫码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  729. if (ExitConfirm == "Yes")
  730. {
  731. if (plc == null)
  732. {
  733. OperateResult.AppendText("请先开始监控\n");
  734. return;
  735. }
  736. OperateResult write = plc.Write(UseScan, 2);
  737. if (write.IsSuccess)
  738. {
  739. OperateResult.AppendText("通过,发送指令" + UseScan + "写入2\n");
  740. }
  741. else
  742. {
  743. OperateResult.AppendText("写入失败\n");
  744. }
  745. }
  746. else
  747. {
  748. Counter.Checked = true;
  749. }
  750. }
  751. }
  752. private void Start_Click(object sender, EventArgs e)
  753. {
  754. if (Stop != "")
  755. {
  756. OperateResult write = plc.Write(Stop, 0);
  757. if (write.IsSuccess)
  758. {
  759. OperateResult.AppendText("通过,发送指令" + Stop + "写入0\n");
  760. StopData = true;
  761. }
  762. else
  763. {
  764. OperateResult.AppendText("写入失败\n");
  765. }
  766. }
  767. else
  768. {
  769. OperateResult.AppendText("写入失败,未维护参数\n");
  770. }
  771. }
  772. private void pr_pcbacount_ValueChanged(object sender, EventArgs e)
  773. {
  774. dh.ExecuteSql("update product set pr_pcbacount='" + pr_pcbacount.Value + "' where pr_code=(select ma_prodcode from make where ma_code='" + ma_code.Text + "')", "update");
  775. }
  776. }
  777. }