123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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;
- }
- /// <summary>
- /// 对于Visa总线,方法close 与Dispose功能一样
- /// </summary>
- public void Dispose()
- {
- this.Close();
- }
- /// <summary>
- /// 打开设备,在Initilize函数之后,可以Open打开设备,或者Close关闭设备
- /// </summary>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 关闭设备
- /// </summary>
- /// <returns></returns>
- public virtual bool Close()
- {
- if (mbSession != null)
- {
- try
- {
- mbSession.Dispose();
- mbSession = null;
- }
- catch (Exception)
- {
- }
- finally
- {
- mbSession = null;
- }
- }
- IsOk = false;
- return true;
- }
- /// <summary>
- /// 写总线
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- public virtual bool Write(string command)
- {
- if (mbSession != null)
- {
- mbSession.Write(command);
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 读总线
- /// </summary>
- /// <returns></returns>
- public virtual string Read()
- {
- if (mbSession != null)
- {
- string responseString = mbSession.ReadString();
- return responseString;
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- public virtual string Query(string command)
- {
- if (mbSession != null)
- {
- mbSession.Write(command);
- string responseString = mbSession.ReadString();
- return responseString;
- }
- else
- {
- return null;
- }
- }
- }
- }
|