├── VISAInstrument
├── logo.ico
├── Utility
│ ├── PortType.cs
│ ├── UninstallInfoOption.cs
│ ├── Extension
│ │ ├── UI
│ │ │ ├── Pair.cs
│ │ │ ├── TextBoxEx.cs
│ │ │ ├── Pair2.cs
│ │ │ ├── PairCollection.cs
│ │ │ └── ComboBoxEx.cs
│ │ ├── RegexEx.cs
│ │ ├── ByteEx.cs
│ │ └── StringEx.cs
│ ├── PortHelper.cs
│ ├── UninstallInfo.cs
│ └── UninstallInfoHelper.cs
├── Ports
│ ├── IPortType.cs
│ ├── IPortOperator.cs
│ ├── PortEventArgs.cs
│ ├── SerialDataReceivedEventArgs.cs
│ ├── UsbPortOperator.cs
│ ├── GpibPortOperator.cs
│ ├── LanPortOperator.cs
│ ├── PortOperatorBase.cs
│ └── VISAInstrument.Port.cs
├── Configuration
│ └── VisaInformation.cs
├── Properties
│ ├── Settings.settings
│ ├── Settings.Designer.cs
│ ├── AssemblyInfo.cs
│ ├── Resources.resx
│ └── Resources.Designer.cs
├── VISAInstrument.csproj
├── Program.cs
├── FrmMain.cs
├── FrmMain.resx
└── FrmMain.Designer.cs
├── README.md
├── VISAInstrument.sln
├── .gitattributes
├── .gitignore
└── LICENSE
/VISAInstrument/logo.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cnxy/VISAInstrument/HEAD/VISAInstrument/logo.ico
--------------------------------------------------------------------------------
/VISAInstrument/Utility/PortType.cs:
--------------------------------------------------------------------------------
1 | namespace VISAInstrument.Utility
2 | {
3 | internal enum PortType
4 | {
5 | Rs232, Usb, Gpib, Lan, None
6 | }
7 | }
--------------------------------------------------------------------------------
/VISAInstrument/Ports/IPortType.cs:
--------------------------------------------------------------------------------
1 | using VISAInstrument.Utility;
2 |
3 | namespace VISAInstrument.Ports
4 | {
5 | internal interface IPortType
6 | {
7 | PortType PortType { get; }
8 | }
9 | }
--------------------------------------------------------------------------------
/VISAInstrument/Utility/UninstallInfoOption.cs:
--------------------------------------------------------------------------------
1 | namespace VISAInstrument.Utility
2 | {
3 | public enum UninstallInfoOption
4 | {
5 | Only32Bit,
6 | Only64Bit,
7 | Both
8 | }
9 | }
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/UI/Pair.cs:
--------------------------------------------------------------------------------
1 | namespace VISAInstrument.Utility.Extension.UI
2 | {
3 | internal class Pair
4 | {
5 | public static string ValueName => "Value";
6 | public static string DisplayName => "Display";
7 | }
8 | }
--------------------------------------------------------------------------------
/VISAInstrument/Ports/IPortOperator.cs:
--------------------------------------------------------------------------------
1 | namespace VISAInstrument.Ports
2 | {
3 | internal interface IPortOperator
4 | {
5 | void Open();
6 | void Close();
7 | void Write(string command);
8 | string Read();
9 | }
10 | }
--------------------------------------------------------------------------------
/VISAInstrument/Configuration/VisaInformation.cs:
--------------------------------------------------------------------------------
1 | namespace VISAInstrument.Configuration
2 | {
3 | internal class VisaInformation
4 | {
5 | public static string[] VisaSharedComponent { get; set; }
6 | public static string[] NiVisaRuntime { get; set; }
7 | }
8 | }
--------------------------------------------------------------------------------
/VISAInstrument/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/RegexEx.cs:
--------------------------------------------------------------------------------
1 | using System.Text.RegularExpressions;
2 |
3 | namespace VISAInstrument.Utility.Extension
4 | {
5 | public static class RegexEx
6 | {
7 | public static bool IsMatch(this string input,string pattern)
8 | {
9 | return Regex.IsMatch(input, pattern);
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/UI/TextBoxEx.cs:
--------------------------------------------------------------------------------
1 | using System.Windows.Forms;
2 |
3 | namespace VISAInstrument.Utility.Extension.UI
4 | {
5 | static class TextBoxEx
6 | {
7 | public static void SetSelect(this TextBoxBase textBox)
8 | {
9 | textBox.Focus();
10 | textBox.SelectAll();
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/VISAInstrument/Ports/PortEventArgs.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace VISAInstrument.Ports
4 | {
5 | internal class PortEventArgs : EventArgs
6 | {
7 | public string Address { get; }
8 | public bool Cancel { set; get; }
9 | public PortEventArgs(string address)
10 | {
11 | Address = address;
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/VISAInstrument/Ports/SerialDataReceivedEventArgs.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace VISAInstrument.Ports
4 | {
5 | public class SerialDataReceivedEventArgs:EventArgs
6 | {
7 | public int BytesToRead { get; }
8 | public SerialDataReceivedEventArgs(int bytesToRead)
9 | {
10 | BytesToRead = bytesToRead;
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/UI/Pair2.cs:
--------------------------------------------------------------------------------
1 | namespace VISAInstrument.Utility.Extension.UI
2 | {
3 | internal class Pair : Pair
4 | {
5 | public Pair() { }
6 |
7 | public Pair(TValue value, TDisplay display)
8 | {
9 | this.Value = value;
10 | this.Display = display;
11 | }
12 |
13 | public TValue Value { set; get; }
14 | public TDisplay Display { set; get; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/VISAInstrument/Ports/UsbPortOperator.cs:
--------------------------------------------------------------------------------
1 | using NationalInstruments.Visa;
2 | using System;
3 | using VISAInstrument.Utility;
4 |
5 | namespace VISAInstrument.Ports
6 | {
7 | internal class UsbPortOperator : PortOperatorBase, IPortType
8 | {
9 | public UsbPortOperator(string address) : base(new UsbSession(address), address)
10 | {
11 | if (!address.ToUpper().Contains("USB"))
12 | throw new ArgumentException("该地址不含USB字样");
13 | }
14 | public PortType PortType => PortType.Usb;
15 | }
16 | }
--------------------------------------------------------------------------------
/VISAInstrument/Ports/GpibPortOperator.cs:
--------------------------------------------------------------------------------
1 | using NationalInstruments.Visa;
2 | using System;
3 | using VISAInstrument.Utility;
4 |
5 | namespace VISAInstrument.Ports
6 | {
7 | internal class GpibPortOperator : PortOperatorBase, IPortType
8 | {
9 | public GpibPortOperator(string address) : base(new GpibSession(address), address)
10 | {
11 | if (!address.ToUpper().Contains("GPIB"))
12 | throw new ArgumentException($"该地址不含GPIB字样");
13 | }
14 | public PortType PortType => PortType.Gpib;
15 | }
16 | }
--------------------------------------------------------------------------------
/VISAInstrument/Ports/LanPortOperator.cs:
--------------------------------------------------------------------------------
1 | using NationalInstruments.Visa;
2 | using System;
3 | using VISAInstrument.Utility;
4 |
5 | namespace VISAInstrument.Ports
6 | {
7 | internal class LanPortOperator : PortOperatorBase, IPortType
8 | {
9 | public LanPortOperator(string address) : base(new TcpipSession(address), address)
10 | {
11 | if (!address.ToUpper().Contains("TCPIP"))
12 | throw new ArgumentException($"该地址不含TCPIP字样");
13 | }
14 | public PortType PortType => PortType.Lan;
15 | }
16 | }
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/ByteEx.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace VISAInstrument.Utility.Extension
4 | {
5 | internal static class ByteEx
6 | {
7 | public static bool TryParseByteToByteString(byte[] bytes, out string byteString)
8 | {
9 | byteString = string.Empty;
10 | try
11 | {
12 | byteString = BitConverter.ToString(bytes).Replace("-", " ");
13 | return true;
14 | }
15 | catch
16 | {
17 | return false;
18 | }
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/UI/PairCollection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace VISAInstrument.Utility.Extension.UI
5 | {
6 | internal class PairCollection
7 | {
8 | public PairCollection()
9 | {
10 | DataList = new List>();
11 | }
12 |
13 | public Pair this[int index] => DataList[index];
14 |
15 | public void Add(TValue value, TDisplay display)
16 | {
17 | DataList.Add(new Pair() { Value = value, Display = display });
18 | }
19 |
20 | public void AddRange(TValue[] values, TDisplay[] displays)
21 | {
22 | if (values.Length != displays.Length) throw new ArgumentException("参数长度必须相等");
23 | for (int i = 0; i < values.Length; i++)
24 | {
25 | Add(values[i], displays[i]);
26 | }
27 | }
28 |
29 | public List> DataList { get; }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/VISAInstrument/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // 此代码由工具生成。
4 | // 运行时版本:4.0.30319.42000
5 | //
6 | // 对此文件的更改可能会导致不正确的行为,并且如果
7 | // 重新生成代码,这些更改将会丢失。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace VISAInstrument.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.1.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # VISAInstrument
2 | 基于NI-VISA的仪器编程,支持RS232、USB、GPIB及LAN
3 |
4 | ## 条件
5 | 运行或开发软件时,必须安装NI-VISA运行时(其他VISA版本不支持,如Keysight VISA等)。
6 | ### Release版本
7 | 运行条件:
8 | 若需运行在Win7及以上系统(最高支持Win11系统),请安装15.5版本或以上的运行时,下载链接如下:
9 | https://download.ni.com/support/softlib/visa/NI-VISA/15.5/Windows/NIVISA1550runtime.zip
10 |
11 | 若需运行在Win7及以上系统(最高支持Win11系统),请安装16.0~21.5版本的运行时,18.5版本的下载链接如下:
12 | https://download.ni.com/support/softlib/visa/NI-VISA/18.5/Windows/NIVISA1850runtime.zip
13 |
14 | ### 开发版本
15 | 为了最佳的开发效果,开发时请使用本软件的对应的开发版本(21.0.0)[目前最新版本为21.5],下载链接如下:
16 | https://download.ni.com/support/nipkg/products/ni-v/ni-visa/21.0/offline/ni-visa_21.0.0_offline.iso
17 |
18 | ## RS232
19 | 支持常见的RS232串口编程,一般地址为“ASRL1::INSTR”
20 |
21 | ## USB
22 | 支持常见的USB接口编程,一般地址类似为“USB0::0x2A8D::0x0101::MY57501899::INSTR”
23 |
24 | ## GPIB
25 | 支持常见的USB接口编程,一般地址类似为“GPIB0::0x2A8D::0x0101::MY57501899::INSTR”
26 | 此处要求安装GPIB卡驱动程序,推荐使用NI-GPIB卡(需要驱动程序,驱动程序可以从NI官方网站下载)
27 |
28 | ## LAN
29 | 支持常见的LAN接口编程,一般地址类似为“TCPIP0::34465A-01899::inst0::INSTR”或“TCPIP0::192.168.0.26::INSTR”
30 |
31 | ## 运行界面
32 | 
33 |
--------------------------------------------------------------------------------
/VISAInstrument/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的一般信息由以下
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("VISAInstrument")]
9 | [assembly: AssemblyDescription("基于NI-VISA接口(GPIB、USB、串口、LAN等)的通信连接")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("by CNXY")]
12 | [assembly: AssemblyProduct("VISAInstrument")]
13 | [assembly: AssemblyCopyright("Copyright © CNXY 2022")]
14 | [assembly: AssemblyTrademark("CNXY")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // 将 ComVisible 设置为 false 会使此程序集中的类型
18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
19 | //请将此类型的 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23 | [assembly: Guid("7fa82266-0999-4572-ad46-aa79ed2e9cac")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 生成号
30 | // 修订号
31 | //
32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
33 | // 方法是按如下所示使用“*”: :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.3.0.6")]
36 | [assembly: AssemblyFileVersion("1.3.0.6")]
37 |
--------------------------------------------------------------------------------
/VISAInstrument.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27004.2008
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VISAInstrument", "VISAInstrument\VISAInstrument.csproj", "{7FA82266-0999-4572-AD46-AA79ED2E9CAC}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {7FA82266-0999-4572-AD46-AA79ED2E9CAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {7FA82266-0999-4572-AD46-AA79ED2E9CAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {7FA82266-0999-4572-AD46-AA79ED2E9CAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {7FA82266-0999-4572-AD46-AA79ED2E9CAC}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {2FFCA02A-00FB-4DFB-B270-ABB05E9DEE74}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/StringEx.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace VISAInstrument.Utility.Extension
5 | {
6 | internal static class StringEx
7 | {
8 | public static bool TryParseByteStringToAsciiString(string byteString, out string asciiString)
9 | {
10 | asciiString = string.Empty;
11 | try
12 | {
13 | string[] byteStrings = byteString.Trim(' ').Split(' ');
14 | char[] charValues = byteStrings.Select(x => (char)Convert.ToByte(x, 16)).ToArray();
15 | asciiString = new string(charValues);
16 | return true;
17 | }
18 | catch
19 | {
20 | return false;
21 | }
22 | }
23 | public static bool TryParseAsciiStringToByteString(string asciiString, out string byteString)
24 | {
25 | byteString = string.Empty;
26 | try
27 | {
28 | byte[] byteValues = asciiString.Select(x => (byte)x).ToArray();
29 | byteString = BitConverter.ToString(byteValues).Replace("-", " ");
30 | return true;
31 | }
32 | catch
33 | {
34 | return false;
35 | }
36 | }
37 | public static bool TryParseByteStringToByte(string byteString, out byte[] bytes)
38 | {
39 | bytes = null;
40 | try
41 | {
42 | string[] byteStrings = byteString.Trim(' ').Split(' ');
43 | bytes = byteStrings.Select(x => Convert.ToByte(x, 16)).ToArray();
44 | return true;
45 | }
46 | catch
47 | {
48 | return false;
49 | }
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/VISAInstrument/Utility/Extension/UI/ComboBoxEx.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using System.Windows.Forms;
3 |
4 | namespace VISAInstrument.Utility.Extension.UI
5 | {
6 | internal static class ComboBoxEx
7 | {
8 | public static void SetItems(this ComboBox comboBox, params object[] content)
9 | {
10 | comboBox.Items.AddRange(content);
11 | }
12 |
13 | public static void SetItems(this ComboBox comboBox, TValue[] values, TDisplay[] displays)
14 | {
15 | PairCollection pc = new PairCollection();
16 | pc.AddRange(values, displays);
17 | comboBox.DataSource = pc.DataList;
18 | comboBox.ValueMember = Pair.ValueName;
19 | comboBox.DisplayMember = Pair.DisplayName;
20 | }
21 |
22 | public static void ClearItems(this ComboBox comboBox)
23 | {
24 | comboBox.DataSource = null;
25 | comboBox.Items.Clear();
26 | }
27 |
28 | public static void SetFirstItem(this ComboBox comboBox)
29 | {
30 | if (comboBox.Items.Count != 0)
31 | comboBox.Text = comboBox.Items[0].ToString();
32 | }
33 | public static void SetFirstItem(this ComboBox comboBox)
34 | {
35 | if (comboBox.Items.Count == 0) return;
36 | string item = ((Pair)comboBox.Items[0]).Display.ToString();
37 | comboBox.Text = !string.IsNullOrEmpty(item) ? item : ((Pair)comboBox.Items[comboBox.Items.Count - 1]).Display.ToString();
38 |
39 | }
40 |
41 | public static void ShowAndDisplay(this ComboBox comboBox, params string[] content)
42 | {
43 | comboBox.ClearItems();
44 | comboBox.SetItems(content.Select(x=>(object)x).ToArray());
45 | comboBox.SetFirstItem();
46 | }
47 |
48 | public static void ShowAndDisplay(this ComboBox comboBox, TValue[] values, TDisplay[] displays)
49 | {
50 | comboBox.ClearItems();
51 | comboBox.SetItems(values, displays);
52 | comboBox.SetFirstItem();
53 | }
54 |
55 | public static void AddItem(this ComboBox comboBox, string content)
56 | {
57 | if (comboBox.Items.Cast