using Aitex.Core.RT.Event; using Aitex.Core.RT.Log; using Aitex.Core.Util; using MECF.Framework.RT.Core.IoProviders; using System; using System.Collections.Generic; using System.Linq; using System.Xml; using Automation.BDaq; namespace EfemRT.Devices { public class AdvantecIoCard : IoProvider { private DICard _diCard; private DOCard _doCard; private IOCard _ioCard; private string _type; protected override void SetParameter(XmlElement nodeParameter) { string name = nodeParameter.GetAttribute("name"); int deviceId = int.Parse(nodeParameter.GetAttribute("device_id")); _type = nodeParameter.GetAttribute("type"); string description = nodeParameter.GetAttribute("description"); int port = int.Parse(nodeParameter.GetAttribute("port")); if (_type == "di") { _diCard = new DICard(name, deviceId); _diCard.Open(); } else if (_type == "do") { _doCard = new DOCard(name, deviceId); _doCard.Open(); } else if (_type == "dio") { //_ioCard = new IOCard(deviceId); _ioCard = new IOCard(description,port); } } protected override bool OnTimer() { try { foreach (var bufferSection in _blockSections) { if (bufferSection.Type == IoType.DI) { bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size); if (diBuffer != null) { _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer); //TraceArray(diBuffer); } } } Dictionary dos = _buffer.GetDoBuffer(_source); if (dos != null) { foreach (var doo in dos) { WriteDo(doo.Key, doo.Value); } } } catch (Exception ex) { LOG.Write(ex); Close(); } return true; } protected override void Open() { } protected override void Close() { } protected override bool[] ReadDi(int offset, int size) { if (_type == "dio") { if (_ioCard == null) return null; if (_ioCard.Read(out byte[] buffer)) { bool[] result1 = new bool[size]; for (int i = 0; i < size && i < buffer.Length; i++) { result1[i] = buffer[i] == 1; } return result1; } return null; } if (_type != "di" || _diCard == null) return null; byte[] data = _diCard.DI; if (data == null || data.Length == 0) return null; bool[] result = new bool[size]; for (int i = 0; i < size && i < data.Length; i++) { result[i] = data[i] == 1; } return result; } protected override short[] ReadAi(int offset, int size) { return null; } protected override void WriteDo(int offset, bool[] buffer) { if (_type == "dio") { if (_ioCard == null) return; if (buffer == null || buffer.Length == 0) return; _ioCard.Write(Array.ConvertAll(buffer, x => x ? (byte)1 : (byte)0)); return; } if (_type != "do" || _doCard == null) return; if (buffer == null || buffer.Length == 0) return; _doCard.Write(Array.ConvertAll(buffer, x => x ? (byte)1 : (byte)0)); } protected override void WriteAo(int offset, short[] buffer) { } } public class DICard { public string Name { get; set; } private int _portCount; public byte[] DI { get { return _buff; } } private byte[] _buff = null; private InstantDiCtrl _ctrl = null; private int _deviceNumber = -1; private R_TRIG _trigError = new R_TRIG(); public DICard(string name, int deviceID) { Name = name; _deviceNumber = deviceID; _ctrl = new InstantDiCtrl(); _ctrl.SelectedDevice = new DeviceInformation(deviceID); } public bool Open() { if (!_ctrl.Initialized) { LOG.Write($"{Name} : {_deviceNumber} initialize failed"); return false; } _portCount = _ctrl.PortCount; _buff = new Byte[_portCount * 8]; return true; } public bool Read() { if (!_ctrl.Initialized) { return false; } ErrorCode err = ErrorCode.Success; for (int i = 0; i < _portCount; i++) { for (int j = 0; j < 8; j++) { err = _ctrl.ReadBit(i, j, out byte data); _buff[i * 8 + j] = data; _trigError.CLK = err != ErrorCode.Success; if (_trigError.Q) { LOG.Write($"{Name} : {_deviceNumber} read error: {err}"); break; } } } if (_trigError.M) { return false; } return true; } public bool Write(byte[] buffer) { return true; } public bool Reset() { return true; } public bool Close() { return true; } } public class DOCard { public string Name { get; set; } private int _portCount; private byte[] _buff = null; R_TRIG _trigError = new R_TRIG(); private InstantDoCtrl _ctrl = null; public DOCard(string name, int deviceID) { Name = name; _ctrl = new InstantDoCtrl(); _ctrl.SelectedDevice = new DeviceInformation(deviceID); } public bool Open() { if (!_ctrl.Initialized) { LOG.Write("Open DO card failed."); return false; } _portCount = _ctrl.PortCount; _buff = new Byte[_portCount * 8]; return true; } public bool Read() { return true; } public bool Write(byte[] buffer) { if (!_ctrl.Initialized) { return false; } for (int i = 0; i < _portCount * 8 && i < buffer.Length; i++) { _buff[i] = buffer[i]; } ErrorCode err = _ctrl.Write(0, _portCount, _buff); _trigError.CLK = err != ErrorCode.Success; if (_trigError.Q) { LOG.Write("Write DO failed."); } if (err != ErrorCode.Success) { return false; } return true; } public bool Reset() { return true; } public bool Close() { return true; } } public enum IOStatus : byte { 关闭 = 0, 打开 = 1 }; public class IOCard { /// /// 构造函数 /// /// // public IOCard(int deviceNum = 0) public IOCard(string deviceNum,int port) { try { _diCtrl.SelectedDevice = new DeviceInformation(deviceNum); _doCtrl.SelectedDevice = new DeviceInformation(deviceNum); init = true; LOG.Write("IO Card selected"); } catch (Exception e) { //string str1=e.ToString(); init = false; //MessageBox.Show("None Find IOCard" + deviceNum.ToString() + " !", " IOCard", MessageBoxButtons.OK, MessageBoxIcon.Warning); } cardName = _diCtrl.SelectedDevice.Description; _portCount = port; _buff = new Byte[_portCount * 8]; } public string Name { get; set; } private int _portCount; private byte[] _buff = null; R_TRIG _trigError = new R_TRIG(); #region Global /// /// 输出 /// private InstantDoCtrl _doCtrl = new InstantDoCtrl(); /// /// 输入 /// private InstantDiCtrl _diCtrl = new InstantDiCtrl(); /// /// 是否初始化 /// private bool init = false; /// /// 初始化 /// public bool Init { get { return init; } } /// /// 设备名(Default:N/A) /// private string cardName = "N/A"; /// /// 设备名(Default:N/A) /// public string CardName { get { return cardName; } } #endregion public bool Write(byte[] buffer) { if (!_doCtrl.Initialized) { return false; } ErrorCode err = ErrorCode.Success; for (int i = 0; i < _portCount; i++) { for (int j = 0; j < 8; j++) { if (i * 8 + j < buffer.Length) err = _doCtrl.WriteBit(i, j, buffer[i * 8 + j]); _trigError.CLK = err != ErrorCode.Success; if (_trigError.Q) { LOG.Write($"{Name} : write error: {err}"); break; } } } _trigError.CLK = err != ErrorCode.Success; if (_trigError.Q) { LOG.Write("Write DO failed."); } if (err != ErrorCode.Success) { return false; } return true; } public bool Read(out byte[] buffer) { if (!_diCtrl.Initialized) { LOG.Write($"{Name} : not initialized"); buffer = null; return false; } ErrorCode err = ErrorCode.Success; for (int i = 0; i < _portCount; i++) { for (int j = 0; j < 8; j++) { err = _diCtrl.ReadBit(i, j, out byte data); _buff[i * 8 + j] = data; _trigError.CLK = err != ErrorCode.Success; if (_trigError.Q) { LOG.Write($"{Name} : read error: {err}"); break; } } } if (_trigError.M) { buffer = null; return false; } buffer = _buff; return true; } /// /// 销毁 /// public void Disp() { _doCtrl.Cleanup(); _diCtrl.Cleanup(); _doCtrl.Dispose(); _diCtrl.Dispose(); } } }