using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NationalInstruments.VisaNS;
namespace CommonDevHostApp.Devices
{
public class CommonVisaResource : IDisposable
{
private MessageBasedSession mbSession;
public bool IsOk { get; set; }
public string ResourceHandle;
public CommonVisaResource()
{
IsOk = false;
}
///
/// 对于Visa总线,方法close 与Dispose功能一样
///
public void Dispose()
{
this.Close();
}
///
/// 打开设备,在Initilize函数之后,可以Open打开设备,或者Close关闭设备
///
///
public virtual bool Open(string ResourceHandle)
{
if (IsOk)
return true;
this.ResourceHandle = ResourceHandle;
if (mbSession != null)
Close();
mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(this.ResourceHandle);
if (mbSession != null)
{
IsOk = true;
return true;
}
else
{
IsOk = false;
return false;
}
}
///
/// 关闭设备
///
///
public virtual bool Close()
{
if (mbSession != null)
{
try
{
mbSession.Dispose();
mbSession = null;
}
catch (Exception)
{
}
finally
{
mbSession = null;
}
}
IsOk = false;
return true;
}
///
/// 写总线
///
///
///
public virtual bool Write(string command)
{
if (mbSession != null)
{
mbSession.Write(command);
return true;
}
else
{
return false;
}
}
///
/// 读总线
///
///
public virtual string Read()
{
if (mbSession != null)
{
string responseString = mbSession.ReadString();
return responseString;
}
else
{
return null;
}
}
///
/// 查询
///
///
///
public virtual string Query(string command)
{
if (mbSession != null)
{
mbSession.Write(command);
string responseString = mbSession.ReadString();
return responseString;
}
else
{
return null;
}
}
}
}