Dialog_MaterialVerify_S1.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using EIP_Protocol;
  2. using Sunny.UI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace MainForm.FaForm
  9. {
  10. public partial class Dialog_MaterialVerify_S1 : Form
  11. {
  12. Form_Home home =new Form_Home();
  13. int plcNo = 1;
  14. string stationCode = "";
  15. string stationName = "";
  16. string tagBaseName = ""; //标签变量名称
  17. string tagMesCommName = "mesCommToPC"; //标签变量名称
  18. string tagBarsetName = "BarcodeSet";
  19. (int, string) resultPLC;
  20. OP30_MesData_t OP30PLC_MesData; //PLC的MES数据
  21. OP70_MesData_t OP70PLC_MesData; //PLC的MES数据
  22. private Timer _inputTimer;
  23. public Dialog_MaterialVerify_S1(string Code,string Name,string tagName)
  24. {
  25. InitializeComponent();
  26. //置顶
  27. this.TopMost = true;
  28. // 设置窗口相对于父窗口居中
  29. this.StartPosition = FormStartPosition.CenterParent;
  30. this.Shown += MainForm_Shown;
  31. stationCode = Code;
  32. stationName = Name;
  33. tagBaseName = tagName;
  34. }
  35. private void Dialog_MaterialVerify_S1_Load(object sender, System.EventArgs e)
  36. {
  37. home.FunsEip.Add(GlobalContext.IsUsePLCNow,
  38. new Inovance_EIP(GlobalContext.PC1Address, GlobalContext.Machine1Address)); //OP10 壳体清洁上料装备
  39. foreach (Inovance_EIP plcEIP in home.FunsEip.Values)
  40. {
  41. if (plcEIP != null)
  42. {
  43. try
  44. {
  45. (int, string) result = plcEIP.Connect();
  46. }
  47. catch (Exception ex)
  48. {
  49. MessageBox.Show($"PLC[{plcEIP._pcIPStr}]连接失败!失败信息:" + ex.Message,
  50. "PLC连接提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
  51. MessageBoxOptions.ServiceNotification);
  52. }
  53. }
  54. }
  55. List<Task> TaskReadProcess = new List<Task>(); // 线程-触发点位(PLC)的线程
  56. TaskReadProcess.Add(new Task(() => { ReadStation(); })); // 开启PLC的业务处理线程-监听PLC点位+状态
  57. foreach (Task task in TaskReadProcess)
  58. {
  59. if (task != null)
  60. task.Start();
  61. }
  62. }
  63. private void button2_Click(object sender, System.EventArgs e)
  64. {
  65. // 初始化定时器
  66. _inputTimer = new Timer();
  67. _inputTimer.Interval = 500; // 500毫秒
  68. _inputTimer.Tick += InputTimer_Tick;
  69. }
  70. private void button1_Click(object sender, System.EventArgs e)
  71. {
  72. }
  73. private void ReadStation()
  74. {
  75. while (true)
  76. {
  77. if (stationCode.Contains("30"))
  78. {
  79. resultPLC = home.FunsEip[plcNo]
  80. .Read_SingleTag<OP30_MesData_t>(tagBaseName, 1, out OP30PLC_MesData, this); //读取单个结构体数据
  81. }
  82. if (stationCode.Contains("70"))
  83. {
  84. resultPLC = home.FunsEip[plcNo]
  85. .Read_SingleTag<OP30_MesData_t>(tagBaseName, 1, out OP30PLC_MesData, this); //读取单个结构体数据
  86. }
  87. if (resultPLC.Item1 == 0)
  88. {
  89. }
  90. }
  91. }
  92. private void MainForm_Shown(object sender, EventArgs e)
  93. {
  94. // 将焦点设置到 textBox1
  95. Barcode.Focus();
  96. }
  97. private void InputTimer_Tick(object sender, EventArgs e)
  98. {
  99. // 定时器触发,表示输入已经完成
  100. _inputTimer.Stop();
  101. //模拟点击
  102. Barcode.Focus();
  103. string strBarcode = Barcode.Text.Trim();
  104. if (!string.IsNullOrEmpty(strBarcode))
  105. {
  106. SN.Text = strBarcode;
  107. Barcode.Text = "";
  108. }
  109. }
  110. }
  111. }