MainForm.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using NationalInstruments.Visa;
  14. using Ivi.Visa;
  15. using Uestc.Auto6.Dso.Hardware.Calibration.Data.Base;
  16. using CalibrationData = Uestc.Auto6.Dso.Hardware.Calibration.Data.Base;
  17. namespace Uestc.Auto6.Dso.Hardware.Calibration.Tool
  18. {
  19. public partial class MainForm : Form
  20. {
  21. public MainForm()
  22. {
  23. InitializeComponent();
  24. toolStripMenuItemLoadRemoteCaliFileData.Click += ToolStripMenuItemLoadRemoteCaliFileData_Click;
  25. toolStripMenuItemSaveCaliData2LocalFile.Click += ToolStripMenuItemSaveCaliData2LocalFile_Click;
  26. toolStripMenuItemLoadRemoteUsingData.Click += ToolStripMenuItemLoadRemoteUsingData_Click;
  27. toolStripMenuItemLoadLocalCaliFileDataAndUsing.Click += ToolStripMenuItemLoadLocalCaliFileDataAndUsing_Click;
  28. toolStripMenuItemLoadLocalCaliFileDataAndSave2RemoteFile.Click += ToolStripMenuItemLoadLocalCaliFileDataAndSave2RemoteFile_Click;
  29. Init();
  30. }
  31. private void ToolStripMenuItemLoadLocalCaliFileDataAndSave2RemoteFile_Click(object sender, EventArgs e)
  32. {
  33. if (currInstrument == null)
  34. {
  35. MessageBox.Show("请先连接仪器!");
  36. return;
  37. }
  38. if (MessageBox.Show("该操作将覆盖远端原有校准数据及保存的数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
  39. return;
  40. if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
  41. return;
  42. string folder = folderBrowserDialog1.SelectedPath;
  43. if (folder.Last<char>() != '\\')
  44. folder += '\\';
  45. foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
  46. {
  47. ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
  48. if (caliData != null)
  49. {
  50. caliData.LoadFromFile(folder);
  51. InstrumentInteract.CaliData_Send(this.currInstrument, dataType);
  52. InstrumentInteract.CaliData_SaveToFile(this.currInstrument, dataType);
  53. InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
  54. }
  55. }
  56. foreach (TabPage tabPage in this.tabControl1.TabPages)
  57. {
  58. if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
  59. {
  60. if (MainFormTabPage.CaliDataType != CaliDataType.None)
  61. MainFormTabPage.RefreshData();
  62. }
  63. }
  64. MessageBox.Show("Ok!");
  65. }
  66. private void ToolStripMenuItemLoadLocalCaliFileDataAndUsing_Click(object sender, EventArgs e)
  67. {
  68. if (currInstrument == null)
  69. {
  70. MessageBox.Show("请先连接仪器!");
  71. return;
  72. }
  73. if (MessageBox.Show("该操作将覆盖远端原有校准数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
  74. return;
  75. if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
  76. return;
  77. string folder = folderBrowserDialog1.SelectedPath;
  78. if (folder.Last<char>() != '\\')
  79. folder += '\\';
  80. foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
  81. {
  82. ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
  83. if (caliData != null)
  84. {
  85. caliData.LoadFromFile(folder);
  86. InstrumentInteract.CaliData_Send(this.currInstrument, dataType);
  87. InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
  88. }
  89. }
  90. foreach (TabPage tabPage in this.tabControl1.TabPages)
  91. {
  92. if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
  93. {
  94. if (MainFormTabPage.CaliDataType != CaliDataType.None)
  95. MainFormTabPage.RefreshData();
  96. }
  97. }
  98. MessageBox.Show("Ok!");
  99. }
  100. private void ToolStripMenuItemLoadRemoteUsingData_Click(object sender, EventArgs e)
  101. {
  102. if (currInstrument == null)
  103. {
  104. MessageBox.Show("请先连接仪器!");
  105. return;
  106. }
  107. if (MessageBox.Show("该操作将覆盖本地校准数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
  108. return;
  109. foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
  110. {
  111. ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
  112. if (caliData != null)
  113. {
  114. InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
  115. }
  116. }
  117. foreach (TabPage tabPage in this.tabControl1.TabPages)
  118. {
  119. if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
  120. {
  121. if (MainFormTabPage.CaliDataType != CaliDataType.None)
  122. MainFormTabPage.RefreshData();
  123. }
  124. }
  125. MessageBox.Show("Ok!");
  126. }
  127. private void ToolStripMenuItemSaveCaliData2LocalFile_Click(object sender, EventArgs e)
  128. {
  129. if (currInstrument == null)
  130. {
  131. MessageBox.Show("请先连接仪器!");
  132. return;
  133. }
  134. if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
  135. return;
  136. string folder = folderBrowserDialog1.SelectedPath;
  137. if (folder.Last<char>() != '\\')
  138. folder += '\\';
  139. foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
  140. {
  141. ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
  142. if (caliData != null)
  143. {
  144. InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
  145. caliData.SaveToFile(folder);
  146. }
  147. }
  148. foreach (TabPage tabPage in this.tabControl1.TabPages)
  149. {
  150. if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
  151. {
  152. if (MainFormTabPage.CaliDataType != CaliDataType.None)
  153. MainFormTabPage.RefreshData();
  154. }
  155. }
  156. MessageBox.Show("Ok!");
  157. }
  158. private void ToolStripMenuItemLoadRemoteCaliFileData_Click(object sender, EventArgs e)
  159. {
  160. if (currInstrument == null)
  161. {
  162. MessageBox.Show("请先连接仪器!");
  163. return;
  164. }
  165. if (MessageBox.Show("该操作将覆盖本地校准数据、远端运行态校准数据,你确认要进行此操作吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
  166. return;
  167. foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
  168. {
  169. ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
  170. if (caliData != null)
  171. {
  172. InstrumentInteract.CaliData_LoadFromFile(this.currInstrument, dataType);
  173. InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
  174. }
  175. }
  176. foreach (TabPage tabPage in this.tabControl1.TabPages)
  177. {
  178. if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
  179. {
  180. if (MainFormTabPage.CaliDataType != CaliDataType.None)
  181. MainFormTabPage.RefreshData();
  182. }
  183. }
  184. MessageBox.Show("Ok!");
  185. }
  186. private void Init()
  187. {
  188. currInstrument = null;
  189. waveViewer1.CurrInstrument = currInstrument;
  190. foreach (TabPage tabPage in this.tabControl1.TabPages)
  191. {
  192. if (tabPage.Controls[0] is IMainFormTabPage)
  193. (tabPage.Controls[0] as IMainFormTabPage).SetInstrumentInteract(currInstrument);
  194. }
  195. }
  196. private IInstrumentSession currInstrument = null;
  197. private void MainForm_Load(object sender, EventArgs e)
  198. {
  199. }
  200. private void RefreshConstDataFromServer()
  201. {
  202. return;
  203. string scpiCmd = InstrumentInteract.GetCmdStr(ScpiCmd.Factory_SpecailData);
  204. scpiCmd += " ? "+ "GetComModelConstData";
  205. currInstrument.WriteString(scpiCmd);
  206. Thread.Sleep(100);
  207. string recvStr = currInstrument.ReadString();
  208. string[] paramList = recvStr.Split('|');
  209. foreach (string s in paramList)
  210. {
  211. string[] perParam = s.Split(':');
  212. int value = Int32.Parse(perParam[1]);
  213. if (perParam[0] == "HardwareAttached")
  214. ServerDomainConstants.HardwareAttached = (value == 1);
  215. else if (perParam[0] == "ProductType")
  216. ServerDomainConstants.ProductType = (ComModel.ProductType)value;
  217. else if (perParam[0] == "AdcBits")
  218. ServerDomainConstants.AdcBits = value;
  219. else if (perParam[0] == "AdcNum")
  220. ServerDomainConstants.AdcNum = value;
  221. else if (perParam[0] == "MaxAdcRes")
  222. ServerDomainConstants.MaxAdcRes = value;
  223. else if (perParam[0] == "AnalogChannelCount")
  224. ServerDomainConstants.AnalogChannelCount = value;
  225. }
  226. }
  227. private void buttonConnectInstrument_Click(object sender, EventArgs e)
  228. {
  229. if (buttonConnectInstrument.Tag.ToString() == "0")
  230. {
  231. string message = "";
  232. if (currInstrument != null)
  233. currInstrument.Close();
  234. if (comboBoxVisaResource.Items.Count == 0)
  235. {
  236. MessageBox.Show("没有可连接的仪器,请点击[刷新]按钮或启动示波器软件!");
  237. return;
  238. }
  239. if (comboBoxVisaResource.SelectedItem == null)
  240. {
  241. MessageBox.Show("没有可连接的仪器,请点击[刷新]按钮或启动示波器软件!");
  242. return;
  243. }
  244. currInstrument = InstrumentSessionEngine.TryGetSession(comboBoxVisaResource.SelectedItem.ToString(), "20", out message);
  245. //currInstrument = InstrumentSessionEngine.TryGetSession("TCPIP0::192.168.2.110::inst0::INSTR", "20", out message);
  246. if (currInstrument == null)
  247. {
  248. MessageBox.Show("不能连接到指定的仪器,请确认:\r\n1、仪器的地址是否正确?\r\n2、仪器是否打开?", "错误提示");
  249. return;
  250. }
  251. waveViewer1.CurrInstrument = currInstrument;
  252. RefreshConstDataFromServer();
  253. this.tabPageMatlabSourceCode1.SetInstrument(currInstrument);
  254. currInstrument.WriteString("*CLS");
  255. foreach (TabPage tabPage in this.tabControl1.TabPages)
  256. {
  257. if (tabPage.Controls[0] is IMainFormTabPage)
  258. (tabPage.Controls[0] as IMainFormTabPage).SetInstrumentInteract(currInstrument);
  259. }
  260. if (MessageBox.Show("需要从远端装载校准数据吗?","提示",MessageBoxButtons.YesNo)== DialogResult.Yes)
  261. {
  262. foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
  263. {
  264. ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
  265. if (caliData != null)
  266. {
  267. InstrumentInteract.CaliData_LoadFromFile(this.currInstrument, dataType);
  268. InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
  269. }
  270. }
  271. foreach (TabPage tabPage in this.tabControl1.TabPages)
  272. {
  273. if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
  274. {
  275. if (MainFormTabPage.CaliDataType != CaliDataType.None)
  276. MainFormTabPage.RefreshData();
  277. }
  278. }
  279. }
  280. buttonConnectInstrument.Text = "断开仪器";
  281. }
  282. else
  283. {
  284. currInstrument.Close();
  285. currInstrument = null;
  286. waveViewer1.CurrInstrument = currInstrument;
  287. this.tabPageMatlabSourceCode1.SetInstrument(null);
  288. foreach (TabPage tabPage in this.tabControl1.TabPages)
  289. {
  290. if (tabPage.Controls[0] is IMainFormTabPage)
  291. (tabPage.Controls[0] as IMainFormTabPage).SetInstrumentInteract(currInstrument);
  292. }
  293. buttonConnectInstrument.Text = "连接仪器";
  294. }
  295. buttonConnectInstrument.Tag = buttonRefresh.Enabled ? "1" : "0";
  296. this.buttonRefresh.Enabled = !this.buttonRefresh.Enabled;
  297. comboBoxVisaResource.Enabled = this.buttonRefresh.Enabled;
  298. }
  299. private void buttonWaveRegionCtrl_Click(object sender, EventArgs e)
  300. {
  301. splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
  302. waveViewer1.Run(!splitContainer1.Panel1Collapsed);
  303. buttonWaveRegionCtrl.Text = splitContainer1.Panel1Collapsed ? "显示波形区" : "隐藏波形区";
  304. buttonCtrlRegionCtrl.Text = splitContainer1.Panel2Collapsed ? "显示控制区" : "隐藏控制区";
  305. }
  306. private void buttonCtrlRegionCtrl_Click(object sender, EventArgs e)
  307. {
  308. splitContainer1.Panel2Collapsed = !splitContainer1.Panel2Collapsed;
  309. waveViewer1.Run(!splitContainer1.Panel1Collapsed);
  310. buttonWaveRegionCtrl.Text = splitContainer1.Panel1Collapsed ? "显示波形区" : "隐藏波形区";
  311. buttonCtrlRegionCtrl.Text = splitContainer1.Panel2Collapsed ? "显示控制区" : "隐藏控制区";
  312. }
  313. private void buttonLoadAllCalibrationData_Click(object sender, EventArgs e)
  314. {
  315. if (currInstrument == null)
  316. {
  317. MessageBox.Show("请先连接仪器!");
  318. return;
  319. }
  320. foreach (CaliDataType dataType in Enum.GetValues(typeof(CaliDataType)))
  321. {
  322. ICaliData? caliData = CalibrationData.Helper.GetICaliData(dataType);
  323. if (caliData != null)
  324. {
  325. InstrumentInteract.CaliData_Get(this.currInstrument, dataType);
  326. }
  327. }
  328. foreach (TabPage tabPage in this.tabControl1.TabPages)
  329. {
  330. if (tabPage.Controls[0] is IMainFormTabPage MainFormTabPage)
  331. {
  332. if (MainFormTabPage.CaliDataType != CaliDataType.None)
  333. MainFormTabPage.RefreshData();
  334. }
  335. }
  336. MessageBox.Show("Ok!");
  337. }
  338. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  339. {
  340. waveViewer1.SaveSetting();
  341. }
  342. private void buttonGotoExePath_Click(object sender, EventArgs e)
  343. {
  344. System.Diagnostics.Process.Start("explorer.exe", Path.GetDirectoryName(Application.ExecutablePath));
  345. }
  346. private void buttonRefresh_Click(object sender, EventArgs e)
  347. {
  348. string filter = "?*";
  349. comboBoxVisaResource.Items.Clear();
  350. using (var rm = new ResourceManager())
  351. {
  352. try
  353. {
  354. IEnumerable<string> resources = rm.Find(filter);
  355. foreach (string s in resources)
  356. {
  357. comboBoxVisaResource.Items.Add(s);
  358. }
  359. }
  360. catch (Exception ex)
  361. {
  362. MessageBox.Show(ex.Message);
  363. }
  364. }
  365. if (comboBoxVisaResource.Items.Count > 0)
  366. comboBoxVisaResource.SelectedIndex = 0;
  367. }
  368. }
  369. }