AutoSMTRXY.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. public AutoSMTRXY()
  44. {
  45. InitializeComponent();
  46. StartPosition = FormStartPosition.CenterScreen;
  47. }
  48. DataTable Dbfind;
  49. string IPAddress = "";
  50. private void nr_rule_DBChange(object sender, EventArgs e)
  51. {
  52. Dbfind = li_code.ReturnData;
  53. BaseUtil.SetFormValue(this.Controls, Dbfind);
  54. }
  55. private void Form1_Load(object sender, EventArgs e)
  56. {
  57. CountType.Text = "PLC";
  58. CheckForIllegalCrossThreadCalls = false;
  59. FormBorderStyle = FormBorderStyle.FixedSingle;
  60. InitDB = new Thread(ConnectDB);
  61. //添加监控事件
  62. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  63. stw.StartPosition = FormStartPosition.CenterScreen;
  64. stw.ShowDialog();
  65. List<string> CacheInf = new List<string>();
  66. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  67. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  68. {
  69. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  70. IPAddress = IpEntry.AddressList[i].ToString();
  71. }
  72. serialPort1.DataReceived += SerialPort1_DataReceived;
  73. //获取缓存信息
  74. try
  75. {
  76. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  77. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  78. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  79. Read.Text = BaseUtil.GetCacheData("Read").ToString();
  80. Write.Text = BaseUtil.GetCacheData("Write").ToString();
  81. ToZero.Text = BaseUtil.GetCacheData("ToZero").ToString();
  82. CountType.Text = BaseUtil.GetCacheData("CountType").ToString();
  83. UseScan.Text = BaseUtil.GetCacheData("UseScan").ToString();
  84. }
  85. catch (Exception ex) { OperateResult.AppendText(ex.Message); }
  86. RemindTimer.Start();
  87. this.WindowState = FormWindowState.Normal;
  88. this.Size = new System.Drawing.Size(900, 400); // 替换为你需要的尺寸
  89. this.CenterToScreen();
  90. }
  91. double lastqty = 0;
  92. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  93. {
  94. int len = serialPort1.BytesToRead;
  95. Byte[] readBuffer = new Byte[len];
  96. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  97. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  98. string error = "";
  99. if (data != "")
  100. {
  101. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  102. if (lastqty > qty)
  103. {
  104. lastqty = 0;
  105. }
  106. NowQTY.Text = qty.ToString();
  107. if (Type.Text == "投入")
  108. {
  109. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  110. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  111. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  112. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  113. }
  114. else
  115. {
  116. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  117. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  118. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  119. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  120. }
  121. dt = (DataTable)dh.ExecuteSql("select nvl(sum(MHC_QTY* MHC_PCBCOUNT),0)MHC_QTY,max(ma_qty)ma_qty from makehourcount left join make on ma_code=mhc_macode where mhc_macode='" + ma_code.Text + "'", "select");
  122. if (dt.Rows.Count > 0)
  123. {
  124. int MHC_QTY = int.Parse(dt.Rows[0]["MHC_QTY"].ToString());
  125. int ma_qty = int.Parse(dt.Rows[0]["ma_qty"].ToString());
  126. if (MHC_QTY >= ma_qty)
  127. {
  128. OperateResult write = plc.Write(Stop.Text, 1);
  129. if (write.IsSuccess)
  130. {
  131. OperateResult.AppendText("通过,发送指令" + Stop.Text + "写入1\n");
  132. OperateResult.ScrollToCaret();
  133. }
  134. else
  135. {
  136. OperateResult.AppendText("写入失败\n");
  137. OperateResult.ScrollToCaret();
  138. }
  139. }
  140. }
  141. if (Counter.Checked)
  142. {
  143. LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, "", qty.ToString(), li_code.Text, "OK", out error);
  144. }
  145. lastqty = qty;
  146. }
  147. }
  148. private void ConnectDB()
  149. {
  150. dh = new DataHelper();
  151. SystemInf.dh = dh;
  152. ma_code.TableName = "make";
  153. ma_code.SelectField = "ma_code # 工单号";
  154. ma_code.FormName = Name;
  155. ma_code.DBTitle = "工单查询";
  156. ma_code.SetValueField = new string[] { "ma_code" };
  157. ma_code.Condition = "";
  158. ma_code.DbChange += nr_rule_DBChange;
  159. li_code.TableName = "line";
  160. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  161. li_code.FormName = Name;
  162. li_code.DBTitle = "线别查询";
  163. li_code.SetValueField = new string[] { "li_code" };
  164. li_code.Condition = "li_wccode in ('SMT','AI')";
  165. li_code.DbChange += nr_rule_DBChange;
  166. }
  167. SerialPort serialPort1 = new SerialPort();
  168. string nextLine;
  169. private void Clean_Click(object sender, EventArgs e)
  170. {
  171. OperateResult.Clear();
  172. }
  173. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  174. {
  175. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  176. if (ExitConfirm != "Yes")
  177. {
  178. WindowState = FormWindowState.Minimized;
  179. e.Cancel = true;
  180. }
  181. }
  182. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  183. {
  184. SetAutoRun();
  185. }
  186. //音频提示站位,料号,剩余数量信息
  187. private void Remind()
  188. {
  189. if (RemindVoice.Checked)
  190. {
  191. sql.Clear();
  192. sql.Append("SELECT 总需求数,余数,站位,料号 from(select bar_batchqty,dsl_barcode,dsl_prodcode 料号, " +
  193. "dsl_location 站位,dsl_baseqty 用量,(select sum(case when dsl_getqty=0 then 1 else dsl_getqty end)" +
  194. " from devsmtlocation T where T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null) " +
  195. "上料数, (select max(to_char(dsl_indate ,'MM-DD hh24:mi:ss')) from devsmtlocation T where " +
  196. "T.dsl_location=devsmtlocation.dsl_location and dsl_usable>0)接料时间,dsl_madeqty 料卷产出数, allmadeqty " +
  197. " 站位产出数,DSL_VALIDTIME, dsl_remainqty,(select sum(dsl_remainqty)from devsmtlocation T where " +
  198. "T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null)余数,dsl_needqty 总需求数 " +
  199. " from devsmtlocation left join (select DSL_LINECODE alllincode,Dsl_Location allLocation, " +
  200. " sum(dsl_madeqty)allmadeqty from devsmtlocation group by DSL_LINECODE,Dsl_Location)A on " +
  201. " alllincode=devsmtlocation.dsl_linecode and allLocation=dsl_location left join barcode " +
  202. " on dsl_barcode = bar_code where dsl_status=0 and dsl_usable=1) ");
  203. System.Data.DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  204. for (int i = 0; i < dt.Rows.Count; i++)
  205. {
  206. string 站位 = dt.Rows[i]["站位"].ToString();
  207. string 余数 = dt.Rows[i]["余数"].ToString();
  208. string 料号 = dt.Rows[i]["料号"].ToString();
  209. BaseUtil.PlaySound("站位" + 站位 + "物料" + 料号 + "数量剩余" + 余数 + "请及时接料");
  210. }
  211. if (dt.Rows.Count > 0)
  212. {
  213. OperateResult write = plc.Write(Stop.Text, 1);
  214. if (write.IsSuccess)
  215. {
  216. OperateResult.AppendText("通过,发送指令" + Stop.Text + "写入1\n");
  217. OperateResult.ScrollToCaret();
  218. }
  219. else
  220. {
  221. OperateResult.AppendText("写入失败\n");
  222. OperateResult.ScrollToCaret();
  223. }
  224. }
  225. }
  226. }
  227. private void SetAutoRun()
  228. {
  229. if (AutoStart.Checked) //设置开机自启动
  230. {
  231. string path = Application.ExecutablePath;
  232. RegistryKey rk = Registry.LocalMachine;
  233. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  234. rk2.SetValue("FileWatcher.exe", path);
  235. rk2.Close();
  236. rk.Close();
  237. }
  238. else //取消开机自启动
  239. {
  240. string path = Application.ExecutablePath;
  241. RegistryKey rk = Registry.LocalMachine;
  242. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  243. rk2.DeleteValue("FileWatcher.exe", false);
  244. rk2.Close();
  245. rk.Close();
  246. }
  247. }
  248. private void ms_sncode_KeyDown(object sender, KeyEventArgs e)
  249. {
  250. if (e.KeyCode == Keys.Enter)
  251. {
  252. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  253. {
  254. OperateResult.AppendText("工单号不存在\n");
  255. OperateResult.ScrollToCaret();
  256. return;
  257. }
  258. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  259. {
  260. OperateResult.AppendText("线别不存在\n");
  261. OperateResult.ScrollToCaret();
  262. return;
  263. }
  264. string error = "";
  265. if (SN.Checked)
  266. {
  267. OperateResult write = plc.Write(Write.Text, 1);
  268. if (write.IsSuccess)
  269. {
  270. OperateResult.AppendText("通过,发送指令" + Write.Text + "写入1\n");
  271. OperateResult.ScrollToCaret();
  272. }
  273. else
  274. {
  275. OperateResult.AppendText("写入失败\n");
  276. OperateResult.ScrollToCaret();
  277. }
  278. System.Data.DataTable dt = (System.Data.DataTable)dh.ExecuteSql("select MSL_MAINSN from makesnlist where msl_sncode='" + ms_sncode.Text + "'", "select");
  279. if (dt.Rows.Count > 0)
  280. {
  281. string MainSN = dt.Rows[0]["MSL_MAINSN"].ToString();
  282. if (LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, MainSN, "0", li_code.Text, "OK", out error))
  283. {
  284. OperateResult.AppendText("SN:" + MainSN + "采集成功\n");
  285. OperateResult.ScrollToCaret();
  286. ms_sncode.Clear();
  287. }
  288. else
  289. {
  290. OperateResult.AppendText(error + "\n");
  291. OperateResult.ScrollToCaret();
  292. ms_sncode.Clear();
  293. }
  294. }
  295. ms_sncode.Clear();
  296. }
  297. }
  298. }
  299. private void RemindTimer_Tick(object sender, EventArgs e)
  300. {
  301. Remind();
  302. }
  303. private void StartWatch_Click(object sender, EventArgs e)
  304. {
  305. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  306. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  307. BaseUtil.SetCacheData("Line", li_code.Text);
  308. BaseUtil.SetCacheData("Read", Read.Text);
  309. BaseUtil.SetCacheData("Write", Write.Text);
  310. BaseUtil.SetCacheData("ToZero", ToZero.Text);
  311. BaseUtil.SetCacheData("Type", Type.Text);
  312. BaseUtil.SetCacheData("CountType", CountType.Text);
  313. BaseUtil.SetCacheData("UseScan", UseScan.Text);
  314. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  315. {
  316. OperateResult.AppendText("工单号不能为空\n");
  317. return;
  318. }
  319. if (CountType.Text == "")
  320. {
  321. OperateResult.AppendText("请选择计数类型\n");
  322. return;
  323. }
  324. if (ComPort.Text == "")
  325. {
  326. OperateResult.AppendText("请选择串口\n");
  327. return;
  328. }
  329. if (Type.Text == "")
  330. {
  331. OperateResult.AppendText("请选择投入产出类型\n");
  332. return;
  333. }
  334. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  335. {
  336. OperateResult.AppendText("线别不能为空\n");
  337. return;
  338. }
  339. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  340. if (ExitConfirm != "Yes")
  341. {
  342. return;
  343. }
  344. //设置按钮不可点击
  345. StartWatch.Enabled = false;
  346. ma_code.Enabled = false;
  347. StopWatch.Enabled = true;
  348. try
  349. {
  350. if (CountType.Text == "PLC")
  351. {
  352. plc = new HslCommunication.Profinet.Panasonic.PanasonicMewtocol();
  353. plc.Station = 238;
  354. HslCommunication.Core.Pipe.PipeSerialPort pipe = new HslCommunication.Core.Pipe.PipeSerialPort();
  355. pipe.SerialPortInni(ComPort.Text, 9600, 8, StopBits.One, Parity.Odd);
  356. pipe.RtsEnable = false;
  357. pipe.DtrEnable = false;
  358. pipe.SleepTime = 20;
  359. pipe.ReceiveTimeOut = 5000;
  360. plc.CommunicationPipe = pipe;
  361. if (Counter.Checked)
  362. {
  363. OperateResult write = plc.Write(UseScan.Text, 0);
  364. if (write.IsSuccess)
  365. {
  366. OperateResult.AppendText("通过,发送指令" + UseScan.Text + "写入0\n");
  367. }
  368. else
  369. {
  370. OperateResult.AppendText("写入失败\n");
  371. }
  372. }
  373. else
  374. {
  375. OperateResult write = plc.Write(UseScan.Text, 2);
  376. if (write.IsSuccess)
  377. {
  378. OperateResult.AppendText("通过,发送指令" + UseScan.Text + "写入2\n");
  379. }
  380. else
  381. {
  382. OperateResult.AppendText("写入失败\n");
  383. }
  384. }
  385. }
  386. else
  387. {
  388. serialPort1.PortName = ComPort.Text;
  389. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  390. serialPort1.Open();
  391. }
  392. Timer.Start();
  393. }
  394. catch (Exception mes)
  395. {
  396. if (BaudRate.Text == "" || BaudRate.Text == "")
  397. OperateResult.AppendText(">>请先维护波特率和串口\n");
  398. else
  399. OperateResult.AppendText(">>" + mes.Message + "\n");
  400. }
  401. OperateResult.AppendText("开始执行监控\n");
  402. }
  403. private void StopWatch_Click(object sender, EventArgs e)
  404. {
  405. StartWatch.Enabled = true;
  406. ma_code.Enabled = true;
  407. StopWatch.Enabled = false;
  408. serialPort1.Close();
  409. OperateResult.AppendText("停止执行监控\n");
  410. }
  411. bool realwritedata = false;
  412. private void Timer_Tick(object sender, EventArgs e)
  413. {
  414. if (CountType.Text == "PLC")
  415. {
  416. string error = "";
  417. OperateResult<short> read = plc.ReadInt16(Read.Text);
  418. //OperateResult.AppendText("读取数据\n");
  419. if (read.IsSuccess)
  420. {
  421. if (read.Content != 0)
  422. {
  423. double qty = Convert.ToInt32(read.Content);
  424. if (lastqty > qty)
  425. {
  426. lastqty = 0;
  427. }
  428. //else
  429. //{
  430. // if (qty - lastqty > 0)
  431. // {
  432. // OperateResult write = plc.Write(Write.Text, 1);
  433. // if (write.IsSuccess)
  434. // {
  435. // OperateResult.AppendText("通过,发送指令" + Write.Text + "写入1\n");
  436. // }
  437. // else
  438. // {
  439. // OperateResult.AppendText("写入失败\n");
  440. // }
  441. // }
  442. //}
  443. NowQTY.Text = qty.ToString();
  444. if (Type.Text == "投入")
  445. {
  446. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  447. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  448. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  449. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  450. }
  451. else
  452. {
  453. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  454. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,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. if (Counter.Checked)
  459. {
  460. LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, "", qty.ToString(), li_code.Text, "OK", out error);
  461. }
  462. lastqty = qty;
  463. }
  464. }
  465. else
  466. {
  467. OperateResult.AppendText("Read [D60] failed: " + read.Message);
  468. }
  469. }
  470. else
  471. {
  472. }
  473. }
  474. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  475. {
  476. string a = hs.Replace(" ", "");
  477. int bytelength = 0;
  478. if (a.Length % 2 == 0)
  479. {
  480. bytelength = a.Length / 2;
  481. }
  482. else
  483. {
  484. bytelength = a.Length / 2 + 1;
  485. }
  486. byte[] b = new byte[bytelength];
  487. //逐个字符变为16进制字节数据
  488. for (int i = 0; i < bytelength; i++)
  489. {
  490. if (i == bytelength - 1)
  491. {
  492. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  493. }
  494. else
  495. {
  496. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  497. }
  498. }
  499. //按照指定编码将字节数组变为字符串
  500. return b;
  501. }
  502. private void CleanData_Click(object sender, EventArgs e)
  503. {
  504. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  505. if (ExitConfirm == "Yes")
  506. {
  507. NowQTY.Text = "0";
  508. OperateResult write = plc.Write(ToZero.Text, 0);
  509. if (write.IsSuccess)
  510. {
  511. OperateResult.AppendText("通过,发送指令" + ToZero.Text + "写入0\n");
  512. }
  513. else
  514. {
  515. OperateResult.AppendText("写入失败\n");
  516. }
  517. }
  518. }
  519. private void Setting_Click(object sender, EventArgs e)
  520. {
  521. if (Read.Enabled == true)
  522. {
  523. ComPort.Enabled = false; Read.Enabled = false; Write.Enabled = false;
  524. BaudRate.Enabled = false; ToZero.Enabled = false; UseScan.Enabled = false;
  525. CountType.Enabled = false; Read.Enabled = false;
  526. }
  527. else
  528. {
  529. if (Password.Text == "1234")
  530. {
  531. ComPort.Enabled = true; Read.Enabled = true; Write.Enabled = true;
  532. BaudRate.Visible = true; ToZero.Enabled = true; UseScan.Enabled = true;
  533. CountType.Enabled = true; Read.Enabled = true;
  534. Password.Text = "";
  535. }
  536. else
  537. {
  538. OperateResult.AppendText("密码不对\n");
  539. }
  540. }
  541. }
  542. private void Counter_CheckedChanged(object sender, EventArgs e)
  543. {
  544. if (Counter.Checked)
  545. {
  546. string ExitConfirm = MessageBox.Show(this, "确认切换为计数器?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  547. if (ExitConfirm == "Yes")
  548. {
  549. OperateResult write = plc.Write(UseScan.Text, 0);
  550. if (write.IsSuccess)
  551. {
  552. OperateResult.AppendText("通过,发送指令" + UseScan.Text + "写入0\n");
  553. }
  554. else
  555. {
  556. OperateResult.AppendText("写入失败\n");
  557. }
  558. }
  559. else
  560. {
  561. SN.Checked = false;
  562. }
  563. }
  564. }
  565. private void SN_CheckedChanged(object sender, EventArgs e)
  566. {
  567. if (SN.Checked)
  568. {
  569. string ExitConfirm = MessageBox.Show(this, "确认切换为扫码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  570. if (ExitConfirm == "Yes")
  571. {
  572. if (plc == null)
  573. {
  574. OperateResult.AppendText("请先开始监控\n");
  575. return;
  576. }
  577. OperateResult write = plc.Write(UseScan.Text, 2);
  578. if (write.IsSuccess)
  579. {
  580. OperateResult.AppendText("通过,发送指令" + UseScan.Text + "写入2\n");
  581. }
  582. else
  583. {
  584. OperateResult.AppendText("写入失败\n");
  585. }
  586. }
  587. else
  588. {
  589. Counter.Checked = true;
  590. }
  591. }
  592. }
  593. }
  594. }