使用C#编写用以与仪表通讯的DLL,将DLL放置在Python项目的工作目录的dll文件夹中,在Python项目中调用。
C#
using NationalInstruments.Visa;
namespace VISA_DLL
{
public class MyVISA
{
public MessageBasedSession Connect(string instrumentAddress)
{
ResourceManager resourceManager = new ResourceManager();
MessageBasedSession instrument = (MessageBasedSession)resourceManager.Open(instrumentAddress);
instrument.RawIO.Write("*IDN?");
string response = instrument.RawIO.ReadString();
Console.WriteLine("Instrument connected: " + response);
return instrument;
}
public void Write(MessageBasedSession instrument, string command)
{
instrument.RawIO.Write(command);
}
public string Read(MessageBasedSession instrument)
{
return instrument.RawIO.ReadString();
}
public string Query(MessageBasedSession instrument, string command)
{
instrument.RawIO.Write(command);
return instrument.RawIO.ReadString();
}
}
}
python
import clr
import os
clr.AddReference(os.getcwd()+f"\\dll\\VISA_DLL.dll")
from VISA_DLL import *
myVISA = MyVISA()
instrument=myVISA.Connect("TCPIP0::192.168.1.10::INSTR")
print(myVISA.Query(instrument,"*IDN?"))
还有一种方法是在编写C# DLL时使用UnmannedExports软件包把每个方法都转成CLR,在python中使用ctypes包调用,该方法研究得浅也没实现出来,不多阐述。
如有错误,望斧正!