123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- 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<int, bool[]> 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
- {
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="deviceNum"></param>
- // 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
- /// <summary>
- /// 输出
- /// </summary>
- private InstantDoCtrl _doCtrl = new InstantDoCtrl();
- /// <summary>
- /// 输入
- /// </summary>
- private InstantDiCtrl _diCtrl = new InstantDiCtrl();
- /// <summary>
- /// 是否初始化
- /// </summary>
- private bool init = false;
- /// <summary>
- /// 初始化
- /// </summary>
- public bool Init
- {
- get
- {
- return init;
- }
- }
- /// <summary>
- /// 设备名(Default:N/A)
- /// </summary>
- private string cardName = "N/A";
- /// <summary>
- /// 设备名(Default:N/A)
- /// </summary>
- 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;
- }
- /// <summary>
- /// 销毁
- /// </summary>
- public void Disp()
- {
- _doCtrl.Cleanup();
- _diCtrl.Cleanup();
- _doCtrl.Dispose();
- _diCtrl.Dispose();
- }
- }
- }
|