//光衰减器初始化 private void btnAttSet_Click(object sender, RoutedEventArgs e) { try { if (cmbAttAdd.SelectedIndex != -1) { if (Hp8156A != null) { Hp8156A.Dispose(); } Hp8156A = new HP8156A(cmbAttAdd.SelectedItem.ToString().Trim()); MessageBox.Show(Hp8156A.GetIdn(), "系统提示"); } else { MessageBox.Show("请选择设备地址", "系统提示"); } } catch (Exception ex) { MessageBox.Show(ex.Message, "系统提示"); } }
static void Dispose() { if (Hp8156 != null) { Hp8156.Dispose(); } if (Keith != null) { Keith.Dispose(); } if (Mp != null) { Mp.Dispose(); } if (Hp8153 != null) { Hp8153.Dispose(); } if (AgE3631 != null) { AgE3631.Dispose(); } }
private void ExecuteInitialize() { StringBuilder info = new StringBuilder(); info.Append("程序正在初始化。。" + DateTime.Now.ToShortTimeString() + "\r\n"); try { //串口初始化 if (!string.IsNullOrEmpty(SelectedCom)) { info.Append("串口初始化中。。。"); if (Port.IsOpen == true) { Port.Close(); } Port.PortName = SelectedCom.Trim(); Port.Parity = 0; Port.BaudRate = 19200; Port.StopBits = StopBits.Two; Port.DataBits = 8; Port.ReadTimeout = 100; Port.WriteTimeout = 100; Port.Open(); string msg = TranBase.IsPortReady(Port, SerBuf) + DateTime.Now.ToShortTimeString(); if (msg == null) { info.Append("I2C通信失败!"); DisplayInfo = info.ToString(); } else { IsPortReady = true; info.Append("初始化成功 \r\n"); info.Append("msg"); DisplayInfo = info.ToString(); } } else { info.Append("未选择串口号!\r\n"); info.Append("串口初始化失败!\r\n"); DisplayInfo = info.ToString(); } //设备初始化 foreach (var item in Devices) { if (item.IsSelected == true) { if (item.Address != 0) { string address = item.Address.ToString(); int index = Array.IndexOf(Devices, item); switch (index) { case 0: if (Ag34401A != null) { Ag34401A.Dispose(); } info.Append("Aglient34401A初始化中。。。\r\n"); Ag34401A = new Aglient34401A(address); info.Append(Ag34401A.GetIdn()); if (Ag34401A != null) { info.Append(Ag34401A.DeviceName + "初始化OK \r\n"); } else { info.Append(Ag34401A.DeviceName + "初始化失败 \r\n"); } DisplayInfo = info.ToString(); break; case 1: if (AgE3631A != null) { AgE3631A.Dispose(); } info.Append("AglientE3631A初始化中。。。\r\n"); AgE3631A = new AglientE3631A(address); info.Append(AgE3631A.GetIdn()); if (AgE3631A != null) { AgE3631A.Open(); info.Append(AgE3631A.DeviceName + "初始化OK \r\n"); } else { info.Append(AgE3631A.DeviceName + "初始化失败 \r\n"); } DisplayInfo = info.ToString(); break; case 2: if (Hp8153A != null) { Hp8153A.Dispose(); } info.Append("Hp8153A初始化中。。。\r\n"); Hp8153A = new HP8153A(address); info.Append(Hp8153A.GetIdn()); if (Hp8153A != null) { info.Append(Hp8153A.DeviceName + "初始化OK \r\n"); } else { info.Append(Hp8153A.DeviceName + "初始化失败 \r\n"); } DisplayInfo = info.ToString(); break; case 3: if (Hp8156A != null) { Hp8156A.Dispose(); } info.Append("Hp8156A初始化中。。。\r\n"); Hp8156A = new HP8156A(address); info.Append(Hp8156A.GetIdn()); if (Hp8156A != null) { Hp8156A.Open(); info.Append(Hp8156A.DeviceName + "初始化OK \r\n"); } else { info.Append(Hp8156A.DeviceName + "hp8156A初始化失败\r\n"); } DisplayInfo = info.ToString(); break; case 4: if (Mp2100A != null) { Mp2100A.Dispose(); } info.Append("MP2100A初始化中。。。\r\n"); Mp2100A = new MP2100A(address); info.Append(Mp2100A.GetIdn()); if (Mp2100A != null) { info.Append(Mp2100A.DeviceName + "初始化OK \r\n"); } else { info.Append(Mp2100A.DeviceName + "初始化失败\r\n"); } DisplayInfo = info.ToString(); break; case 5: if (P3202 != null) { P3202.Dispose(); } info.Append("P3202初始化中。。。\r\n"); P3202 = new PST3202(address); info.Append(P3202.GetIdn()); if (P3202 != null) { P3202.Open(); info.Append(P3202.DeviceName + "初始化OK \r\n"); } else { info.Append(P3202.DeviceName + "初始化失败"); } DisplayInfo = info.ToString(); break; case 6: if (Aq6317B != null) { Aq6317B.Dispose(); } info.Append("AQ6317B初始化中。。。\r\n"); Aq6317B = new AQ6317B(address); info.Append(Aq6317B.GetIdn()); if (Aq6317B != null) { info.Append(Aq6317B.DeviceName + "初始化OK \r\n"); } else { info.Append(Aq6317B.DeviceName + "初始化失败"); } DisplayInfo = info.ToString(); break; case 7: if (Keith != null) { Keith.Dispose(); } info.Append("Keithley初始化中。。。\r\n"); Keith = new Keithley(address); info.Append(Keith.GetIdn()); if (Keith != null) { info.Append(Keith.DeviceName + "初始化成功\r\n"); } else { info.Append(Keith.DeviceName + "初始化失败\r\n"); } DisplayInfo = info.ToString(); break; default: throw new Exception("该设备不存在"); } } } } IsReady = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "系统提示"); IsReady = false; } }