123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using NationalInstruments.Visa;
- using Ivi.Visa;
- using Uestc.Auto6.Dso.Hardware.Calibration.Data.Base;
- using CalibrationData = Uestc.Auto6.Dso.Hardware.Calibration.Data.Base;
- namespace Uestc.Auto6.Dso.Hardware.Calibration.Tool
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- toolStripMenuItemLoadRemoteCaliFileData.Click += ToolStripMenuItemLoadRemoteCaliFileData_Click;
- toolStripMenuItemSaveCaliData2LocalFile.Click += ToolStripMenuItemSaveCaliData2LocalFile_Click;
- toolStripMenuItemLoadRemoteUsingData.Click += ToolStripMenuItemLoadRemoteUsingData_Click;
- toolStripMenuItemLoadLocalCaliFileDataAndUsing.Click += ToolStripMenuItemLoadLocalCaliFileDataAndUsing_Click;
- toolStripMenuItemLoadLocalCaliFileDataAndSave2RemoteFile.Click += ToolStripMenuItemLoadLocalCaliFileDataAndSave2RemoteFile_Click;
- Init();
- }
- private void ToolStripMenuItemLoadLocalCaliFileDataAndSave2RemoteFile_Click(object sender, EventArgs e)
- {
- if (currInstrument == null)
- {
- MessageBox.Show("请先连接仪器!");
- return;
- }
- if (MessageBox.Show("该操作将覆盖远端原有校准数据及保存的数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
- return;
- if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
- return;
- string folder = folderBrowserDialog1.SelectedPath;
- if (folder.Last<char>() != '\\')
- folder += '\\';
- foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
- {
- ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
- if (caliData != null)
- {
- caliData.LoadFromFile(folder);
- InstrumentInteract.CaliData_Send(this.currInstrument, dataType);
- InstrumentInteract.CaliData_SaveToFile(this.currInstrument, dataType);
- InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
- }
- }
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
- {
- if (MainFormTabPage.CaliDataType != CaliDataType.None)
- MainFormTabPage.RefreshData();
- }
- }
- MessageBox.Show("Ok!");
- }
- private void ToolStripMenuItemLoadLocalCaliFileDataAndUsing_Click(object sender, EventArgs e)
- {
- if (currInstrument == null)
- {
- MessageBox.Show("请先连接仪器!");
- return;
- }
- if (MessageBox.Show("该操作将覆盖远端原有校准数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
- return;
- if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
- return;
- string folder = folderBrowserDialog1.SelectedPath;
- if (folder.Last<char>() != '\\')
- folder += '\\';
- foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
- {
- ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
- if (caliData != null)
- {
- caliData.LoadFromFile(folder);
- InstrumentInteract.CaliData_Send(this.currInstrument, dataType);
- InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
- }
- }
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
- {
- if (MainFormTabPage.CaliDataType != CaliDataType.None)
- MainFormTabPage.RefreshData();
- }
- }
- MessageBox.Show("Ok!");
- }
- private void ToolStripMenuItemLoadRemoteUsingData_Click(object sender, EventArgs e)
- {
- if (currInstrument == null)
- {
- MessageBox.Show("请先连接仪器!");
- return;
- }
- if (MessageBox.Show("该操作将覆盖本地校准数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
- return;
- foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
- {
- ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
- if (caliData != null)
- {
- InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
- }
- }
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
- {
- if (MainFormTabPage.CaliDataType != CaliDataType.None)
- MainFormTabPage.RefreshData();
- }
- }
- MessageBox.Show("Ok!");
- }
- private void ToolStripMenuItemSaveCaliData2LocalFile_Click(object sender, EventArgs e)
- {
- if (currInstrument == null)
- {
- MessageBox.Show("请先连接仪器!");
- return;
- }
- if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
- return;
- string folder = folderBrowserDialog1.SelectedPath;
- if (folder.Last<char>() != '\\')
- folder += '\\';
- foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
- {
- ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
- if (caliData != null)
- {
- InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
- caliData.SaveToFile(folder);
- }
- }
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
- {
- if (MainFormTabPage.CaliDataType != CaliDataType.None)
- MainFormTabPage.RefreshData();
- }
- }
- MessageBox.Show("Ok!");
- }
- private void ToolStripMenuItemLoadRemoteCaliFileData_Click(object sender, EventArgs e)
- {
- if (currInstrument == null)
- {
- MessageBox.Show("请先连接仪器!");
- return;
- }
- if (MessageBox.Show("该操作将覆盖本地校准数据、远端运行态校准数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
- return;
- foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
- {
- ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
- if (caliData != null)
- {
- InstrumentInteract.CaliData_LoadFromFile(this.currInstrument, dataType);
- InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
- }
- }
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
- {
- if (MainFormTabPage.CaliDataType != CaliDataType.None)
- MainFormTabPage.RefreshData();
- }
- }
- MessageBox.Show("Ok!");
- }
- private void Init()
- {
- currInstrument = null;
- waveViewer1.CurrInstrument = currInstrument;
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage)
- (tabPage.Controls[0] as IMainFormTabPage).SetInstrumentInteract(currInstrument);
- }
- }
- private IInstrumentSession currInstrument = null;
- private void MainForm_Load(object sender, EventArgs e)
- {
- }
- private void RefreshConstDataFromServer()
- {
- return;
- string scpiCmd = InstrumentInteract.GetCmdStr(ScpiCmd.Factory_SpecailData);
- scpiCmd += " ? "+ "GetComModelConstData";
- currInstrument.WriteString(scpiCmd);
- Thread.Sleep(100);
- string recvStr = currInstrument.ReadString();
- string[] paramList = recvStr.Split('|');
- foreach (string s in paramList)
- {
- string[] perParam = s.Split(':');
- int value = Int32.Parse(perParam[1]);
- if (perParam[0] == "HardwareAttached")
- ServerDomainConstants.HardwareAttached = (value == 1);
- else if (perParam[0] == "ProductType")
- ServerDomainConstants.ProductType = (ComModel.ProductType)value;
- else if (perParam[0] == "AdcBits")
- ServerDomainConstants.AdcBits = value;
- else if (perParam[0] == "AdcNum")
- ServerDomainConstants.AdcNum = value;
- else if (perParam[0] == "MaxAdcRes")
- ServerDomainConstants.MaxAdcRes = value;
- else if (perParam[0] == "AnalogChannelCount")
- ServerDomainConstants.AnalogChannelCount = value;
- }
- }
- private void buttonConnectInstrument_Click(object sender, EventArgs e)
- {
- if (buttonConnectInstrument.Tag.ToString() == "0")
- {
- string message = "";
- if (currInstrument != null)
- currInstrument.Close();
- if (comboBoxVisaResource.Items.Count == 0)
- {
- MessageBox.Show("没有可连接的仪器,请点击[刷新]按钮或启动示波器软件!");
- return;
- }
- if (comboBoxVisaResource.SelectedItem == null)
- {
- MessageBox.Show("没有可连接的仪器,请点击[刷新]按钮或启动示波器软件!");
- return;
- }
- currInstrument = InstrumentSessionEngine.TryGetSession(comboBoxVisaResource.SelectedItem.ToString(), "20", out message);
- //currInstrument = InstrumentSessionEngine.TryGetSession("TCPIP0::192.168.2.110::inst0::INSTR", "20", out message);
- if (currInstrument == null)
- {
- MessageBox.Show("不能连接到指定的仪器,请确认:\r\n1、仪器的地址是否正确?\r\n2、仪器是否打开?", "错误提示");
- return;
- }
- waveViewer1.CurrInstrument = currInstrument;
-
- RefreshConstDataFromServer();
- this.tabPageMatlabSourceCode1.SetInstrument(currInstrument);
- currInstrument.WriteString("*CLS");
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage)
- (tabPage.Controls[0] as IMainFormTabPage).SetInstrumentInteract(currInstrument);
- }
- if (MessageBox.Show("需要从远端装载校准数据吗?","提示",MessageBoxButtons.YesNo)== DialogResult.Yes)
- {
- foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
- {
- ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
- if (caliData != null)
- {
- InstrumentInteract.CaliData_LoadFromFile(this.currInstrument, dataType);
- InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
- }
- }
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
- {
- if (MainFormTabPage.CaliDataType != CaliDataType.None)
- MainFormTabPage.RefreshData();
- }
- }
- }
- buttonConnectInstrument.Text = "断开仪器";
- }
- else
- {
- currInstrument.Close();
- currInstrument = null;
- waveViewer1.CurrInstrument = currInstrument;
- this.tabPageMatlabSourceCode1.SetInstrument(null);
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage)
- (tabPage.Controls[0] as IMainFormTabPage).SetInstrumentInteract(currInstrument);
- }
- buttonConnectInstrument.Text = "连接仪器";
- }
- buttonConnectInstrument.Tag = buttonRefresh.Enabled ? "1" : "0";
- this.buttonRefresh.Enabled = !this.buttonRefresh.Enabled;
- comboBoxVisaResource.Enabled = this.buttonRefresh.Enabled;
- }
- private void buttonWaveRegionCtrl_Click(object sender, EventArgs e)
- {
- splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
- waveViewer1.Run(!splitContainer1.Panel1Collapsed);
- buttonWaveRegionCtrl.Text = splitContainer1.Panel1Collapsed ? "显示波形区" : "隐藏波形区";
- buttonCtrlRegionCtrl.Text = splitContainer1.Panel2Collapsed ? "显示控制区" : "隐藏控制区";
- }
- private void buttonCtrlRegionCtrl_Click(object sender, EventArgs e)
- {
- splitContainer1.Panel2Collapsed = !splitContainer1.Panel2Collapsed;
- waveViewer1.Run(!splitContainer1.Panel1Collapsed);
- buttonWaveRegionCtrl.Text = splitContainer1.Panel1Collapsed ? "显示波形区" : "隐藏波形区";
- buttonCtrlRegionCtrl.Text = splitContainer1.Panel2Collapsed ? "显示控制区" : "隐藏控制区";
- }
- private void buttonLoadAllCalibrationData_Click(object sender, EventArgs e)
- {
- if (currInstrument == null)
- {
- MessageBox.Show("请先连接仪器!");
- return;
- }
- foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
- {
- ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
- if (caliData != null)
- {
- InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
- }
- }
- foreach (TabPage tabPage in this.tabControl1.TabPages)
- {
- if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
- {
- if (MainFormTabPage.CaliDataType != CaliDataType.None)
- MainFormTabPage.RefreshData();
- }
- }
- MessageBox.Show("Ok!");
- }
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- waveViewer1.SaveSetting();
- }
- private void buttonGotoExePath_Click(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start("explorer.exe", Path.GetDirectoryName(Application.ExecutablePath));
- }
- private void buttonRefresh_Click(object sender, EventArgs e)
- {
- string filter = "?*";
- comboBoxVisaResource.Items.Clear();
- using (var rm = new ResourceManager())
- {
- try
- {
- IEnumerable<string> resources = rm.Find(filter);
- foreach (string s in resources)
- {
- comboBoxVisaResource.Items.Add(s);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- if (comboBoxVisaResource.Items.Count > 0)
- comboBoxVisaResource.SelectedIndex = 0;
- }
- }
- }
|