/// <summary>
        /// 添加仪器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void NewBtn_ClickAsync(object sender, RoutedEventArgs e)
        {
            //创建创建仪器对话框
            var dialog3 = new InputDialog3();

            dialog3.Model().Label1           = "Instrument Name";
            dialog3.Model().Label3           = "Communication Type";
            dialog3.Model().ItemsSource      = new ObservableCollection <ComboBoxItemModel>(SystemSettings.Comm1);
            dialog3.Model().Input1           = "";
            dialog3.Model().WarningMsg1      = "Input not available!";
            dialog3.Model().Input1Validation = (
                s =>
            {
                return(!SystemSettings.InstrumentsList.Any(t => t.Name.ToUpper() == s.ToUpper()));
            });
Input:
            var result = await DialogHost.Show(dialog3, "RootDialog", new DialogClosingEventHandler((s, t) => { }));

            if ((bool)result)
            {
                var typeStr = ((ComboBoxItemModel)dialog3.Model().Input3).Name;
                var type    = (CommunicationType)Enum.Parse(typeof(CommunicationType), typeStr);

                //仪器通讯类型错误则返回重新选择类型
                if (type == CommunicationType.None)
                {
                    await this.MsgBox("Please select the correct communication type.");

                    goto Input;
                }

                var instr = new InstrumentModel()
                {
                    Name               = dialog3.Model().Input1,
                    Description        = dialog3.Model().Input1,
                    CommnunicationType = type,
                    Config             = CommunicationBase.CreateCfg(type),
                    CommReference      = CommunicationBase.CreateCommunication(type)
                };
                //添加仪器至全局变量
                SystemSettings.InstrumentsList.Add(instr);
                //选中当前添加的仪器
                Instruments.SelectedItem = instr;
            }
        }
Exemple #2
0
        /// <summary>
        /// 从仪器配置文件中读取所有仪器配置
        /// </summary>
        private void LoadInstruments()
        {
            //SerialPort,
            //GPIB,
            //TCP,
            //USB,
            //UDP,
            object obj = null;

            InIHelper.FileName = SystemSettings.InstrumentIniFile;
            //Json转换条件,忽略空值属性
            var jsSetting = new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            };

            //读取所有设备字段信息-设备名称
            foreach (var item in InIHelper.ReadSections())
            {
                var cfg     = InIHelper.Read(item, "Config", null); //读取配置
                var typeStr = InIHelper.Read(item, "Type", null);   //读取通信方式
                if (Enum.IsDefined(typeof(CommunicationType), typeStr))
                {
                    var type = (CommunicationType)Enum.Parse(typeof(CommunicationType), typeStr, true);//格式化通讯方式为enum
                    //根据不同通讯方式创建不同仪器实例信息
                    switch (type)
                    {
                    case CommunicationType.UDP:
                        obj = JsonConvert.DeserializeObject <UdpCfgModel>(cfg, jsSetting);
                        break;

                    case CommunicationType.TCP:
                        obj = JsonConvert.DeserializeObject <TcpIpCfgModel>(cfg, jsSetting);
                        break;

                    case CommunicationType.SerialPort:
                        obj = JsonConvert.DeserializeObject <SerialPortCfgModel>(cfg, jsSetting);
                        break;

                    case CommunicationType.GPIB:
                        obj = JsonConvert.DeserializeObject <GPIBCfgModel>(cfg, jsSetting);
                        break;

                    case CommunicationType.USB:
                        obj = JsonConvert.DeserializeObject <USBCfgModel>(cfg, jsSetting);
                        break;

                    default:
                        break;
                    }
                    var instr = new InstrumentModel()
                    {
                        Name               = item,
                        Config             = obj,
                        CommnunicationType = type,
                        CommReference      = CommunicationBase.CreateCommunication(type)
                    };
                    //添加至系统全局变量中
                    SystemSettings.InstrumentsList.Add(instr);
                }
            }
        }