//---------------------------------------------------------------------------- /// <summary> /// Читает визитную карточку устройства и выводит её на экран /// </summary> private void GetTypeDevice() { OperationResult result; CallingCard deviceInfo; String _typeOfDevice; // Получаем данные об устройстве. if (_Host == null) { _Host = new Modbus.OSIModel.ApplicationLayer.Master.Device( "Network", _SerialPort); this.ReadCallingCard(out result, out deviceInfo); _Host = null; } else { this.ReadCallingCard(out result, out deviceInfo); } if (result.Result == OPERATION_RESULT.OK) { // Если визитную карточку удалось получить, и виртуальное // устройство не создано, то предлагаем // создать не её основе устройство. В противном случае просто выводим информацию switch ((UInt16)deviceInfo.TypeOfDevice) { case (UInt16)TYPE_NGK_DEVICE.UNKNOWN_DEVICE: { _typeOfDevice = "Неизвестное устройство"; break; } case (UInt16)TYPE_NGK_DEVICE.BI_BATTERY_POWER: { _typeOfDevice = "Устройство БИ(У)-01"; break; } case (UInt16)TYPE_NGK_DEVICE.BI_MAIN_POWERED: { _typeOfDevice = "Устройство БИ(У)-00"; break; } default: { throw new Exception("Данное устройство не поддерживается в данной версии ПО"); } } if (_MeasuringDevice == null) { // Предлагаем создать устройство DialogResult dlgres = MessageBox.Show(this, String.Format("Тип устройства: {0} \n Версия ПО: {1} \n Версия аппаратуры: {2} \n Серийный номер: {3} \n Создать устройство?", _typeOfDevice, deviceInfo.SofwareVersion, deviceInfo.HardwareVersion, deviceInfo.SerialNumber), "Устройство НГК", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dlgres == System.Windows.Forms.DialogResult.Yes) { // Создаём новое устройство IMeasuringDevice device; // Создаём экземпляр switch (deviceInfo.TypeOfDevice) { case TYPE_NGK_DEVICE.BI_BATTERY_POWER: { //MessageBox.Show(this, "Данный тип БИ не поддерживается ПО", // "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); //return; MeasuringDeviceBatteryPower dv = new MeasuringDeviceBatteryPower(); device = (IMeasuringDevice)dv; break; } case TYPE_NGK_DEVICE.BI_MAIN_POWERED: { MeasuringDeviceMainPower dv = new MeasuringDeviceMainPower(); device = (IMeasuringDevice)dv; break; } default: { MessageBox.Show(this, "Данный тип БИ не поддерживается ПО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } CreateDevice(device); } } else { // Только выводим информацию об устройстве MessageBox.Show(this, String.Format("Тип устройства: {0} \n Версия ПО: {1} \n Версия аппаратуры: {2} \n Серийный номер: {3}", _typeOfDevice, deviceInfo.SofwareVersion, deviceInfo.HardwareVersion, deviceInfo.SerialNumber), "Устройство НГК", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { // При выполении запроса возникла ошибка MessageBox.Show(this, String.Format("Неудалось идентифицировать устройство. Ошибка: {0}", result.Message), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; }
//---------------------------------------------------------------------------- /// <summary> /// Создаём виртуальное устройство и функционал для работы с ним /// </summary> /// <param name="device">Устройство КИП, /// если передаётся null - создаётся новое устройство</param> private void CreateDevice(IMeasuringDevice device) { if (device == null) { // Открываем форму для создания нового устройства Forms.FormSelectTypeDevice frm = new Forms.FormSelectTypeDevice(); DialogResult result = frm.ShowDialog(this); if (result != System.Windows.Forms.DialogResult.OK) { return; } else { // Создаём экземпляр switch (frm.TypeDevice) { case TYPE_NGK_DEVICE.BI_BATTERY_POWER: { //MessageBox.Show(this, "Данный тип БИ не поддерживается ПО", // "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); //return; MeasuringDeviceBatteryPower dv = new MeasuringDeviceBatteryPower(); device = (IMeasuringDevice)dv; break; } case TYPE_NGK_DEVICE.BI_MAIN_POWERED: { MeasuringDeviceMainPower dv = new MeasuringDeviceMainPower(); device = (IMeasuringDevice)dv; break; } default: { MessageBox.Show(this, "Данный тип БИ не поддерживается ПО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } } switch (device.GetDeviceType()) { case TYPE_NGK_DEVICE.BI_MAIN_POWERED: { _Host = new Modbus.OSIModel.ApplicationLayer.Master.Device( "NetworkModbus", (IDataLinkLayer)_SerialPort); _MeasuringDevice = device; _MeasuringDevice.PropertyChanged += new PropertyChangedEventHandler(_MeasuringDevice_PropertyChanged); break; } case TYPE_NGK_DEVICE.BI_BATTERY_POWER: { _Host = new Modbus.OSIModel.ApplicationLayer.Master.Device("NetworkModbus", (IDataLinkLayer)_SerialPort); _MeasuringDevice = device; _MeasuringDevice.PropertyChanged += new PropertyChangedEventHandler(_MeasuringDevice_PropertyChanged); break; } default: { throw new Exception("Попытка создания устройства БИ с неопределённым типом"); } } // Настраиваем элементы навигации, управления и меню ToolStripMenuItem itemMenu; ToolStripButton button; // Главное меню: // Меню "Файл" itemMenu = ((ToolStripMenuItem)this.menuStripMain.Items["mnuFile"]); itemMenu.Enabled = true; itemMenu.DropDownItems["mnuFileCreate"].Enabled = false; itemMenu.DropDownItems["mnuFileOpen"].Enabled = true; itemMenu.DropDownItems["mnuFileClose"].Enabled = true; itemMenu.DropDownItems["mnuFileSave"].Enabled = true; itemMenu.DropDownItems["mnuFileSaveAs"].Enabled = true; itemMenu.DropDownItems[MENU.mnuFilePrint].Enabled = true; itemMenu.DropDownItems[MENU.mnuFilePrintPreview].Enabled = true; itemMenu.DropDownItems[MENU.mnuFilePrintSettings].Enabled = true; itemMenu.DropDownItems[MENU.mnuFilePageSettings].Enabled = true; itemMenu.DropDownItems["mnuFileExit"].Enabled = true; // Меню "Устройство КИП" if (_SerialPort.IsOpen()) { itemMenu = ((ToolStripMenuItem)this.menuStripMain.Items["mnuDevice"]); itemMenu.Enabled = true; itemMenu.DropDownItems["mnuDeviceRead"].Enabled = true; itemMenu.DropDownItems["mnuDeviceWrite"].Enabled = true; } else { itemMenu = ((ToolStripMenuItem)this.menuStripMain.Items["mnuDevice"]); itemMenu.Enabled = false; itemMenu.DropDownItems["mnuDeviceRead"].Enabled = true; itemMenu.DropDownItems["mnuDeviceWrite"].Enabled = true; } // Меню "Подключение" //itemMenu = ((ToolStripMenuItem)this.menuStripMain.Items["mnuConnection"]); //itemMenu.Enabled = true; //itemMenu.DropDownItems["mnuConnectionConnect"].Enabled = false; //itemMenu.DropDownItems["mnuConnectionDisconnect"].Enabled = true; // Меню "Помощь" itemMenu = ((ToolStripMenuItem)this.menuStripMain.Items["mnuHelp"]); itemMenu.Enabled = true; itemMenu.DropDownItems["mnuHelpHelp"].Enabled = true; itemMenu.DropDownItems["mnuHelpAbout"].Enabled = true; // Инициализируем панель инструментов button = (ToolStripButton)this.toolStripMain.Items["buttonSaveDevice"]; button.Enabled = true; button = (ToolStripButton)this.toolStripMain.Items["buttonNewDevice"]; button.Enabled = false; button = (ToolStripButton)this.toolStripMain.Items["buttonDeleteDevice"]; button.Enabled = true; button = (ToolStripButton)this.toolStripMain.Items["buttonConnection"]; //button.Enabled = true; //button.ToolTipText = "Поключить"; //button.Image = global::NGK.MeasuringDeviceTech.Properties.Resources.IconDisconnect; //((ToolStripMenuItem)menuStripMain.Items["mnuConnection"]).DropDownItems["mnuConnectionConnect"].Enabled = true; //((ToolStripMenuItem)menuStripMain.Items["mnuConnection"]).DropDownItems["mnuConnectionDisconnect"].Enabled = false; if (_SerialPort.IsOpen()) { button = (ToolStripButton)this.toolStripMain.Items["buttonReadDevice"]; button.Enabled = true; button = (ToolStripButton)this.toolStripMain.Items["buttonWriteDevice"]; button.Enabled = true; button = (ToolStripButton)this.toolStripMain.Items["buttonVerifyInitDevice"]; button.Enabled = true; button = (ToolStripButton)this.toolStripMain.Items["buttonSyncDateTime"]; button.Enabled = true; button = (ToolStripButton)this.toolStripMain.Items[TOOLSTRIPBUTTON.buttonStartMonitor]; button.Enabled = true; button = (ToolStripButton)this.toolStripMain.Items[TOOLSTRIPBUTTON.buttonStopMonitor]; button.Enabled = false; } else { button = (ToolStripButton)this.toolStripMain.Items["buttonReadDevice"]; button.Enabled = false; button = (ToolStripButton)this.toolStripMain.Items["buttonWriteDevice"]; button.Enabled = false; button = (ToolStripButton)this.toolStripMain.Items["buttonVerifyInitDevice"]; button.Enabled = false; button = (ToolStripButton)this.toolStripMain.Items["buttonSyncDateTime"]; button.Enabled = false; button = (ToolStripButton)this.toolStripMain.Items[TOOLSTRIPBUTTON.buttonStartMonitor]; button.Enabled = false; button = (ToolStripButton)this.toolStripMain.Items[TOOLSTRIPBUTTON.buttonStopMonitor]; button.Enabled = false; } // Переходим в дереве на устройство и обновляем данные if (this.treeViewMain.SelectedNode == this.treeViewMain.Nodes["NodeRoot"].Nodes["NodeMeasuringDevice"]) { // Это необходимо делать, т.к. если в данный момен выбран нод "NodeMeasuringDevice" // и создаётся устройство, то отображение данных созданного устройства в propertygrid не происходит!!! this.treeViewMain.SelectedNode = this.treeViewMain.Nodes["NodeRoot"]; this.treeViewMain.SelectedNode = this.treeViewMain.Nodes["NodeRoot"].Nodes["NodeMeasuringDevice"]; } else { this.treeViewMain.SelectedNode = this.treeViewMain.Nodes["NodeRoot"].Nodes["NodeMeasuringDevice"]; } return; }