DI.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.Util;
  11. namespace Aitex.Core.Backend
  12. {
  13. public partial class DI : UserControl, IIOView
  14. {
  15. PeriodicJob _thread;
  16. public DI()
  17. {
  18. InitializeComponent();
  19. this.Load += new EventHandler(DI_Load);
  20. }
  21. void DI_Load(object sender, EventArgs e)
  22. {
  23. if (this.panel1.Controls.Count > 0)
  24. return;
  25. BuildIOTable();
  26. _thread = new PeriodicJob(500, OnTimer, "DITimer", false, true);
  27. VisibleChanged += (sender1, e1) =>
  28. {
  29. if (Visible) _thread.Start(); else _thread.Pause();
  30. };
  31. }
  32. private void BuildIOTable()
  33. {
  34. List<Tuple<int, int, string>> ioList = IO.GetIONameList(IOType.DI);
  35. int index = 0;
  36. foreach (var item in ioList)
  37. {
  38. DICtrl diCtrl1 = new DICtrl
  39. {
  40. Location = new System.Drawing.Point(0, index * 30),
  41. LabelName = $"DI-{item.Item1}.{item.Item3}",
  42. IOName = item.Item3,
  43. Name = $"DI_{item.Item1}",
  44. Size = new System.Drawing.Size(350, 25),
  45. Tag = item.Item3
  46. };
  47. this.panel1.Controls.Add(diCtrl1);
  48. index++;
  49. }
  50. List<Tuple<int, int, string>> ioListRemote = IO.GetIONameList("remote", IOType.DI);
  51. index = 0;
  52. foreach (var item in ioListRemote)
  53. {
  54. DICtrl diCtrl1 = new DICtrl
  55. {
  56. Location = new System.Drawing.Point(355, index * 30),
  57. LabelName = $"DI-{item.Item1}.{item.Item3}",
  58. IOName = item.Item3,
  59. Name = $"DI_{item.Item1}",
  60. Size = new System.Drawing.Size(350, 25),
  61. Tag = item.Item3
  62. };
  63. this.panel1.Controls.Add(diCtrl1);
  64. index++;
  65. }
  66. // OLD
  67. /*
  68. List<Tuple<int, int, string>> ioList = IO.GetIONameList(IOType.DI);
  69. List<Tuple<int, int, string>> ioListRemote = IO.GetIONameList("remote", IOType.DI);
  70. foreach (var tuple in ioListRemote)
  71. {
  72. ioList.Add(tuple);
  73. }
  74. int totalColumn = 3;
  75. int countPerColumn = (ioList.Count / totalColumn) + ((ioList.Count % totalColumn) > 0 ? 1 : 0);
  76. int index = 0;
  77. foreach (var item in ioList)
  78. {
  79. int row = index % countPerColumn;
  80. int col = index / countPerColumn;
  81. DICtrl diCtrl1 = new DICtrl();
  82. diCtrl1.Location = new System.Drawing.Point(col * 300, row * 25);
  83. diCtrl1.SetName(string.Format("DI-{0}.{1}", item.Item1, item.Item3));
  84. diCtrl1.SetIoName("local", item.Item3);
  85. diCtrl1.Name = string.Format("DI_{0}", item.Item1);
  86. diCtrl1.Size = new System.Drawing.Size(300, 25);
  87. diCtrl1.Tag = item.Item3;
  88. diCtrl1.SetSimulatorValue += (s, b) =>
  89. {
  90. if (UserLoginView.Simulator != null)
  91. UserLoginView.Simulator.SetDi("local", s, b ? (byte)1 : (byte)0);
  92. };
  93. this.panel1.Controls.Add(diCtrl1);
  94. index++;
  95. }
  96. */
  97. }
  98. bool OnTimer()
  99. {
  100. Invoke(new Action(() =>
  101. {
  102. foreach (DICtrl ctrl in panel1.Controls)
  103. {
  104. string name = (string)ctrl.Tag;
  105. if (ctrl != null)
  106. {
  107. ctrl.SetValue(IO.DI[name].Value, IO.DI[name].RawData);
  108. }
  109. }
  110. }));
  111. return true;
  112. }
  113. public void EnableTimer(bool enable)
  114. {
  115. if (enable)
  116. _thread.Start();
  117. else
  118. _thread.Pause();
  119. }
  120. public void Close()
  121. {
  122. if (_thread != null)
  123. _thread.Stop();
  124. }
  125. }
  126. }