Dialog_BandBarode_S1.cs 6.6 KB

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