IDevice.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using Aitex.Core.RT.Log;
  10. namespace Aitex.Core.RT.Device
  11. {
  12. public interface IDevice
  13. {
  14. string Module { get; set; }
  15. string Name { get; set; }
  16. bool Initialize();
  17. void Monitor();
  18. void Terminate();
  19. void Reset();
  20. }
  21. public interface IModuleDevice
  22. {
  23. bool IsReady { get; }
  24. bool IsError { get; }
  25. bool IsInit { get; }
  26. bool Home(out string reason);
  27. }
  28. public class BaseDevice
  29. {
  30. public string UniqueName { get; set; }
  31. public string Module { get; set; }
  32. public string Name { get; set; }
  33. public string Display { get; set; }
  34. public string DeviceID { get; set; }
  35. private DeviceTimer counter = new DeviceTimer();
  36. public string ScBasePath { get; set; }
  37. public string IoBasePath { get; set; }
  38. public BaseDevice()
  39. {
  40. ScBasePath = "System";
  41. IoBasePath = "System";
  42. }
  43. public BaseDevice(string module, string name, string display, string id):this()
  44. {
  45. display = string.IsNullOrEmpty(display) ? name : display;
  46. id = string.IsNullOrEmpty(id) ? name : id;
  47. Module = module;
  48. Name = name;
  49. Display = display;
  50. DeviceID = id;
  51. UniqueName = $"{module}.{name}";
  52. }
  53. public DOAccessor ParseDoNode(string name, XmlElement node, string ioModule = "")
  54. {
  55. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  56. return IO.DO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  57. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  58. return null;
  59. }
  60. public DIAccessor ParseDiNode(string name, XmlElement node, string ioModule = "")
  61. {
  62. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  63. return IO.DI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  64. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  65. return null;
  66. }
  67. public AOAccessor ParseAoNode(string name, XmlElement node, string ioModule = "")
  68. {
  69. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  70. return IO.AO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  71. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  72. return null;
  73. }
  74. public AIAccessor ParseAiNode(string name, XmlElement node, string ioModule = "")
  75. {
  76. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  77. return IO.AI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  78. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  79. return null;
  80. }
  81. public SCConfigItem ParseScNode(string name, XmlElement node, string ioModule="", string defaultScPath="")
  82. {
  83. SCConfigItem result = null;
  84. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  85. result = SC.GetConfigItem(node.GetAttribute(name));
  86. if (result == null && !string.IsNullOrEmpty(defaultScPath) && SC.ContainsItem(defaultScPath))
  87. result = SC.GetConfigItem(defaultScPath);
  88. return result;
  89. }
  90. public SCConfigItem ParseScNodeEx(string name, XmlElement node, string ioModule = "")
  91. {
  92. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  93. {
  94. string aName = node.GetAttribute(name);
  95. if (string.IsNullOrWhiteSpace(ioModule))
  96. {
  97. return SC.GetConfigItem(aName);
  98. }
  99. else
  100. {
  101. return SC.GetConfigItem($"{ioModule}.{aName}");
  102. }
  103. }
  104. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  105. return null;
  106. }
  107. public static T ParseDeviceNode<T>(string name, XmlElement node) where T : class, IDevice
  108. {
  109. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  110. return DEVICE.GetDevice<T>(node.GetAttribute(name));
  111. //LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
  112. return null;
  113. }
  114. public static T ParseDeviceNode<T>(string module, string name, XmlElement node) where T : class, IDevice
  115. {
  116. string device_id = node.GetAttribute(name);
  117. if (!string.IsNullOrEmpty(device_id) && !string.IsNullOrEmpty(device_id.Trim()))
  118. {
  119. return DEVICE.GetDevice<T>($"{module}.{device_id}");
  120. }
  121. //LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
  122. return null;
  123. }
  124. protected void Counter()
  125. {
  126. if (counter.IsIdle())
  127. {
  128. counter.Start(10000);
  129. }
  130. else
  131. {
  132. //LOG.Info(String.Format("{0} Monitor elapsed {1}", Name, counter.GetElapseTime()));
  133. counter.Start(10000);
  134. }
  135. }
  136. protected float _GetRealFloat(IOAccessor<short> accessor)
  137. {
  138. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  139. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  140. float res = BitConverter.ToSingle(new[] { low[0], low[1], high[0], high[1] }, 0);
  141. return res;
  142. }
  143. protected void _SetRealFloat(IOAccessor<short> accessor, float val)
  144. {
  145. //val = val / 50 * 4000;
  146. byte[] val2 = BitConverter.GetBytes(val);
  147. accessor.Buffer[accessor.Index] = BitConverter.ToInt16(val2, 0);
  148. accessor.Buffer[accessor.Index + 1] = BitConverter.ToInt16(val2, 2);
  149. }
  150. protected float GetAiValue(string name)
  151. {
  152. byte[] high = BitConverter.GetBytes(IO.AI[name].Buffer[IO.AI[name].Index]);
  153. byte[] low = BitConverter.GetBytes(IO.AI[name].Buffer[IO.AI[name].Index + 1]);
  154. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  155. return flow;
  156. }
  157. protected float GetAoValue(string name)
  158. {
  159. byte[] high = BitConverter.GetBytes(IO.AO[name].Buffer[IO.AO[name].Index]);
  160. byte[] low = BitConverter.GetBytes(IO.AO[name].Buffer[IO.AO[name].Index + 1]);
  161. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  162. return flow;
  163. }
  164. }
  165. }