一个GPIB的操作类,采用Adlink的GPIB板卡,使用Adlink提供的C#类(只是调用DLL封装了一下)

类中包括对安捷伦数字万用表的基本操作,对固纬程控电源的基本操作和程控变阻箱的基本操作


using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace GPIB { class gpib { #region GPIB操作包括DMM,PWR,PRS0,PRS1 public short DMMaddr = 22, PWRaddr = 8, PRS0addr = 16, PRS1addr = 17; private int ud = 0; private int ibsta, iberr, ibcnt, ibcntl; public bool write(int addr, string strWrite) { //Open and intialize an GPIB instrument int dev = GPIB.ibdev(0, addr, 0, (int)GPIB.gpib_timeout.T1s, 1, 0); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in initializing the GPIB instrument."); return false; } //clear the specific GPIB instrument GPIB.ibclr(dev); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in clearing the GPIB device."); return false; } //Write a string command to a GPIB instrument using the ibwrt() command GPIB.ibwrt(dev, strWrite, strWrite.Length); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in writing the string command to the GPIB instrument."); return false; } //Offline the GPIB interface card GPIB.ibonl(dev, 0); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in offline the GPIB interface card."); return false; } return true; } public bool read(int addr, string strWrite, string strRead) { StringBuilder str = new StringBuilder(100); //Open and intialize an GPIB instrument int dev = GPIB.ibdev(0, addr, 0, (int)GPIB.gpib_timeout.T1s, 1, 0); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in initializing the GPIB instrument."); return false; } //clear the specific GPIB instrument GPIB.ibclr(dev); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in clearing the GPIB device."); return false; } //Write a string command to a GPIB instrument using the ibwrt() command GPIB.ibwrt(dev, strWrite, strWrite.Length); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in writing the string command to the GPIB instrument."); return false; } //Read the response string from the GPIB instrument using the ibrd() command GPIB.ibrd(dev, str, 100); strRead = str.ToString(); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in reading the response string from the GPIB instrument."); return false; } //Offline the GPIB interface card GPIB.ibonl(dev, 0); GPIB.gpib_get_globals(out ibsta, out iberr, out ibcnt, out ibcntl); if ((ibsta & (int)GPIB.ibsta_bits.ERR) != 0) { //MessageBox.Show("Error in offline the GPIB interface card."); return false; } return true; } #region 数字万用表操作集合 /// <summary> /// 数字万用表:读取电压 /// </summary> public bool DMM_ReadVoltage(double value) { try { string strRead = ""; if (write(DMMaddr, "CONF:VOLT:DC DEF") == false) return false; if (read(DMMaddr, "READ?", strRead) == false) return false; value = Convert.ToDouble(strRead); return true; } catch { //MessageBox.Show("Voltage Read Fail!"); return false; } } /// <summary> /// 数字万用表:读取电流 /// </summary> public bool DMM_ReadCurrent(double value) { try { string strRead = ""; if (write(DMMaddr, "CONF:CURR:DC DEF") == false) return false; if (read(DMMaddr, "READ?", strRead) == false) return false; value = Convert.ToDouble(strRead); return true; } catch { //MessageBox.Show("Voltage Read Fail!"); return false; } } /// <summary> /// 数字万用表:读取阻值 /// </summary> public bool DMM_ReadRes(double value) { try { string strRead = ""; if (write(DMMaddr, "CONF:RES DEF") == false) return false; if (read(DMMaddr, "READ?", strRead) == false) return false; value = Convert.ToDouble(strRead); return true; } catch { //MessageBox.Show("Voltage Read Fail!"); return false; } } /// <summary> /// 数字万用表:读取频率 /// </summary> public bool DMM_ReadFreq(double value) { try { string strRead = ""; if (write(DMMaddr, "CONF:FRES:DC DEF") == false) return false; if (read(DMMaddr, "READ?", strRead) == false) return false; value = Convert.ToDouble(strRead); return true; } catch { //MessageBox.Show("Voltage Read Fail!"); return false; } } /// <summary> /// 数字万用表:设定直流电压范围 /// </summary> public bool DMM_SetDCRange(double VRange, double VDelta) { try { return write(DMMaddr, "CONF:VOLT:DC " + VRange.ToString() + "," + VDelta.ToString()); } catch { MessageBox.Show("Voltage DC Range Setting Fail!"); return false; } } /// <summary> /// 数字万用表:恢复设置 /// </summary> public bool DMM_Reset() { try { if (write(DMMaddr,"*RST") == false) return false; return write(DMMaddr, "*CLS"); } catch { MessageBox.Show("DMM Reset Fail!"); return false; } } /// <summary> /// 数字万用表:设定自动调零 /// </summary> public bool DMM_AutoZero(bool autoZero) { try { if (autoZero) return write(DMMaddr, "ZERO:AUTO ON"); else return write(DMMaddr, "ZERO:AUTO OFF"); } catch { MessageBox.Show("DMM Auto Zero Setting Fail!"); return false; } } /// <summary> /// 数字万用表:设定触发延时 /// </summary> public bool DMM_TrigDelay(double trigDelay) { try { return write(DMMaddr, "TRIG:DEL " + trigDelay.ToString()); } catch { MessageBox.Show("DMM Auto Zero Setting Fail!"); return false; } } /// <summary> /// 数字万用表:设定采样点数 /// </summary> public bool DMM_SampleCounts(uint sampleCount) { try { return write(DMMaddr, "SAMP:COUN " + sampleCount.ToString()); } catch { MessageBox.Show("DMM Sample Counts Setting Fail!"); return false; } } #endregion #region 程控电源操作集合 /// <summary> /// 设置电压值 /// </summary> public bool PWR_SetVoltage(double voltageSet) { try { return write(PWRaddr, ":CHAN1:VOLT " + voltageSet.ToString() + "/n"); } catch { MessageBox.Show("PWR Voltage Setting Fail"); return false; } } /// <summary> /// 设置电流值 /// </summary> public bool PWR_SetCurrent(double currentSet) { try { return write(PWRaddr, ":CHAN1:CURR " + currentSet.ToString() + "/n"); } catch { MessageBox.Show("PWR Current Setting Fail"); return false; } } /// <summary> /// 限流保护 /// </summary> public bool PWR_LimitCurrent(bool currentLimit) { try { return write(PWRaddr, ":CHAN1:PROT:CURR " + currentLimit.ToString() + "/n"); } catch { MessageBox.Show("PWR Current (Un)Limit Fail"); return false; } } /// <summary> /// 限压保护 /// </summary> public bool PWR_LimitVoltage(bool voltageLimit) { try { return write(PWRaddr, ":CHAN1:PROT:VOLT " + voltageLimit.ToString() + "/n"); } catch { MessageBox.Show("PWR Voltage (Un)Limit Fail"); return false; } } /// <summary> /// 启动输出 /// </summary> public bool PWR_Output(bool output) { try { if (output) { if(write(PWRaddr, "OUTP:PROT:CLE/n") == false) return false; return write(PWRaddr, "OUTP:STAT 1/n"); } else return write(PWRaddr, "OUTP:STAT 0/n"); } catch { MessageBox.Show("PWR (Un)Output Fail"); return false; } } /// <summary> /// 获取输出状态 /// </summary> public bool PWR_GetOutput(bool output) { string strRead = ""; output = false; try { if(read(PWRaddr, "OUTP:STAT?/n", strRead) == false) return false; if (strRead == "1/n") output = true; return true; } catch { //MessageBox.Show("PWR (Un)Output Fail"); return false; } } /// <summary> /// 获取电压 /// </summary> public bool PWR_GetVoltage(double value) { string strRead = ""; try { if(read(PWRaddr, ":CHAN1:MEAS:VOLT?/n", strRead) == false) return false; value = Convert.ToDouble(strRead); return true; } catch { //MessageBox.Show("PWR Get Voltage Fail"); return false; } } /// <summary> /// 获取电流 /// </summary> public bool PWR_GetCurrent(double value) { string strRead = ""; try { if (read(PWRaddr, ":CHAN1:MEAS:CURR?/n", strRead) == false) return false; value = Convert.ToDouble(strRead); return true; } catch { //MessageBox.Show("PWR Get Current Fail"); return false; } } /// <summary> /// 恢复设置 /// </summary> public bool PWR_Reset() { try { return write(PWRaddr, "*RST/n"); } catch { MessageBox.Show("PWR Reset Fail"); return false; } } /// <summary> /// 清除状态 /// </summary> public bool PWR_Clear() { try { return write(PWRaddr, "*CLS/n"); } catch { MessageBox.Show("PWR Reset Fail"); return false; } } #endregion #region 程控变阻箱操作集合 /// <summary> /// 设置电阻值0 /// </summary> public bool PRS0_SetResistance(ulong resSet) { try { string resString = ""; string resStringAdd = resSet.ToString(); if (resStringAdd.Length < 2) resString = "SOURce:DATA 200000000000"; else if (resStringAdd.Length > 8) resString = "SOURce:DATA 100000000000"; else { for (short i = 12; i > resStringAdd.Length; i--) resString += "0"; //resStringAdd.PadRight(12, '0'); resString += resStringAdd; resString = "SOURce:DATA " + resString; } return (write(PRS0addr,resString)); } catch { MessageBox.Show("PRS0 Setting Fail"); return false; } } /// <summary> /// 设置电阻值1 /// </summary> public bool PRS1_SetResistance(ulong resSet) { try { string resString = ""; string resStringAdd = resSet.ToString(); if (resStringAdd.Length < 2) resString = "SOURce:DATA 200000000000"; else if (resStringAdd.Length > 8) resString = "SOURce:DATA 100000000000"; else { for (short i = 12; i > resStringAdd.Length; i--) resString += "0"; //resStringAdd.PadRight(12, '0'); resString += resStringAdd; resString = "SOURce:DATA " + resString; } return (write(PRS1addr, resString)); } catch { MessageBox.Show("PRS1 Setting Fail"); return false; } } #endregion #endregion } }

一个GPIB操作的C#类相关推荐

  1. 自己封装的poi操作Excel工具类

    在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完成的功能是:读取Excel.汇总Exc ...

  2. 【转载苏飞的博客】一个操作数据库的类(备份,还原,分离,附加,添加删除用户等操作)

    一个操作数据库的类(备份,还原,分离,附加,添加删除用户等操作)  by sufeinet, 地址: cnblogs.com/sufei/archive/2011/09/13/2174716.html ...

  3. 自己动手写一个能操作redis的客户端

    作者:孤独烟 来自:http://rjzheng.cnblogs.com/ 引言 redis大家在项目中经常会使用到.官网也提供了多语言的客户端供大家操作redis,如下图所示 但是,大家有思考过,这 ...

  4. php tp3 操作绑定到类,快速入门 17:操作绑定到类

    # 快速入门(十七):操作绑定到类 如果你的应用规模比较大,每个操作方法彼此相对独立,那么就可以尝试下操作绑定到类的功能. ## 定义 系统提供了把每个操作方法定位到一个类的功能,可以让你的开发工作更 ...

  5. python 均值滤波_Python的一个骚操作

    PS作为世界四大发明之一可以说被广大网友用到了极致,只有你想不到的没有我P不了的,任何正经的图片在都可以变成搞笑图片(比如下图)当然也可以用ps做一点正经的事情. 作为一个爱折腾的程序猿能用代码的解决 ...

  6. C#.NET操作数据库通用类(MS SQL Server篇)

    下面给出了一个C#操作MS SQL Server 数据库的通用类,通过该类可以对数据库进行任何操作,包括执行SQL语句.执行存储过程.以下是其详细实现过程,希望大家共同修改优化之.稍后将介绍如何使用它 ...

  7. C#.NET操作数据库通用类

    下面给出了一个C#操作MS SQL Server 数据库的通用类,通过该类可以对数据库进行任何操作,包括执行SQL语句.执行存储过程.以下是其详细实现过程,希望大家共同修改优化之.稍后将介绍如何使用它 ...

  8. 【JetPack】为现有 Android 项目配置视图绑定 ( ViewBinding ) 模块 ( 视图绑定不影响传统布局操作 | 视图绑定类关联 Activity | 视图绑定类本质 )

    文章目录 I . 为现有项目配置 视图绑定 ( ViewBinding ) 应用 II . 视图绑定 ( ViewBinding ) 定制 III . 视图绑定 ( ViewBinding ) 对于正 ...

  9. [19/04/04-星期四] IO技术_CommonsIO(通用IO,别人造的轮子,FileUtils类 操作文件 IOUtilsl类 操作里边的内容 )...

    一.概念 JDK中提供的文件操作相关的类,但是功能都非常基础,进行复杂操作时需要做大量编程工作.实际开发中,往往需要 你自己动手编写相关的代码,尤其在遍历目录文件时,经常用到递归,非常繁琐. Apac ...

最新文章

  1. apache 不执行PHP,显示代码
  2. 近20万奖金:天池异常检测大赛来了!
  3. leetcode171
  4. SAP Spartacus HTTP拦截器Interceptor
  5. h3c交换机配置文件的导出
  6. java自定义表单_JSP实现用于自动生成表单标签html代码的自定义表单标签
  7. 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:离线矢量数据同步...
  8. “你在哪里上班?”“呵呵呵!”
  9. 小程序源码 租房管理系统_租房小程序解决方案
  10. 吐槽过后,我为什么要夸夸 Go 语言
  11. 存图方式---邻接表邻接矩阵前向星
  12. Python_画boxplot 盒图/箱线图
  13. Oracle10g BIGFILE表空间带来的好处
  14. 计算机考试准考证去哪下载
  15. matlab 折线图 标记_matlab画折线图标记线
  16. Python下载视频
  17. 边缘计算中任务卸载研究综述
  18. 信息安全等级保护等级划分及适用行业
  19. Android Studio设置自动换行快捷键
  20. SIMCOM 常用AT指令

热门文章

  1. C++:strcat、strcpy、strcmp、strupr、strlwr
  2. win10控制面板快捷键_常用的几个win10使用小窍门
  3. 【做题策略】考研英语一初步分析
  4. 百度推广 计算机访问url,介绍一下百度推广显示URL与访问URL
  5. Wifi自动连接评分机制
  6. 2022-2028年中国安防雷达行业发展模式分析及投资潜力研究报告
  7. 昆明理工计算机研究生分数线,昆明理工大学2018年研究生复试分数线
  8. android quicken模式,启动耗时分析(三)-ART编译分析
  9. Koa2利用koa-body实现文件上传需要注意的问题
  10. 使用Amazon S3 Python版本 连接ceph 基本操作