AO.cs 2.5 KB

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