AutoSMTRXY.cs 32 KB

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