C# 如何使用c通过USB连接到仪器#
我正在尝试使用Ivi.Visa.Interop.dll与使用USB的Voltech PM1000+电能表通信。我对C#比较陌生,不知道从哪里开始。我正在使用Visual Studio 2015社区。我已经使用GPIB与不同的仪器进行了交谈,下面是代码:C# 如何使用c通过USB连接到仪器#,c#,initialization,usb,communication,C#,Initialization,Usb,Communication,我正在尝试使用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
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更好的方法
提前感谢重新启动设备一次。清除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控制器时的一些Keysight文档 这可以适用于任何GPIB乐器,唯一的区别是发送到乐器的字符串。这些可以通过获取您感兴趣的仪器的编程手册来获得 我安装了与安捷伦82357B一起使用的Keysight(以前的安捷伦)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 { }
}
}
}
}
快乐编程 看到这是如何非常具体的一件设备99.9%的我们没有,你可能不会得到很多帮助。我建议联系发布API的人,直接寻求帮助。我不是在寻找特定的代码集。我正在寻找如何打开、写入、读取和关闭USB端口。“*IDN?”只是发送给仪器以读取仪器标识的标准命令。我不是在寻找确切的命令,我是在寻找方向。另外,请确保您发送到特定仪器的特定命令是否需要您等待指定的时间来处理该命令。这与正在编程的设备有关,而不是编程本身。