Dialog_BandBarode_S1.cs 6.8 KB

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