BandBarodeDialog_S1.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using static MainForm.SQLHelper;
  11. using ZedGraph;
  12. using EIP_Protocol;
  13. using System.Numerics;
  14. using SqlSugar;
  15. namespace MainForm.FaForm
  16. {
  17. public partial class BandBarodeDialog_S1 : Form
  18. {
  19. public static bool isNoClose { get; set; }=false;
  20. //private string _strProductBarcode = "";
  21. //private string _CarrierBarcode = "";
  22. int plcNo = 1;
  23. string stationCode = "[OP10]";
  24. string stationName = "壳体清洁上料";
  25. string tagBaseName = "g_OP10_MES"; //标签变量名称
  26. string tagMesCommName = "mesCommToPC"; //标签变量名称
  27. string tagBarsetName = "BarcodeSet";
  28. OP10_MesData_t stPLC_MesData; //PLC的MES数据
  29. private Timer _inputTimer;
  30. Form_Home home=new Form_Home();
  31. public BandBarodeDialog_S1()
  32. {
  33. InitializeComponent();
  34. //置顶
  35. this.TopMost = true;
  36. // 设置窗口相对于父窗口居中
  37. this.StartPosition = FormStartPosition.CenterParent;
  38. this.Shown += MainForm_Shown;
  39. }
  40. private void BandBarodeDialog_S1_Load(object sender, EventArgs e)
  41. {
  42. // 初始化定时器
  43. _inputTimer = new Timer();
  44. _inputTimer.Interval = 500; // 500毫秒
  45. _inputTimer.Tick += InputTimer_Tick;
  46. // 绑定 TextChanged 事件
  47. //GetBarcode.TextChanged += GetBarcode_TextChanged;
  48. //Form_Home.StopWhile = true;
  49. ProductBarcode.Text = "";
  50. CarrierBarcode.Text = "";
  51. Form_Home._strCarrierBarcode = "";
  52. Form_Home._strProductBarcode = "";
  53. home.FunsEip.Add(GlobalContext.IsUsePLCNow,
  54. new Inovance_EIP(GlobalContext.PC1Address, GlobalContext.Machine1Address)); //OP10 壳体清洁上料装备
  55. foreach (Inovance_EIP plcEIP in home.FunsEip.Values)
  56. {
  57. if (plcEIP != null)
  58. {
  59. try
  60. {
  61. (int, string) result = plcEIP.Connect();
  62. }
  63. catch (Exception ex)
  64. {
  65. MessageBox.Show($"PLC[{plcEIP._pcIPStr}]连接失败!失败信息:" + ex.Message,
  66. "PLC连接提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
  67. MessageBoxOptions.ServiceNotification);
  68. }
  69. }
  70. }
  71. List<Task> TaskReadProcess = new List<Task>(); // 线程-触发点位(PLC)的线程
  72. TaskReadProcess.Add(new Task(() => { ReadStation(); })); //OP10 壳体清洁上料装备
  73. // 开启PLC的业务处理线程-监听PLC点位+状态
  74. foreach (Task task in TaskReadProcess)
  75. {
  76. if (task != null)
  77. task.Start();
  78. }
  79. }
  80. private void InputTimer_Tick(object sender, EventArgs e)
  81. {
  82. int nRet = 0;
  83. string strRet = "";
  84. string stationNameStr = stationCode + stationName;
  85. // 定时器触发,表示输入已经完成
  86. _inputTimer.Stop();
  87. //模拟点击
  88. GetBarcode.Focus();
  89. //收到PLC确认反馈,关闭弹窗
  90. if (isNoClose) {
  91. closeWin();
  92. }
  93. // 处理条码
  94. string Barcode = GetBarcode.Text.Trim();
  95. if (!string.IsNullOrEmpty(Barcode))
  96. {
  97. if (Barcode.Length > 9)
  98. ProductBarcode.Text = Barcode;
  99. else
  100. CarrierBarcode.Text= Barcode;
  101. Form_Home._strProductBarcode = ProductBarcode.Text.Trim();
  102. Form_Home._strCarrierBarcode = CarrierBarcode.Text.Trim();
  103. GetBarcode.Text = "";
  104. }
  105. if (!string.IsNullOrEmpty(ProductBarcode.Text) && !string.IsNullOrEmpty(CarrierBarcode.Text))
  106. {
  107. CommandFromPLC CommandToPlC = new CommandFromPLC();
  108. CommandToPlC.cmd = 0;
  109. CommandToPlC.cmdParam = 2;
  110. CommandToPlC.cmdResult = 0;
  111. (nRet, strRet) = home.WriteResultToPlc(plcNo, stationNameStr, tagBaseName + "." + tagMesCommName, 1, CommandToPlC);
  112. if (nRet != 0)
  113. {
  114. ErrorLab.Text = $"PLC写入失败!失败原因:" + strRet;
  115. }
  116. else
  117. {
  118. ErrorLab.Text = $"扫码完成,请点击下方设备屏幕上确认按键!";
  119. }
  120. }
  121. }
  122. private void GetBarcode_TextChanged(object sender, EventArgs e)
  123. {
  124. // 每次文本变化时重置定时器
  125. _inputTimer.Stop();
  126. _inputTimer.Start();
  127. }
  128. private void closeWin()
  129. {
  130. this.DialogResult = DialogResult.OK;
  131. //Form_Home.StopWhile = false;
  132. this.Close();
  133. }
  134. private void BandBarodeDialog_S1_FormClosing(object sender, FormClosingEventArgs e)
  135. {
  136. //Form_Home.StopWhile = false;
  137. }
  138. private void ReadStation() {
  139. while (true)
  140. {
  141. (int, string) result = home.FunsEip[plcNo]
  142. .Read_SingleTag<OP10_MesData_t>(tagBaseName, 1, out stPLC_MesData, this); //读取单个结构体数据
  143. if (result.Item1==0)
  144. {
  145. if (stPLC_MesData.mesCommFrmPLC.cmdParam==3)
  146. {
  147. closeWin();
  148. };
  149. }
  150. }
  151. }
  152. private void MainForm_Shown(object sender, EventArgs e)
  153. {
  154. // 将焦点设置到 textBox1
  155. GetBarcode.Focus();
  156. }
  157. }
  158. }