AutoSMTRXY.cs 32 KB

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