【问题标题】:How to connect to instrument through USB using c#如何使用c#通过USB连接仪器
【发布时间】:2015-11-05 13:57:08
【问题描述】:

我正在尝试使用 Ivi.Visa.Interop .dll 通过 USB 与 Voltech PM1000+ 功率计进行通信。我对 C# 比较陌生,不知道从哪里开始。我正在使用 Visual Studio 2015 社区。我已经与使用 GPIB 的不同工具进行了交谈,这里是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ivi.Visa.Interop;


namespace commOverIP
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void InitiateIOBtn_Click(object sender, EventArgs e)
    {
        ///testing out excel
        InitiateIOBtn.Text = "Initializing";



        try
        {
            // resource manager and message-based session manager
            Ivi.Visa.Interop.ResourceManager mngr = new Ivi.Visa.Interop.ResourceManager();

            // GPIB address
            string srcAddress = "GPIB::27::INSTR"; // GPIB address of data acquisition
            //setting up communication
            Ivi.Visa.Interop.FormattedIO488 instrument = new Ivi.Visa.Interop.FormattedIO488();
            Ivi.Visa.Interop.IMessage Imsg = (mngr.Open(srcAddress, Ivi.Visa.Interop.AccessMode.NO_LOCK, 1000, "") as IMessage);
            instrument.IO = Imsg;

            instrument.IO.Clear();//clear io buffer
            instrument.WriteString("*RST", true);//send RST? command to instrument
            instrument.WriteString("*IDN?", true);//send IDN? command to instrument
            returnOfCommand.Text = instrument.ReadString();//read IDN? result

            //close communication
            instrument.IO.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(instrument);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mngr);

            InitiateIOBtn.Text = "Initialize I/O";
            //*/
        }
        catch(Exception exp)
        {
            MessageBox.Show(exp.Message);
        }
        InitiateIOBtn.Text = "Initialize I/O";
    }
}
}

这工作正常,但 USB 似乎是一个不同的野兽。我发现的唯一真正的线索是在 .dll 中: IUsb.Init(字符串,Ivi.Visa.Interop.AccessMode,int,字符串) 我尝试实现这一点,但我真的不知道从哪里开始。

谁能给我一个如何查询“*IDN”的示例?命令会很棒。或者,即使有比通过 Ivi.Visa.Interop dll 更好的方法。

提前致谢

【问题讨论】:

  • 看到这对于我们 99.9% 没有的设备来说是非常具体的,你可能在这里得不到很多帮助。我建议联系发布 API 的人并直接寻求帮助。
  • 我不是在寻找特定的代码集。我正在寻找如何打开、写入、读取和关闭 USB 端口。 “*国际化域名?”只是您发送到仪器以读取仪器身份的标准命令。我不是在寻找确切的命令,我在寻找方向。

标签: c# initialization usb communication


【解决方案1】:

重新启动您的设备一次。清除 IO 也有帮助。之后以下代码应该可以正常工作:

    string resourceString= "USB0::xxx::xxx::xxx::0::INSTR";  
    ResourceManager manager = new ResourceManager();  
    FormattedIO488 connection = new FormattedIO488();
    connection.IO = (IMessage)manager.Open(resourceString, AccessMode.NO_LOCK, 0, "");
    connection.IO.Clear();
    connection.WriteString("*IDN?", true);
    string result = connection.ReadString();

【讨论】:

    【解决方案2】:

    我一直按照您的要求做,我完全理解这有多么令人沮丧。我记得做谷歌搜索想出这段代码。该代码实际上来自我购买 Agilent 82357B USB/GPIB 控制器时的一些是德科技文档。

    这可以适用于任何 GPIB 乐器,唯一的区别是您发送到乐器的字符串。这些可以通过获取您感兴趣的仪器的编程手册来获得。

    我安装了用于 Agilent 82357B 的 Keysight(以前称为 Agilent)I/O 库套件。不明显的一件事是您应该禁用“自动发现”选项,因为此功能有时会将您的设备置于本地模式。

    using System.Threading;
    using System.Runtime.InteropServices;
    // Add reference for VISA-COM 5.9 Type Library
    using Ivi.Visa.Interop;
    
    namespace USBCommunications
    {
        class Program
        {
            static void Main(string[] args)
            {
                Gpib.Write(address: 5, command: "*IDN?");
                bool success = Gpib.Read(address: 5, valueRead: out string valueRead);
                System.Console.WriteLine($"The ID is {valueRead}");
    
                System.Console.ReadLine();
            }
        }
    
        public class Gpib
        {
            static ResourceManager resourceManager;
            static FormattedIO488 ioObject;
    
            public static bool Write(byte address, string command)
            {
                resourceManager = new ResourceManager();
                ioObject = new FormattedIO488();
    
                string addr = $"GPIB::{address.ToString()}::INSTR";
    
                try
                {
                    ioObject.IO = (IMessage)resourceManager.Open(addr, AccessMode.NO_LOCK, 0, "");
                    Thread.Sleep(20);
                    ioObject.WriteString(data: command, flushAndEND: true);
    
                    return true;
                }
                catch
                {
                    return false;
                }
                finally
                {
                    try { ioObject.IO.Close(); }
                    catch { }
                    try { Marshal.ReleaseComObject(ioObject); }
                    catch { }
                    try { Marshal.ReleaseComObject(resourceManager); }
                    catch { }
                }
            }
    
    
            public static bool Read(byte address, out string valueRead)
            {
                resourceManager = new ResourceManager();
                ioObject = new FormattedIO488();
    
                string addr = $"GPIB::{address.ToString()}::INSTR";
    
                try
                {
                    ioObject.IO = (IMessage)resourceManager.Open(addr, AccessMode.NO_LOCK, 0, "");
                    Thread.Sleep(20);
                    valueRead = ioObject.ReadString();
    
                    return true;
                }
                catch
                {
                    valueRead = "";
                    return false;
                }
                finally
                {
                    try { ioObject.IO.Close(); }
                    catch { }
                    try { Marshal.ReleaseComObject(ioObject); }
                    catch { }
                    try { Marshal.ReleaseComObject(resourceManager); }
                    catch { }
                }
            }
        }
    }
    

    编程愉快!!

    【讨论】:

    • 另外,请务必查看您发送到特定仪器的特定命令是否需要您等待指定时间来处理命令。这与您正在编程的设备有关,与编程本身无关。
    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多