如何使用c#通过USB连接到仪器

3

我正在尝试使用Ivi.Visa.Interop .dll通过USB与Voltech PM1000+功率计通信。我对C#相对较新,不太清楚从哪里开始。我在使用Visual Studio 2015 Community。我已经使用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(string, Ivi.Visa.Interop.AccessMode, int, string) 我尝试实现这个,但我真的不知道从哪里开始。
如果有人能给我一个查询“*IDN?”命令的示例,那就太好了。或者,如果有比通过Ivi.Visa.Interop dll更好的方法,请告诉我。
提前感谢!

由于这非常特定于我们99.9%没有的设备,您可能在这里得不到太多帮助。我建议联系发布API的人并直接寻求帮助。 - Ron Beyer
我不是在寻找特定的代码集。我正在寻找如何打开、写入、读取和关闭USB端口的方法。"*IDN?"只是一个标准命令,用于读取仪器的身份。我不是在寻找确切的命令,而是在寻找方向。 - rtyson
2个回答

2

先重启您的设备。清除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
我经常做你要求的事情,我完全理解这会带来多么沮丧的感觉。我记得曾经在谷歌上搜索代码。该代码实际上来自Keysight文档,当我购买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 { }
            }
        }
    }
}

愉快的编程!


此外,请确保查看您发送给特定仪器的特定命令是否需要等待指定时间来处理该命令。这与您编程的设备有关,而不是与编程本身有关。 - EngineerDude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接