IOCard.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using Automation.BDaq;
  5. using System;
  6. namespace athosRT.Devices.IO
  7. {
  8. public class IOCard
  9. {
  10. private int _portCount;
  11. private byte[] _buff = (byte[]) null;
  12. private R_TRIG _trigError = new R_TRIG();
  13. private InstantDoCtrl _doCtrl = new InstantDoCtrl();
  14. private InstantDiCtrl _diCtrl = new InstantDiCtrl();
  15. private bool init = false;
  16. private string cardName = "N/A";
  17. public IOCard(string deviceNum, int port)
  18. {
  19. try
  20. {
  21. this._diCtrl.SelectedDevice = new DeviceInformation(deviceNum);
  22. this._doCtrl.SelectedDevice = new DeviceInformation(deviceNum);
  23. this.init = true;
  24. LOG.Write("IO Card selected", file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\AdvantecIoCard.cs", member: ".ctor", line: 361);
  25. }
  26. catch (Exception ex)
  27. {
  28. this.init = false;
  29. }
  30. this.cardName = this._diCtrl.SelectedDevice.Description;
  31. this._portCount = port;
  32. this._buff = new byte[this._portCount * 8];
  33. }
  34. public string Name { get; set; }
  35. public bool Init => this.init;
  36. public string CardName => this.cardName;
  37. public bool Write(byte[] buffer)
  38. {
  39. if (!this._doCtrl.Initialized)
  40. return false;
  41. ErrorCode errorCode = ErrorCode.Success;
  42. for (int port = 0; port < this._portCount; ++port)
  43. {
  44. for (int bit = 0; bit < 8; ++bit)
  45. {
  46. if (port * 8 + bit < buffer.Length)
  47. errorCode = this._doCtrl.WriteBit(port, bit, buffer[port * 8 + bit]);
  48. this._trigError.CLK = errorCode != 0;
  49. if (this._trigError.Q)
  50. {
  51. LOG.Write(string.Format("{0} : write error: {1}", (object) this.Name, (object) errorCode), file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\AdvantecIoCard.cs", member: nameof (Write), line: 448);
  52. break;
  53. }
  54. }
  55. }
  56. this._trigError.CLK = errorCode != 0;
  57. if (this._trigError.Q)
  58. LOG.Write("Write DO failed.", file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\AdvantecIoCard.cs", member: nameof (Write), line: 458);
  59. return errorCode == 0;
  60. }
  61. public bool Read(out byte[] buffer)
  62. {
  63. if (!this._diCtrl.Initialized)
  64. {
  65. LOG.Write(this.Name + " : not initialized", file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\AdvantecIoCard.cs", member: nameof (Read), line: 474);
  66. buffer = (byte[]) null;
  67. return false;
  68. }
  69. for (int port = 0; port < this._portCount; ++port)
  70. {
  71. for (int bit = 0; bit < 8; ++bit)
  72. {
  73. byte data;
  74. ErrorCode errorCode = this._diCtrl.ReadBit(port, bit, out data);
  75. this._buff[port * 8 + bit] = data;
  76. this._trigError.CLK = errorCode != 0;
  77. if (this._trigError.Q)
  78. {
  79. LOG.Write(string.Format("{0} : read error: {1}", (object) this.Name, (object) errorCode), file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\AdvantecIoCard.cs", member: nameof (Read), line: 490);
  80. break;
  81. }
  82. }
  83. }
  84. if (this._trigError.M)
  85. {
  86. buffer = (byte[]) null;
  87. return false;
  88. }
  89. buffer = this._buff;
  90. return true;
  91. }
  92. public void Disp()
  93. {
  94. this._doCtrl.Cleanup();
  95. this._diCtrl.Cleanup();
  96. this._doCtrl.Dispose();
  97. this._diCtrl.Dispose();
  98. }
  99. }
  100. }