| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 | 
							- using Aitex.Common.Util;
 
- using Aitex.Core.RT.Log;
 
- using Aitex.Core.RT.SCCore;
 
- using Aitex.Core.Util;
 
- using MECF.Framework.Common.Device.Festo;
 
- using MECF.Framework.Common.Equipment;
 
- using MECF.Framework.Common.IOCore;
 
- using MECF.Framework.Common.Net;
 
- using Newtonsoft.Json;
 
- using System;
 
- using System.Collections.Concurrent;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Reflection;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- namespace MECF.Framework.Common.Device.Galil
 
- {
 
-     public class GalilControllerCfgManager : Singleton<GalilControllerCfgManager>
 
-     {
 
-         #region 常量 
 
-         private const string STOPCODE = "StopCode";
 
-         private const string REFERENCE_POSITION = "ReferencePosition";
 
-         private const string TORQUE = "Torque";
 
-         private const string VELOCITY = "Velocity";
 
-         private const string POSITION_ERROR = "PositionError";
 
-         private const string AUXILIARY_POSITION="AuxiliaryPosition";
 
-         private const string GAILI_21 = "Galil21";
 
-         private const string GALIL_40 = "Galil40";
 
-         #endregion
 
-         #region 内部变量
 
-         /// <summary>
 
-         /// 模块数据字典(key-模块名称,value-模块Galil数据)
 
-         /// </summary>
 
-         private Dictionary<string, GalilControllerData> _moduleGalilDataDictionary = new Dictionary<string, GalilControllerData>();
 
-         /// <summary>
 
-         /// 电机数据字典(key-电机名称,value-电机数据)
 
-         /// </summary>
 
-         private Dictionary<string, GalilAxisData> _moduleNameAxisDataDictionary = new Dictionary<string, GalilAxisData>();
 
-         /// <summary>
 
-         /// 电机索引字典(key-电机名称,value-索引)
 
-         /// </summary>
 
-         private Dictionary<string, int> _moduleNameIndexDictionary = new Dictionary<string, int>();
 
-         /// <summary>
 
-         /// 模块电机集合字典(key-模块名称,value-电机集合)
 
-         /// </summary>
 
-         private Dictionary<string,List<string>> _moduleNameLstDictionary= new Dictionary<string,List<string>>();
 
-         /// <summary>
 
-         /// 模块电机配置字典(key-电机名称,value-电机配置)
 
-         /// </summary>
 
-         private Dictionary<string, GalilAxisConfig> _moduleNameAxisConfigDictionary = new Dictionary<string, GalilAxisConfig>();
 
-         /// <summary>
 
-         /// 模块设备配置字典
 
-         /// </summary>
 
-         private Dictionary<string, GalilDeviceConfig> _moduleDeviceConfigDictionary = new Dictionary<string, GalilDeviceConfig>();
 
-         /// <summary>
 
-         /// 模块电机tcp设备字典(key-模块名称,value-tcp设备)
 
-         /// </summary>
 
-         private Dictionary<string, IGalilDevice> _moduleGalilTcpDeviceDictionary = new Dictionary<string, IGalilDevice>();
 
-         /// <summary>
 
-         /// 模块DI数组字典
 
-         /// </summary>
 
-         private ConcurrentDictionary<string, byte[]> _moduleDiDictionary = new ConcurrentDictionary<string, byte[]>();
 
-         /// <summary>
 
-         /// 电机
 
-         /// </summary>
 
-         private char[] _axisArray = null;
 
-         #endregion
 
-         /// <summary>
 
-         /// 初始化
 
-         /// </summary>
 
-         public void Initialize()
 
-         {
 
-             bool isSimulate = SC.GetValue<bool>("System.IsSimulatorMode");
 
-             string xmlPath = "";
 
-             try
 
-             {
 
-                 if (isSimulate)
 
-                 {
 
-                     xmlPath = PathManager.GetCfgDir() + "Devices\\GalilControllerCfg-Simulator.xml";
 
-                 }
 
-                 else
 
-                 {
 
-                     xmlPath = PathManager.GetCfgDir() + "Devices\\GalilControllerCfg.xml";
 
-                 }
 
-                 GalilControllerCfg cfg = CustomXmlSerializer.Deserialize<GalilControllerCfg>(new FileInfo(xmlPath));
 
-                 if (cfg != null)
 
-                 {
 
-                     foreach (var config in cfg.GalilDeviceConfigs)
 
-                     {
 
-                         InitializeGalilDevice(config);
 
-                         if (config.GalilType == GAILI_21)
 
-                         {
 
-                             Galil21TcpDevice galilDevice = new Galil21TcpDevice(config.Module, config.IpAddress, config.Port);
 
-                             galilDevice.ReceiveTimeout = config.RecvTimeout;
 
-                             galilDevice.SendTimeout = config.SendTimeout;
 
-                             galilDevice.Initialize();
 
-                             List<string> lst = _moduleNameLstDictionary[config.Module];
 
-                             foreach (var item in lst)
 
-                             {
 
-                                 _moduleGalilTcpDeviceDictionary[item] = galilDevice;
 
-                             }
 
-                         }
 
-                         else
 
-                         {
 
-                             Galil40TcpDevice galilDevice = new Galil40TcpDevice(config.Module, config.IpAddress, config.Port);
 
-                             galilDevice.ReceiveTimeout = config.RecvTimeout;
 
-                             galilDevice.SendTimeout = config.SendTimeout;
 
-                             galilDevice.Initialize();
 
-                             List<string> lst = _moduleNameLstDictionary[config.Module];
 
-                             foreach (var item in lst)
 
-                             {
 
-                                 _moduleGalilTcpDeviceDictionary[item] = galilDevice;
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             catch(Exception ex)
 
-             {
 
-                 LOG.WriteLog(eEvent.ERR_GALIL, "Galil", "Load galil xml failed");
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 初始化Galil 设备
 
-         /// </summary>
 
-         /// <param name="deviceConfig"></param>
 
-         private void InitializeGalilDevice(GalilDeviceConfig deviceConfig)
 
-         {
 
-             List<string> lst = new List<string>();
 
-             _moduleDeviceConfigDictionary[deviceConfig.Module] = deviceConfig;
 
-             foreach(var item in deviceConfig.GalilAxises)
 
-             {
 
-                 _moduleNameIndexDictionary[item.Name] = item.Index;
 
-                 _moduleNameAxisConfigDictionary[item.Name]=item;
 
-                 if (!lst.Contains(item.Name))
 
-                 {
 
-                     lst.Add(item.Name);
 
-                 }
 
-             }
 
-             if (lst.Count > 0)
 
-             {
 
-                 _moduleNameLstDictionary[deviceConfig.Module] = lst;                
 
-             }
 
-             _axisArray = new char[10];
 
-             int a = 'A';
 
-             for (int i = 0; i < _axisArray.Length; i++)
 
-             {
 
-                 _axisArray[i] = (char)(a + i);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 更新模块数据
 
-         /// </summary>
 
-         /// <param name="controllerData"></param>
 
-         public void UpdateModuleData(string module,GalilControllerData controllerData)
 
-         {
 
-             if (!_moduleGalilDataDictionary.ContainsKey(module))
 
-             {
 
-                 _moduleGalilDataDictionary[module] = controllerData;
 
-             }
 
-             if (!_moduleNameLstDictionary.ContainsKey(module))
 
-             {
 
-                 return;
 
-             }
 
-             UpdateModuleDI(module, controllerData.Inputs);
 
-             List<string> lst = _moduleNameLstDictionary[module];
 
-             foreach (var item in lst)
 
-             {
 
-                 string moduleName = item;
 
-                 if (!_moduleNameIndexDictionary.ContainsKey(moduleName))
 
-                 {
 
-                     continue;
 
-                 }
 
-                 int index = _moduleNameIndexDictionary[moduleName];
 
-                 if(index>=controllerData.GalilAxisDatas.Count)
 
-                 {
 
-                     continue;
 
-                 }
 
-                 GalilAxisData galilAxisData=controllerData.GalilAxisDatas[index];
 
-                 CheckAxisDataChanged(moduleName, galilAxisData);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 更新ModuleDI
 
-         /// </summary>
 
-         /// <param name="module"></param>
 
-         /// <param name="diValues"></param>
 
-         private void UpdateModuleDI(string module, byte[] diValues)
 
-         {
 
-             if (!_moduleDeviceConfigDictionary.ContainsKey(module))
 
-             {
 
-                 return;
 
-             }
 
-             GalilDeviceConfig galilDeviceConfig = _moduleDeviceConfigDictionary[module];
 
-             if (galilDeviceConfig.GalilDigIn == null || galilDeviceConfig.GalilDigIn.DIs == null)
 
-             {
 
-                 return;
 
-             }
 
-             bool isInitial = false;
 
-             if (!_moduleDiDictionary.ContainsKey(module))
 
-             {
 
-                 _moduleDiDictionary[module] = new byte[diValues.Length];
 
-                 isInitial=true;
 
-             }
 
-             if (_moduleDiDictionary[module].Length == diValues.Length)
 
-             {
 
-                 for(int i=0;i<diValues.Length;i++)
 
-                 {
 
-                     byte item= _moduleDiDictionary[module][i];
 
-                     byte diValue = diValues[i];
 
-                     if (isInitial)
 
-                     {
 
-                         _moduleDiDictionary[module][i]= diValue;
 
-                         UpdateDIValue(galilDeviceConfig,diValue, i);
 
-                     }
 
-                     else if (item != diValue)
 
-                     {
 
-                         _moduleDiDictionary[module][i] = diValue;
 
-                         UpdateDIValue(galilDeviceConfig, diValue, i);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 更新DI数值
 
-         /// </summary>
 
-         /// <param name="byt"></param>
 
-         /// <param name="index"></param>
 
-         private void UpdateDIValue(GalilDeviceConfig deviceConfig,byte byt,int index)
 
-         {
 
-             List<GalilDI> dIs = deviceConfig.GalilDigIn.DIs.FindAll(O => O.Address == index);
 
-             foreach(GalilDI item in dIs)
 
-             {
 
-                 bool value = false;
 
-                 if (item.Bit == 0)
 
-                 {
 
-                     value = (byt & 0x01) == 0x01;
 
-                 }
 
-                 else
 
-                 {
 
-                     value = (byt >> item.Bit & 0x01) == 0x01;
 
-                 }
 
-                 if (item.Invert)
 
-                 {
 
-                     value = !value;
 
-                 }
 
-                 IOModuleManager.Instance.UpdateIoValue(item.Name,value );
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 检验电机数据是否发生变化
 
-         /// </summary>
 
-         /// <param name="moduleName"></param>
 
-         /// <param name="axisData"></param>
 
-         private void CheckAxisDataChanged(string moduleName,GalilAxisData axisData)
 
-         {
 
-             if (_moduleNameAxisDataDictionary.ContainsKey(moduleName))
 
-             {
 
-                 NotifyGalilAxisData(moduleName, _moduleNameAxisDataDictionary[moduleName],axisData);
 
-                 _moduleNameAxisDataDictionary[moduleName].Copy(axisData);
 
-             }
 
-             else
 
-             {
 
-                 NotifyGalilAxisAllData(moduleName, axisData);
 
-                 _moduleNameAxisDataDictionary[moduleName] = axisData.Clone();
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 更新Galil电机数据
 
-         /// </summary>
 
-         /// <param name="axisData"></param>
 
-         private void NotifyGalilAxisAllData(string moduleName,GalilAxisData axisData)
 
-         {
 
-             PropertyInfo[] propertyInfos= axisData.GetType().GetProperties();
 
-             foreach(var info in propertyInfos)
 
-             {
 
-                 object value = info.GetValue(axisData);
 
-                 IOModuleManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", value);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 通知Galil电机数据
 
-         /// </summary>
 
-         /// <param name="sourceData"></param>
 
-         /// <param name="targetData"></param>
 
-         private void NotifyGalilAxisData(string moduleName,GalilAxisData sourceData, GalilAxisData targetData)
 
-         {
 
-             PropertyInfo[] propertyInfos = sourceData.GetType().GetProperties();
 
-             foreach (var info in propertyInfos)
 
-             {
 
-                 object sourceValue= info.GetValue(sourceData);
 
-                 object targetValue = info.GetValue(targetData);
 
-                 if (sourceValue.ToString() != targetValue.ToString())
 
-                 {
 
-                     IOModuleManager.Instance.UpdateIoValue($"{moduleName}.{info.Name}", targetValue);
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 设置
 
-         /// </summary>
 
-         /// <param name="module"></param>
 
-         /// <param name="name"></param>
 
-         /// <param name="variable"></param>
 
-         /// <param name="value"></param>
 
-         /// <returns></returns>
 
-         public bool SetSystemCommand(string module, string name, string commad, object value)
 
-         {
 
-             string str = $"{module}.{name}";
 
-             if (!_moduleGalilTcpDeviceDictionary.ContainsKey(str))
 
-             {
 
-                 LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
 
-                 return false;
 
-             }
 
-             if (_moduleNameIndexDictionary.ContainsKey(str))
 
-             {
 
-                 int index = _moduleNameIndexDictionary[str];
 
-                 if (index >= _axisArray.Length)
 
-                 {
 
-                     LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
 
-                     return false;
 
-                 }
 
-                 string strCommand = "";
 
-                 if (value != null)
 
-                 {
 
-                     strCommand = $"{commad}{value};";
 
-                 }
 
-                 else
 
-                 {
 
-                     strCommand = $"{commad};";
 
-                 }
 
-                 return _moduleGalilTcpDeviceDictionary[str].SetGalilCommand(strCommand);
 
-             }
 
-             else
 
-             {
 
-                 LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
 
-                 return false;
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 设置
 
-         /// </summary>
 
-         /// <param name="module"></param>
 
-         /// <param name="name"></param>
 
-         /// <param name="variable"></param>
 
-         /// <param name="value"></param>
 
-         /// <returns></returns>
 
-         public bool SetAxisCommand(string module, string name,string commad, object value) 
 
-         {
 
-             string str = $"{module}.{name}";
 
-             if (!_moduleGalilTcpDeviceDictionary.ContainsKey(str))
 
-             {
 
-                 LOG.WriteLog(eEvent.ERR_GALIL, module, $"{module} does not have tcp device");
 
-                 return false;
 
-             }
 
-             if(_moduleNameIndexDictionary.ContainsKey(str))
 
-             {
 
-                 int index = _moduleNameIndexDictionary[str];
 
-                 if (index >= _axisArray.Length)
 
-                 {
 
-                     LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
 
-                     return false;
 
-                 }
 
-                 string strCommand = "";
 
-                 string axis = _axisArray[index].ToString();
 
-                 if (value != null)
 
-                 {
 
-                     strCommand = $"{commad}{axis}={value};";
 
-                 }
 
-                 else
 
-                 {
 
-                     strCommand = $"{commad}{axis};";
 
-                 }
 
-                 return _moduleGalilTcpDeviceDictionary[str].SetGalilCommand(strCommand);
 
-             }
 
-             else
 
-             {
 
-                 LOG.WriteLog(eEvent.ERR_GALIL, module, $"{str} is not matched with index");
 
-                 return false;
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 获取Galil配置对象
 
-         /// </summary>
 
-         /// <param name="module"></param>
 
-         /// <param name="name"></param>
 
-         /// <returns></returns>
 
-         public GalilAxisConfig GetGalilAxisConfig(string module,string name)
 
-         {
 
-             string str = $"{module}.{name}";
 
-             if (_moduleNameAxisConfigDictionary.ContainsKey(str))
 
-             {
 
-                 return _moduleNameAxisConfigDictionary[str];
 
-             }
 
-             else
 
-             {
 
-                 return null;
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |