AI.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 AI : UserControl, IIOView
  14. {
  15. PeriodicJob _thread;
  16. public AI()
  17. {
  18. InitializeComponent();
  19. this.Load += new EventHandler(AI_Load);
  20. }
  21. void AI_Load(object sender, EventArgs e)
  22. {
  23. if (this.Controls.Count > 0)
  24. return;
  25. List<Tuple<int, int, string>> ioList = IO.GetIONameList(IOType.AI);
  26. int index = 0;
  27. foreach (var item in ioList)
  28. {
  29. AICtrl aiCtrl1 = new AICtrl();
  30. aiCtrl1.Location = new System.Drawing.Point(index % 6 * 162, index / 6 * 38);
  31. aiCtrl1.SetName(string.Format("{0}.{1}", item.Item1, item.Item3));
  32. aiCtrl1.Name = string.Format("AI_{0}", item.Item1);
  33. aiCtrl1.Size = new System.Drawing.Size(161, 37);
  34. aiCtrl1.Visible = true;
  35. aiCtrl1.Tag = item.Item3;
  36. this.Controls.Add(aiCtrl1);
  37. index++;
  38. }
  39. _thread = new PeriodicJob(500, OnTimer, "AITimer", false, true);
  40. VisibleChanged += (sender1, e1) =>
  41. {
  42. if (Visible) _thread.Start(); else _thread.Pause();
  43. };
  44. }
  45. bool OnTimer()
  46. {
  47. Invoke(new Action(() =>
  48. {
  49. foreach (AICtrl ctrl in Controls)
  50. {
  51. if (ctrl != null)
  52. {
  53. ctrl.SetValue(IO.AI[(string)ctrl.Tag].Value);
  54. }
  55. }
  56. }));
  57. return true;
  58. }
  59. public void EnableTimer(bool enable)
  60. {
  61. if (enable)
  62. _thread.Start();
  63. else
  64. _thread.Pause();
  65. }
  66. public void Close()
  67. {
  68. if (_thread != null)
  69. _thread.Stop();
  70. }
  71. }
  72. }