AutoSMTRXY.cs 30 KB

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