AutoSMTRXY.cs 27 KB

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