Exemple #1
0
 public override bool Init()
 {
     try
     {
         HardwareCfgManager hardwareCfg = ConfigMgr.HardwareCfgMgr;
         if (Config.ConnectMode.ToUpper() == @"COMPORT")
         {
             foreach (var it in hardwareCfg.Comports)
             {
                 if (it.PortName == Config.PortName)
                 {
                     comportCfg = it;
                     break;
                 }
             }
             comPort = new System.IO.Ports.SerialPort();
             if (comPort != null && comportCfg != null)
             {
                 GetPortProfileData(comportCfg);
                 comPort.PortName     = comportData.Port;
                 comPort.BaudRate     = comportData.BaudRate;
                 comPort.Parity       = comportData.parity;
                 comPort.StopBits     = comportData.stopbits;
                 comPort.DataBits     = comportData.DataBits;
                 comPort.ReadTimeout  = comportData.Timeout;
                 comPort.WriteTimeout = comportData.Timeout;
                 if (comPort.IsOpen)
                 {
                     comPort.Close();
                 }
                 comPort.Open();
                 return(comPort.IsOpen);
             }
             return(false);
         }
         else if (Config.ConnectMode.ToUpper() == @"ETHERNET")
         {
             foreach (var it in hardwareCfg.EtherNets)
             {
                 if (it.PortName == Config.PortName)
                 {
                     etherNetCfg = it;
                 }
             }
             if (etherNetCfg == null)
             {
                 return(false);
             }
         }
         return(false);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
        //public static
        public void LoadConfig(out List <string> errList)
        {
            #region >>>>Hardware init
            errList = new List <string>();
            try
            {
                var json_string = File.ReadAllText(File_HardwareCfg);
                HardwareCfgMgr = JsonConvert.DeserializeObject <HardwareCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                errList.Add($"Unable to load config file { File_HardwareCfg}, { ex.Message}");
            }

            InstrumentBase instrumentBase = null;


            Type hardWareMgrType = HardwareCfgMgr.GetType();
            foreach (var it in hardWareMgrType.GetProperties())
            {
                switch (it.Name)
                {
                case "Instruments":
                    var instrumentCfgs = it.GetValue(HardwareCfgMgr) as InstrumentCfg[];
                    if (instrumentCfgs == null)
                    {
                        break;
                    }
                    foreach (var instrumentCfg in instrumentCfgs)
                    {
                        if (instrumentCfg.Enabled)
                        {
                            instrumentBase = hardWareMgrType.Assembly.CreateInstance("JPT_TosaTest.Instruments." + instrumentCfg.InstrumentName.Substring(0, instrumentCfg.InstrumentName.IndexOf("[")), true, BindingFlags.CreateInstance, null, null, null, null) as InstrumentBase;
                            if (instrumentBase != null)
                            {
                                if (instrumentBase.Init())
                                {
                                }
                            }
                        }
                    }
                    break;

                case "IOCards":
                case "MotonCards":
                case "Cameras":
                case "Lights":
                case "Comports":
                case "Ethernets":
                case "Gpibs":
                case "Visas":
                    break;

                default:
                    errList.Add("Invalid hardware type!");
                    break;
                }
            }

            #endregion

            #region >>>>Software init
            try
            {
                var json_string = File.ReadAllText(File_SoftwareCfg);
                SoftwareCfgMgr = JsonConvert.DeserializeObject <SoftwareCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                errList.Add(String.Format("Unable to load config file {0}, {1}", File_SoftwareCfg, ex.Message));
            }

            Type             tStationCfg  = SoftwareCfgMgr.GetType();
            PropertyInfo[]   pis          = tStationCfg.GetProperties();
            WorkFlowConfig[] WorkFlowCfgs = null;
            WorkFlowBase     workFlowBase = null;
            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name == "WorkFlowConfigs")
                {
                    WorkFlowCfgs = pi.GetValue(SoftwareCfgMgr) as WorkFlowConfig[];
                    foreach (var it in WorkFlowCfgs)
                    {
                        if (it.Enable)
                        {
                            workFlowBase = tStationCfg.Assembly.CreateInstance("GPAP.WorkFlow." + it.Name, true, BindingFlags.CreateInstance, null, new object[] { it }, null, null) as WorkFlowBase;
                            if (workFlowBase == null)
                            {
                                errList.Add($"Station: {it.Name} Create instance failed!");
                            }
                            else
                            {
                                WorkFlowMgr.Instance.AddStation(it.Name, workFlowBase);
                            }
                        }
                    }
                }
            }
            #endregion
        }
        //public static
        public void LoadConfig()
        {
            #region >>>>Hardware init
            try
            {
                var json_string = File.ReadAllText(File_HardwareCfg);
                HardwareCfgMgr = JsonConvert.DeserializeObject <HardwareCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send <string>(String.Format("Unable to load config file {0}, {1}", File_HardwareCfg, ex.Message), "ShowError");
                throw new Exception(ex.Message);
            }
            InstrumentBase             inst     = null;
            HardwareCfgLevelManager1[] instCfgs = null;
            string         strClassName         = "";
            Type           t             = HardwareCfgMgr.GetType();
            PropertyInfo[] PropertyInfos = t.GetProperties();
            for (int i = 0; i < PropertyInfos.Length; i++)
            {
                if (PropertyInfos[i].Name.ToUpper().Contains("COMPORT") || PropertyInfos[i].Name.ToUpper().Contains("ETHERNET") ||
                    PropertyInfos[i].Name.ToUpper().Contains("GPIB") || PropertyInfos[i].Name.ToUpper().Contains("NIVISA") ||
                    PropertyInfos[i].Name.ToUpper().Contains("CAMERACFG"))
                {
                    continue;
                }
                PropertyInfo pi = PropertyInfos[i];
                instCfgs     = pi.GetValue(HardwareCfgMgr) as HardwareCfgLevelManager1[];
                strClassName = pi.Name.Substring(0, pi.Name.Length - 1);
                foreach (var it in instCfgs)
                {
                    if (!it.Enabled)
                    {
                        continue;
                    }
                    inst = t.Assembly.CreateInstance("CPAS.Instrument." + strClassName, true, BindingFlags.CreateInstance, null, new object[] { it }, null, null) as InstrumentBase;
                    if (inst != null && it.Enabled)
                    {
                        if (inst.Init())
                        {
                            InstrumentMgr.Instance.AddInstrument(it.InstrumentName, inst);
                        }
                        else
                        {
                            Messenger.Default.Send <string>(string.Format("Instrument :{0} init failed", it.InstrumentName), "ShowError");
                        }
                    }
                }
            }
            #endregion

            #region >>>>Software init
            try
            {
                var json_string = File.ReadAllText(File_SoftwareCfg);
                SoftwareCfgMgr = JsonConvert.DeserializeObject <SoftwareCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send <string>(String.Format("Unable to load config file {0}, {1}", File_SoftwareCfg, ex.Message), "ShowError");
                throw new Exception(ex.Message);
            }

            Type             tStationCfg  = SoftwareCfgMgr.GetType();
            PropertyInfo[]   pis          = tStationCfg.GetProperties();
            WorkFlowConfig[] WorkFlowCfgs = null;
            WorkFlowBase     workFlowBase = null;
            foreach (PropertyInfo pi in pis)
            {
                WorkFlowCfgs = pi.GetValue(SoftwareCfgMgr) as SoftwareManager.WorkFlowConfig[];
                foreach (var it in WorkFlowCfgs)
                {
                    if (it.Enable)
                    {
                        workFlowBase = tStationCfg.Assembly.CreateInstance("CPAS.WorkFlow." + it.Name, true, BindingFlags.CreateInstance, null, new object[] { it }, null, null) as WorkFlowBase;
                        WorkFlowMgr.Instance.AddStation(it.Name, workFlowBase);
                    }
                }
            }
            #endregion

            #region >>>>SystemCfg
            try
            {
                var json_string = File.ReadAllText(File_SystemParaCfg);
                SystemParaCfgMgr = JsonConvert.DeserializeObject <SystemParaCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send <string>(String.Format("Unable to load config file {0}, {1}", File_SystemParaCfg, ex.Message), "ShowError");
                throw new Exception(ex.Message);
            }
            #endregion
        }
Exemple #4
0
        //public static
        public void LoadConfig(out List <string> errList)
        {
            #region >>>>Hardware init
            errList = new List <string>();
            try
            {
                var json_string = File.ReadAllText(File_HardwareCfg);
                HardwareCfgMgr = JsonConvert.DeserializeObject <HardwareCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                errList.Add($"Unable to load config file { File_HardwareCfg}, { ex.Message}");
            }
            IMotion        motionBase     = null;
            IIO            ioBase         = null;
            InstrumentBase instrumentBase = null;
            LightBase      lightBase      = null;

            Type hardWareMgrType = HardwareCfgMgr.GetType();

            //先初始化通信端口
            foreach (var it in hardWareMgrType.GetProperties())
            {
                switch (it.Name)
                {
                case "Comports":
                    foreach (var comportCfg in HardwareCfgMgr.Comports)
                    {
                        CommunicationPortBase port = new Comport(comportCfg);
                        CommunicationMgr.Instance.AddCommunicationPort(comportCfg.PortName, port);
                    }
                    break;

                case "Ethernets":
                case "Gpibs":
                case "Visas":
                    break;

                default:
                    break;
                }
            }

            foreach (var it in hardWareMgrType.GetProperties())
            {
                switch (it.Name)
                {
                case "MotionCards":
                    var motionCfgs = it.GetValue(HardwareCfgMgr) as MotionCardCfg[];
                    if (motionCfgs == null)
                    {
                        break;
                    }
                    foreach (var motionCfg in motionCfgs)
                    {
                        if (motionCfg.Enabled)
                        {
                            motionBase = hardWareMgrType.Assembly.CreateInstance("JPT_TosaTest.MotionCards." + motionCfg.Name.Substring(0, motionCfg.Name.IndexOf("[")), true, BindingFlags.CreateInstance, null, /*new object[] { motionCfg }*/ null, null, null) as IMotion;
                            if (motionBase != null)
                            {
                                if (motionCfg.ConnectMode.ToLower() != "none")
                                {
                                    var p        = hardWareMgrType.GetProperty($"{motionCfg.ConnectMode}s");
                                    var portCfgs = p.GetValue(HardwareCfgMgr) as ICommunicationPortCfg[];
                                    var ports    = from portCfg in portCfgs where portCfg.PortName == motionCfg.PortName select portCfg;
                                    if (ports != null && ports.Count() > 0)
                                    {
                                        if (motionBase.Init(motionCfg, ports.ElementAt(0)))
                                        {
                                            //设置单位,轴类型, 软限位等
                                            for (int i = 0; i < motionBase.MAX_AXIS - motionBase.MIN_AXIS + 1; i++)
                                            {
                                                var settings = HardwareCfgMgr.AxisSettings.Where(a => a.AxisNo == i + motionBase.MIN_AXIS);
                                                try
                                                {
                                                    motionBase.SetAxisPara(i, settings == null ? null : settings.First());
                                                }
                                                catch (Exception ex)
                                                {
                                                    errList.Add($"{ex.Message}");
                                                }
                                            }
                                            MotionMgr.Instance.AddMotionCard(motionCfg.Name, motionBase);
                                        }
                                        else
                                        {
                                            errList.Add($"{motionCfg.Name} init failed");
                                        }
                                    }
                                    else
                                    {
                                        errList.Add($"{motionCfg.Name} init failed");
                                    }
                                }
                                else      //无需选择通信端口
                                {
                                    if (motionBase.Init(motionCfg, null))
                                    {
                                        //设置单位,轴类型, 软限位等
                                        for (int i = 0; i < motionBase.MAX_AXIS - motionBase.MIN_AXIS; i++)
                                        {
                                            var settings = HardwareCfgMgr.AxisSettings.Where(a => a.AxisNo == i + motionBase.MIN_AXIS);
                                            try
                                            {
                                                motionBase.SetAxisPara(i, settings == null ? null : settings.First());
                                            }
                                            catch (Exception ex)
                                            {
                                                errList.Add($"{ex.Message}");
                                            }
                                        }
                                        MotionMgr.Instance.AddMotionCard(motionCfg.Name, motionBase);
                                    }
                                    else
                                    {
                                        errList.Add($"{motionCfg.Name} init failed");
                                    }
                                }
                            }
                            else
                            {
                                errList.Add($"{motionCfg.Name} Create instanse failed");
                            }
                        }
                    }
                    break;

                case "IOCards":
                    var ioCfgs = it.GetValue(HardwareCfgMgr) as IOCardCfg[];
                    if (ioCfgs == null)
                    {
                        break;
                    }
                    foreach (var ioCfg in ioCfgs)
                    {
                        if (ioCfg.Enabled)
                        {
                            ioBase = hardWareMgrType.Assembly.CreateInstance("JPT_TosaTest.IOCards." + ioCfg.Name.Substring(0, ioCfg.Name.IndexOf("[")), true, BindingFlags.CreateInstance, null, null, null, null) as IIO;
                            if (ioBase != null)
                            {
                                ioBase.ioCfg = ioCfg;
                                if (ioCfg.ConnectMode.ToLower() != "none")      //没有屏蔽端口
                                {
                                    var p        = hardWareMgrType.GetProperty($"{ioCfg.ConnectMode}s");
                                    var portCfgs = p.GetValue(HardwareCfgMgr) as ICommunicationPortCfg[];
                                    var ports    = from portCfg in portCfgs where portCfg.PortName == ioCfg.PortName select portCfg;
                                    if (ports != null && ports.Count() > 0)
                                    {
                                        if (ioBase.Init(ioCfg, ports.ElementAt(0)))
                                        {
                                            IOCardMgr.Instance.AddIOCard(ioCfg.Name, ioBase);
                                        }
                                        else
                                        {
                                            errList.Add($"{ioCfg.Name} init failed");
                                        }
                                    }
                                    else
                                    {
                                        errList.Add($"{ioCfg.Name} init failed");
                                    }
                                }
                                else      //无需选择通信端口
                                {
                                    if (ioBase.Init(ioCfg, null))
                                    {
                                        IOCardMgr.Instance.AddIOCard(ioCfg.Name, ioBase);
                                    }
                                    else
                                    {
                                        errList.Add($"{ioCfg.Name} init failed");
                                    }
                                }
                            }
                            else
                            {
                                errList.Add($"{ioCfg.Name} Create instanse failed");
                            }
                        }
                    }
                    break;

                case "Instruments":
                    var instrumentCfgs = it.GetValue(HardwareCfgMgr) as InstrumentCfg[];
                    if (instrumentCfgs == null)
                    {
                        break;
                    }
                    foreach (var instrumentCfg in instrumentCfgs)
                    {
                        if (instrumentCfg.Enabled)
                        {
                            instrumentBase = hardWareMgrType.Assembly.CreateInstance("JPT_TosaTest.Instruments." + instrumentCfg.InstrumentName.Substring(0, instrumentCfg.InstrumentName.IndexOf("[")), true, BindingFlags.CreateInstance, null, null, null, null) as InstrumentBase;
                            if (instrumentBase != null)
                            {
                                if (instrumentBase.Init())
                                {
                                }
                            }
                        }
                    }
                    break;

                case "Cameras":
                    var cameraCfgs = it.GetValue(HardwareCfgMgr) as CameraCfg[];
                    break;

                case "Lights":
                    var lightCfgs = it.GetValue(HardwareCfgMgr) as LightCfg[];
                    foreach (var lightCfg in lightCfgs)
                    {
                        if (lightCfg.Enabled)
                        {
                            lightBase = hardWareMgrType.Assembly.CreateInstance("JPT_TosaTest.Vision.Light." + lightCfg.Name.Substring(0, lightCfg.Name.IndexOf("[")), true, BindingFlags.CreateInstance, null, null, null, null) as LightBase;
                            if (lightBase != null)
                            {
                                if (lightCfg.ConnectMode.ToLower() != "none")
                                {
                                    var p        = hardWareMgrType.GetProperty($"{lightCfg.ConnectMode}s");
                                    var portCfgs = p.GetValue(HardwareCfgMgr) as ICommunicationPortCfg[];
                                    var ports    = from portCfg in portCfgs where portCfg.PortName == lightCfg.PortName select portCfg;
                                    if (ports != null && ports.Count() > 0)
                                    {
                                        if (lightBase.Init(lightCfg, ports.ElementAt(0)))     //如果不需要初始化就直接加入字典
                                        {
                                            LigtMgr.Instance.AddLight(lightCfg.Name, lightBase);
                                        }
                                        else
                                        {
                                            errList.Add($"{lightCfg.Name} init failed");
                                        }
                                    }
                                    else
                                    {
                                        errList.Add($"{lightCfg.Name} init failed");
                                    }
                                }
                                else     //无需选择通信端口
                                {
                                    if (lightBase.Init(lightCfg, null))
                                    {
                                        LigtMgr.Instance.AddLight(lightCfg.Name, lightBase);
                                    }
                                    else
                                    {
                                        errList.Add($"{lightCfg.Name} init failed");
                                    }
                                }
                            }
                            else
                            {
                                errList.Add($"{lightCfg.Name} Create instanse failed");
                            }
                        }
                    }
                    break;

                default:
                    break;
                }
            }

            #endregion

            #region >>>>Software init
            try
            {
                var json_string = File.ReadAllText(File_SoftwareCfg);
                SoftwareCfgMgr = JsonConvert.DeserializeObject <SoftwareCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                errList.Add(String.Format("Unable to load config file {0}, {1}", File_SoftwareCfg, ex.Message));
            }

            Type             tStationCfg  = SoftwareCfgMgr.GetType();
            PropertyInfo[]   pis          = tStationCfg.GetProperties();
            WorkFlowConfig[] WorkFlowCfgs = null;
            WorkFlowBase     workFlowBase = null;
            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name == "WorkFlowConfigs")
                {
                    WorkFlowCfgs = pi.GetValue(SoftwareCfgMgr) as SoftwareManager.WorkFlowConfig[];
                    foreach (var it in WorkFlowCfgs)
                    {
                        if (it.Enable)
                        {
                            workFlowBase = tStationCfg.Assembly.CreateInstance("JPT_TosaTest.WorkFlow." + it.Name, true, BindingFlags.CreateInstance, null, new object[] { it }, null, null) as WorkFlowBase;
                            if (workFlowBase == null)
                            {
                                errList.Add($"Station: {it.Name} Create instance failed!");
                            }
                            else
                            {
                                WorkFlowMgr.Instance.AddStation(it.Name, workFlowBase);
                            }
                        }
                    }
                }
            }
            #endregion

            #region >>>>SystemCfg
            try
            {
                var json_string = File.ReadAllText(File_SystemParaCfg);
                SystemParaCfgMgr = JsonConvert.DeserializeObject <SystemParaCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                errList.Add(String.Format("Unable to load config file {0}, {1}", File_SystemParaCfg, ex.Message));
            }
            #endregion

            #region >>>> UserCfg init
            try
            {
                var json_string = File.ReadAllText(File_UserCfg);
                UserCfgMgr = JsonConvert.DeserializeObject <UserCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                errList.Add(String.Format("Unable to load config file {0}, {1}", File_UserCfg, ex.Message));
            }
            #endregion

            #region >>>>ProcessPara
            //从文件中读取参数
            try
            {
                var json_string = File.ReadAllText(File_ProcessPara);
                ProcessDataMgr = JsonConvert.DeserializeObject <ProcessParaMgr>(json_string);
            }
            catch (Exception ex)
            {
                errList.Add(String.Format("Unable to load config file {0}, {1}", File_UserCfg, ex.Message));
            }
            #endregion
        }
Exemple #5
0
 public override bool Init()
 {
     try
     {
         HardwareCfgManager hardwareCfg = ConfigMgr.HardwareCfgMgr;
         if (Config.ConnectMode.ToUpper() == @"COMPORT")
         {
             foreach (var it in hardwareCfg.Comports)
             {
                 if (it.PortName == Config.PortName)
                 {
                     comportCfg = it;
                 }
             }
             comPort = new System.IO.Ports.SerialPort();
             if (comPort != null && comportCfg != null)
             {
                 GetPortProfileData(comportCfg);
                 comPort.PortName     = comportData.Port;
                 comPort.BaudRate     = comportData.BaudRate;
                 comPort.Parity       = comportData.parity;
                 comPort.StopBits     = comportData.stopbits;
                 comPort.DataBits     = comportData.DataBits;
                 comPort.ReadTimeout  = comportData.Timeout;
                 comPort.WriteTimeout = comportData.Timeout;
                 if (comPort.IsOpen)
                 {
                     comPort.Close();
                 }
                 comPort.Open();
                 return(comPort.IsOpen);
             }
             return(false);
         }
         else if (Config.ConnectMode.ToUpper() == @"NIVISA")
         {
             HandleRef Instrument_Handle = new HandleRef();
             TLPM      searchDevice      = new TLPM(Instrument_Handle.Handle);
             uint      count             = 0;
             int       pInvokeResult     = searchDevice.findRsrc(out count);
             if (count == 0)
             {
                 searchDevice.Dispose();
                 return(false);
             }
             foreach (var it in hardwareCfg.NIVisas)
             {
                 for (uint i = 0; i < count; i++)
                 {
                     StringBuilder descr = new StringBuilder(1024);
                     searchDevice.getRsrcName(i, descr);
                     if (descr.ToString().Contains(it.KeyWord1))
                     {
                         tlpm = new TLPM(descr.ToString(), false, false);
                         return(tlpm != null);
                     }
                 }
             }
         }
         return(false);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Exemple #6
0
        public void LoadConfig()
        {
            #region >>>>Hardware init
            try
            {
                var json_string = File.ReadAllText(File_HardwareCfg);
                HardwareCfgMgr = JsonConvert.DeserializeObject <HardwareCfgManager>(json_string);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send <string>($"Unable to load config file { File_HardwareCfg }:{ ex.Message}", "ShowError");
            }
            InstrumentBase             inst     = null;
            HardwareCfgLevelManager1[] instCfgs = null;

            string         strClassName  = "";
            Type           t             = HardwareCfgMgr.GetType();
            PropertyInfo[] PropertyInfos = t.GetProperties();
            for (int i = 0; i < PropertyInfos.Length; i++)
            {
                if (PropertyInfos[i].Name.ToUpper().Contains("COMPORT") || PropertyInfos[i].Name.ToUpper().Contains("ETHERNET") ||
                    PropertyInfos[i].Name.ToUpper().Contains("GPIB") || PropertyInfos[i].Name.ToUpper().Contains("NIVISA") ||
                    PropertyInfos[i].Name.ToUpper().Contains("CAMERACFG"))
                {
                    continue;
                }
                PropertyInfo pi = PropertyInfos[i];
                instCfgs     = pi.GetValue(HardwareCfgMgr) as HardwareCfgLevelManager1[];
                strClassName = pi.Name.Substring(0, pi.Name.Length - 1);

                foreach (var it in instCfgs)
                {
                    if (!it.Enabled)
                    {
                        continue;
                    }
                    inst = t.Assembly.CreateInstance("RFTestAUX.Instrument." + strClassName, true, BindingFlags.CreateInstance, null, new object[] { it }, null, null) as InstrumentBase;
                    if (inst != null && it.Enabled)
                    {
                        if (inst.Init())
                        {
                            InstrumentMgr.Instance.AddInstrument(it.InstrumentName, inst);
                        }
                        else
                        {
                            Messenger.Default.Send <string>($"{it.InstrumentName} init Error", "ShowError");
                        }
                    }
                }
            }
            //if (sbError.ToString().Length > 5)
            //    throw new Exception($"Instrument :{sbError.ToString()} init failed");
            #endregion

            #region >>>> Software Init
            try
            {
                var json_string = File.ReadAllText(File_ParaCfg);
                ParaMgr = JsonConvert.DeserializeObject <ParaManager>(json_string);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send <string>($"Unable to load config file { File_ParaCfg }:{ ex.Message}", "ShowError");
            }
            #endregion
        }
Exemple #7
0
 public override bool Init()
 {
     try
     {
         HardwareCfgManager hardwareCfg = ConfigMgr.HardwareCfgMgr;
         if (Config.ConnectMode.ToUpper() == @"COMPORT")
         {
             foreach (var it in hardwareCfg.Comports)
             {
                 if (it.PortName == Config.PortName)
                 {
                     comportCfg = it;
                 }
             }
             comPort = new System.IO.Ports.SerialPort();
             if (comPort != null && comportCfg != null)
             {
                 GetPortProfileData(comportCfg);
                 comPort.PortName     = comportData.Port;
                 comPort.BaudRate     = comportData.BaudRate;
                 comPort.Parity       = comportData.parity;
                 comPort.StopBits     = comportData.stopbits;
                 comPort.DataBits     = comportData.DataBits;
                 comPort.ReadTimeout  = comportData.Timeout;
                 comPort.WriteTimeout = comportData.Timeout;
                 if (comPort.IsOpen)
                 {
                     comPort.Close();
                 }
                 comPort.Open();
                 return(comPort.IsOpen);
             }
             return(false);
         }
         else if (Config.ConnectMode.ToUpper() == @"NIVISA")
         {
             foreach (var it in hardwareCfg.NIVisas)
             {
                 if (it.PortName == Config.PortName)
                 {
                     nivisaCfg = it;
                 }
             }
             if (nivisaCfg != null)
             {
                 string[] resources = ResourceManager.GetLocalManager().FindResources(nivisaCfg.KeyWord1);
                 foreach (var res in resources)
                 {
                     if (res.Contains(nivisaCfg.KeyWord2))
                     {
                         session = ResourceManager.GetLocalManager().Open(resources[0].ToString()) as MessageBasedSession;
                     }
                 }
                 string str = session.Query("READ?");
                 return(session != null);
             }
         }
         return(false);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }