AdvantecIoCard.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.Util;
  3. using MECF.Framework.RT.Core.IoProviders;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Xml;
  7. namespace athosRT.Devices.IO
  8. {
  9. public class AdvantecIoCard : IoProvider
  10. {
  11. private DICard _diCard;
  12. private DOCard _doCard;
  13. private IOCard _ioCard;
  14. private string _type;
  15. protected override void SetParameter(XmlElement nodeParameter)
  16. {
  17. string attribute1 = nodeParameter.GetAttribute("name");
  18. int deviceID = int.Parse(nodeParameter.GetAttribute("device_id"));
  19. this._type = nodeParameter.GetAttribute("type");
  20. string attribute2 = nodeParameter.GetAttribute("description");
  21. int port = int.Parse(nodeParameter.GetAttribute("port"));
  22. if (this._type == "di")
  23. {
  24. this._diCard = new DICard(attribute1, deviceID);
  25. this._diCard.Open();
  26. }
  27. else if (this._type == "do")
  28. {
  29. this._doCard = new DOCard(attribute1, deviceID);
  30. this._doCard.Open();
  31. }
  32. else
  33. {
  34. if (!(this._type == "dio"))
  35. return;
  36. this._ioCard = new IOCard(attribute2, port);
  37. }
  38. }
  39. protected override bool OnTimer()
  40. {
  41. try
  42. {
  43. foreach (IoBlockItem blockSection in this._blockSections)
  44. {
  45. if (blockSection.Type == IoType.DI)
  46. {
  47. bool[] buffer = this.ReadDi(blockSection.Offset, (int) blockSection.Size);
  48. if (buffer != null)
  49. this._buffer.SetDiBuffer(this._source, blockSection.Offset, buffer);
  50. }
  51. }
  52. Dictionary<int, bool[]> doBuffer = this._buffer.GetDoBuffer(this._source);
  53. if (doBuffer != null)
  54. {
  55. foreach (KeyValuePair<int, bool[]> keyValuePair in doBuffer)
  56. this.WriteDo(keyValuePair.Key, keyValuePair.Value);
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\AdvantecIoCard.cs", nameof (OnTimer), 75);
  62. this.Close();
  63. }
  64. return true;
  65. }
  66. public override void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, Dictionary<int, string> ioMappingPathFile)
  67. {
  68. Module = module;
  69. Name = name;
  70. _source = module + "." + name;
  71. _buffer = buffer;
  72. _nodeParameter = nodeParameter;
  73. _blockSections = lstBuffers;
  74. buffer.SetBufferBlock(_source, lstBuffers);
  75. buffer.SetIoMap(_source, ioMappingPathFile);
  76. SetParameter(nodeParameter);
  77. State = IoProviderStateEnum.Uninitialized;
  78. _thread = new PeriodicJob(50, OnTimer, name);
  79. }
  80. public override void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule)
  81. {
  82. Module = module;
  83. Name = name;
  84. _source = module + "." + name;
  85. _buffer = buffer;
  86. _nodeParameter = nodeParameter;
  87. _blockSections = lstBuffers;
  88. buffer.SetBufferBlock(_source, lstBuffers);
  89. buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);
  90. SetParameter(nodeParameter);
  91. State = IoProviderStateEnum.Uninitialized;
  92. _thread = new PeriodicJob(50, OnTimer, name);
  93. }
  94. protected override void Open()
  95. {
  96. }
  97. protected override void Close()
  98. {
  99. }
  100. protected override bool[] ReadDi(int offset, int size)
  101. {
  102. if (this._type == "dio")
  103. {
  104. byte[] buffer;
  105. if (this._ioCard == null || !this._ioCard.Read(out buffer))
  106. return (bool[]) null;
  107. bool[] flagArray = new bool[size];
  108. for (int index = 0; index < size && index < buffer.Length; ++index)
  109. flagArray[index] = buffer[index] == (byte) 1;
  110. return flagArray;
  111. }
  112. if (this._type != "di" || this._diCard == null)
  113. return (bool[]) null;
  114. byte[] di = this._diCard.DI;
  115. if (di == null || di.Length == 0)
  116. return (bool[]) null;
  117. bool[] flagArray1 = new bool[size];
  118. for (int index = 0; index < size && index < di.Length; ++index)
  119. flagArray1[index] = di[index] == (byte) 1;
  120. return flagArray1;
  121. }
  122. protected override short[] ReadAi(int offset, int size) => (short[]) null;
  123. protected override void WriteDo(int offset, bool[] buffer)
  124. {
  125. if (this._type == "dio")
  126. {
  127. if (this._ioCard == null || buffer == null || buffer.Length == 0)
  128. return;
  129. this._ioCard.Write(Array.ConvertAll<bool, byte>(buffer, (Converter<bool, byte>) (x => !x ? (byte) 0 : (byte) 1)));
  130. }
  131. else
  132. {
  133. if (this._type != "do" || this._doCard == null || buffer == null || buffer.Length == 0)
  134. return;
  135. this._doCard.Write(Array.ConvertAll<bool, byte>(buffer, (Converter<bool, byte>) (x => !x ? (byte) 0 : (byte) 1)));
  136. }
  137. }
  138. protected override void WriteAo(int offset, short[] buffer)
  139. {
  140. }
  141. }
  142. }