BandBarodeDialog_S1.cs 5.5 KB

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