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

本文关键字:连接 仪器 USB 通过 何使用 | 更新日期: 2023-09-27 18:07:51

我正试图使用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, string)我试着实现这个,但我真的不知道从哪里开始。

如果有人能给我一个如何查询"*IDN?"命令的例子,那将是伟大的。或者,即使有比通过Ivi.Visa.Interop dll更好的方法,

Thanks in advance

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

重启一次设备。清除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();

我一直在做你要求的事情,我完全理解这有多令人沮丧。我记得我用谷歌搜索得出了这个代码。代码实际上来自于我购买安捷伦82357B USB/GPIB控制器时的一些是德科技文档。

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

我安装了与Agilent 82357B一起使用的Keysight(以前的Agilent) I/O Library Suites。有一件事不太明显,那就是你应该禁用"自动发现"选项,因为这个功能偶尔会让你的设备进入本地模式。

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 { }
            }
        }
    }
}

编程快乐! !