C# GPIB通讯

需要引用以下两个dll;

Ivi.Visa.dll    和 Ivi.Visa.Interop.dll

安装NIVISA版本驱动dll会在C盘

C:\Program Files\IVI Foundation\VISA\Microsoft.NET\Framework64\v2.0.50727\VISA.NET Shared Components 5.8.0

C:\Program Files\IVI Foundation\VISA\VisaCom64\Primary Interop Assemblies

详细代码

using Ivi.Visa.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HelpTool
{
    /// <summary>
    /// GPIB通讯类
    /// </summary>
    public class GPIB_Comm
    {
        private ResourceManager ResourceMgr = new ResourceManager();
        private FormattedIO488 DeviceIO488 = null;
        private string address = "";
        private string errorMsg = "";
        private bool isOpen;

        /// <summary>
        /// GPIB通讯地址
        /// </summary>
        public int Address;
        /// <summary>
        /// 连接电脑的USB接口号
        /// </summary>
        public int ID;

        /// <summary>
        /// 
        /// </summary>
        public GPIB_Comm()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="addr">GPIB通讯地址</param>
        /// <param name="id">连接电脑的USB接口号</param>
        public GPIB_Comm(int addr, int id = 0)
        {
            Address = addr;
            ID = id;
        }

        /// <summary>
        /// 设置通讯USB接口号,及GPIB地址
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="id">连接电脑的USB接口号</param>
        public void SetID(int addr, int id = 0)
        {
            Address = addr;
            ID = id;
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string GetErrorMessage()
        {
            string temp = errorMsg;
            errorMsg = "";//获取错误信息后清除,避免重复获取

            return temp;
        }

        /// <summary>
        /// 仪器是否连接
        /// </summary>
        public bool IsOpen
        {
            get { return isOpen; }
        }

        /// <summary>
        /// 打开仪器
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            try
            {
                isOpen = false;
                address = "GPIB" + ID + "::" + Address + "::INSTR";

                DeviceIO488 = new FormattedIO488();
                DeviceIO488.IO = (IMessage)ResourceMgr.Open(address);

                if (DeviceIO488.IO != null)
                {
                    DeviceIO488.IO.TerminationCharacterEnabled = true;
                }

                Thread.Sleep(200);

                // Check Model Name
                string model = "";
                DeviceIO488.WriteString("*IDN?");   //This command reads the instrument's (mainframe) identification string which contains comma-separated fields.
                Thread.Sleep(20);
                model = DeviceIO488.ReadString().Trim();
                if (model.Contains("MODEL 2400"))
                {
                    isOpen = true;
                }
            }
            catch (Exception ex)
            {
                DeviceIO488 = null;
                errorMsg = ex.Message;
                isOpen = false;
            }
            return isOpen;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        public void Close()
        {
            if (IsOpen)
            {
                DeviceIO488.IO.Close();
                isOpen = false;
            }
        }

        /// <summary>
        /// 写入数据,按仪器通讯协议,对应指令实现功能
        /// </summary>
        /// <param name="strData"></param>
        public void Write(string strData)
        {
            try
            {
                if (isOpen)
                {
                    DeviceIO488.WriteString(strData);
                }
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
            }
        }

        /// <summary>
        /// 读取数据,以结束符停止
        /// </summary>
        /// <returns></returns>
        public string Read()
        {
            string strInfo = "";
            try
            {
                if (isOpen)
                {
                    strInfo = DeviceIO488.ReadString();
                }
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
            }

            return strInfo;
        }
    }
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在C#中进行GPIB通讯控制Keithley源表,您可以使用NI-VISA库和Keithley提供的GPIB驱动程序。下面是一个简单的示例代码,演示如何使用C#进行通讯控制: ```csharp using System; using Ivi.Visa.Interop; namespace GPIB_Communication { class Program { static void Main(string[] args) { ResourceManager resourceManager = new ResourceManager(); FormattedIO488 gpib = new FormattedIO488(); try { // 打开GPIB设备 string deviceAddress = "GPIB0::22::INSTR"; gpib.IO = (IMessage)resourceManager.Open(deviceAddress, AccessMode.NO_LOCK, 2000, ""); // 发送命令到设备 gpib.WriteString("*IDN?", true); // 读取设备返回的数据 string response = gpib.ReadString(); Console.WriteLine("Device response: " + response); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } finally { // 关闭连接 gpib.IO.Close(); gpib.IO.Dispose(); resourceManager.Close(); } } } } ``` 请确保已经安装了NI-VISA库和Keithley提供的GPIB驱动程序,并在项目中引用了相应的库。在示例代码中,我们使用`ResourceManager`来管理设备资源,使用`FormattedIO488`来进行格式化的输入输出。 在`deviceAddress`变量中,您需要根据实际情况指定Keithley源表的GPIB地址。然后,我们发送命令`*IDN?`到设备,并读取设备返回的数据。 请注意,此示例代码仅供参考,具体的实现可能需要根据您的设备和需求进行适当修改。希望这能帮助到您!如果您有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值