ContecIoCard.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Log;
  5. using CdioCs;
  6. using MECF.Framework.Common.Communications;
  7. using MECF.Framework.RT.Core.IoProviders;
  8. namespace MECF.Framework.RT.Core.IoProviders
  9. {
  10. public class ContecIoCard:IoProvider, IConnection
  11. {
  12. public string Address
  13. {
  14. get { return _device.Name; }
  15. }
  16. public bool IsConnected
  17. {
  18. get { return IsOpened; }
  19. }
  20. public bool Connect()
  21. {
  22. return true;
  23. }
  24. public bool Disconnect()
  25. {
  26. return true;
  27. }
  28. private Cdio _dio = new Cdio();
  29. private ContecIoDevice _device;
  30. private int _inputsize = 0;
  31. private int _outputsize = 0;
  32. private Byte[] _inputDI;
  33. private Byte[] _ouputDO;
  34. //<Parameter name="System.dio000" in_offset="0" in_size="64" out_offset="0" out_size="64"></Parameter>
  35. protected override void SetParameter(XmlElement nodeParameter)
  36. {
  37. string name = nodeParameter.GetAttribute("name");
  38. int in_offset = int.Parse(nodeParameter.GetAttribute("in_offset"));
  39. int in_size = int.Parse(nodeParameter.GetAttribute("in_size"));
  40. int out_offset = int.Parse(nodeParameter.GetAttribute("out_offset"));
  41. int out_size = int.Parse(nodeParameter.GetAttribute("out_size"));
  42. _device = new ContecIoDevice(_dio, name, in_offset, (short)in_size, out_offset, (short)out_size);
  43. _inputsize = Math.Max(_inputsize, _device.InputOffset + _device.InputSize);
  44. _outputsize = Math.Max(_inputsize, _device.OutputOffset + _device.OutputSize);
  45. _inputDI = new byte[_inputsize];
  46. _ouputDO = new byte[_outputsize];
  47. ConnectionManager.Instance.Subscribe(name, this);
  48. }
  49. protected override void Open()
  50. {
  51. if (!_device.Open())
  52. {
  53. EV.PostAlarmLog("System", "can not open io card");
  54. return;
  55. }
  56. SetState(IoProviderStateEnum.Opened);
  57. }
  58. protected override void Close()
  59. {
  60. try
  61. {
  62. _device.Reset();
  63. _device.Close();
  64. }
  65. catch (Exception ex)
  66. {
  67. LOG.Write(ex);
  68. }
  69. }
  70. protected override bool[] ReadDi(int offset, int size)
  71. {
  72. try
  73. {
  74. _device.Read();
  75. for (short i = 0; i < _device.InputSize / 8; i++)
  76. {
  77. for (short j = 0; j < 8; j++)
  78. _inputDI[_device.InputOffset + i * 8 + j] = (byte)((_device.Input[i] >> j) & 0x01);
  79. }
  80. return Array.ConvertAll(_inputDI, x => x == 1);
  81. }
  82. catch (Exception e)
  83. {
  84. LOG.Write(e);
  85. }
  86. return null;
  87. }
  88. protected override short[] ReadAi(int offset, int size)
  89. {
  90. return null;
  91. }
  92. protected override void WriteDo(int offset, bool[] buffer)
  93. {
  94. try
  95. {
  96. _ouputDO = Array.ConvertAll(buffer, x => x ? (byte)1 : (byte)0);
  97. _device.Write(_ouputDO);
  98. }
  99. catch (Exception e)
  100. {
  101. LOG.Write(e);
  102. }
  103. }
  104. protected override void WriteAo(int offset, short[] buffer)
  105. {
  106. }
  107. }
  108. public class ContecIoDevice
  109. {
  110. public string Name { get; set; }
  111. public int InputOffset { get; set; } // Input 起点
  112. public short InputSize { get; set; } // 长度
  113. public int OutputOffset { get; set; } // Outnput 起点
  114. public short OutputSize { get; set; }
  115. public byte[] Input { get { return _inputBuff; } }
  116. private Cdio _io = null;
  117. private short _id = 0;
  118. private short _inputSize;
  119. private byte[] _inputBuff = null;
  120. private short[] _inputPort = null;
  121. private short _outputSize;
  122. private byte[] _outputBuff = null;
  123. private short[] _outputPort = null;
  124. public ContecIoDevice(Cdio io, string name, int inoffset, short inlen, int outoffset, short outlen)
  125. {
  126. _io = io;
  127. Name = name;
  128. InputOffset = inoffset;
  129. InputSize = inlen;
  130. OutputOffset = outoffset;
  131. OutputSize = outlen;
  132. }
  133. public bool Open()
  134. {
  135. int ret = _io.Init(Name, out _id);
  136. if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
  137. {
  138. string error;
  139. _io.GetErrorString(ret, out error);
  140. LOG.Error(string.Format("Init IO Card {0} failed,{1}", Name, error));
  141. return false;
  142. }
  143. _io.GetMaxPorts(_id, out _inputSize, out _outputSize);
  144. if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
  145. {
  146. string error;
  147. _io.GetErrorString(ret, out error);
  148. LOG.Error(string.Format("Init IO Card {0} failed,{1}", Name, error));
  149. return false;
  150. }
  151. int len = InputSize / 8;
  152. _inputBuff = new byte[len];
  153. _inputPort = new short[len];
  154. for (short i = 1; i < len; i++)
  155. _inputPort[i] = i;
  156. len = OutputSize / 8;
  157. _outputBuff = new byte[len];
  158. _outputPort = new short[len];
  159. for (short i = 0; i < len; i++)
  160. _outputPort[i] = i;
  161. return true;
  162. }
  163. public bool Read()
  164. {
  165. short size = Math.Min(_inputSize, (short)(InputSize / 8));
  166. int ret = _io.InpMultiByte(_id, _inputPort, size, _inputBuff);
  167. if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
  168. {
  169. string error;
  170. _io.GetErrorString(ret, out error);
  171. LOG.Error(string.Format("Read IO Card {0} failed,{1}", Name, error));
  172. return false;
  173. }
  174. return true;
  175. }
  176. public bool Write(byte[] buffer)
  177. {
  178. short size = Math.Min(_outputSize, (short)(OutputSize / 8));
  179. for (int i = 0; i < size; i++)
  180. {
  181. byte value = 0;
  182. for (int j = 0; j < 8; j++)
  183. {
  184. value |= (byte)((buffer[OutputOffset + i * 8 + j] & 0x01) << j);
  185. }
  186. _outputBuff[i] = value;
  187. }
  188. int ret = _io.OutMultiByte(_id, _outputPort, size, _outputBuff);
  189. if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
  190. {
  191. string error;
  192. _io.GetErrorString(ret, out error);
  193. LOG.Error(string.Format("Write IO Card {0} failed,{1}", Name, error));
  194. return false;
  195. }
  196. return true;
  197. }
  198. public bool Reset()
  199. {
  200. return _io.ResetDevice(_id) == (int)CdioConst.DIO_ERR_SUCCESS;
  201. }
  202. public bool Close()
  203. {
  204. return _io.Exit(_id) == (int)CdioConst.DIO_ERR_SUCCESS;
  205. }
  206. }
  207. }