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

问题描述 投票:3回答:2

我正在尝试使用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(string,Ivi.Visa.Interop.AccessMode,int,string)我尝试实现这个但我真的不知道从哪里开始。

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

提前致谢

c# initialization usb communication
2个回答
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();

1
投票

我一直在做你所要求的事情,我完全明白它是多么令人沮丧。我记得在谷歌搜索中提出这个代码。当我购买Agilent 82357B USB / GPIB控制器时,代码实际上来自一些是德科技文档。

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

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

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

快乐编程!!

© www.soinside.com 2019 - 2024. All rights reserved.