GPIB + C编程

GPIB + C编程一个 GPIB 的操作类 采用 Adlink 的 GPIB 板卡 使用 Adlink 提供的 C 类 只是调用 DLL 封装了一下 类中包括对安捷伦数字万用表的基本操作 对固纬程控电源的基本操作和程控变阻箱的基本操作 usingSystem usingSystem Collections Generic usingSystem Text usingSystem Threading namespa

一个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 数字万用表操作集合 ///  /// 数字万用表:读取电压 ///  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; } } ///  /// 数字万用表:读取电流 ///  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; } } ///  /// 数字万用表:读取阻值 ///  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; } } ///  /// 数字万用表:读取频率 ///  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; } } ///  /// 数字万用表:设定直流电压范围 ///  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; } } ///  /// 数字万用表:恢复设置 ///  public bool DMM_Reset() { try { if (write(DMMaddr,"*RST") == false) return false; return write(DMMaddr, "*CLS"); } catch { MessageBox.Show("DMM Reset Fail!"); return false; } } ///  /// 数字万用表:设定自动调零 ///  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; } } ///  /// 数字万用表:设定触发延时 ///  public bool DMM_TrigDelay(double trigDelay) { try { return write(DMMaddr, "TRIG:DEL " + trigDelay.ToString()); } catch { MessageBox.Show("DMM Auto Zero Setting Fail!"); return false; } } ///  /// 数字万用表:设定采样点数 ///  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 程控电源操作集合 ///  /// 设置电压值 ///  public bool PWR_SetVoltage(double voltageSet) { try { return write(PWRaddr, ":CHAN1:VOLT " + voltageSet.ToString() + "/n"); } catch { MessageBox.Show("PWR Voltage Setting Fail"); return false; } } ///  /// 设置电流值 ///  public bool PWR_SetCurrent(double currentSet) { try { return write(PWRaddr, ":CHAN1:CURR " + currentSet.ToString() + "/n"); } catch { MessageBox.Show("PWR Current Setting Fail"); return false; } } ///  /// 限流保护 ///  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; } } ///  /// 限压保护 ///  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; } } ///  /// 启动输出 ///  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; } } ///  /// 获取输出状态 ///  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; } } ///  /// 获取电压 ///  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; } } ///  /// 获取电流 ///  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; } } ///  /// 恢复设置 ///  public bool PWR_Reset() { try { return write(PWRaddr, "*RST/n"); } catch { MessageBox.Show("PWR Reset Fail"); return false; } } ///  /// 清除状态 ///  public bool PWR_Clear() { try { return write(PWRaddr, "*CLS/n"); } catch { MessageBox.Show("PWR Reset Fail"); return false; } } #endregion #region 程控变阻箱操作集合 ///  /// 设置电阻值0 ///  public bool PRS0_SetResistance(ulong resSet) { try { string resString = ""; string resStringAdd = resSet.ToString(); if (resStringAdd.Length < 2) resString = "SOURce:DATA 0"; else if (resStringAdd.Length > 8) resString = "SOURce:DATA 0"; 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; } } ///  /// 设置电阻值1 ///  public bool PRS1_SetResistance(ulong resSet) { try { string resString = ""; string resStringAdd = resSet.ToString(); if (resStringAdd.Length < 2) resString = "SOURce:DATA 0"; else if (resStringAdd.Length > 8) resString = "SOURce:DATA 0"; 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 } }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221490.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午5:58
下一篇 2026年3月17日 下午5:59


相关推荐

  • [STL] list merge 函数

    [STL] list merge 函数CopyFrom: http://blog.csdn.net/ysuliu/article/details/3497862STLlist容器由于采用了双向迭代器,不支持随机访问,所以标准库的merge(),sort()等功能函数都不适用,list单独实现了merge(),sort()等函数。首先说一下merge()(以voidmerge(list&__x);为例)按照

    2022年5月23日
    40
  • AI写测试用例为什么越来越不靠谱?ChatGPT、Claude、DeepSeek 都解决不了这个问题

    AI写测试用例为什么越来越不靠谱?ChatGPT、Claude、DeepSeek 都解决不了这个问题

    2026年3月13日
    2
  • tensorflow指定版本的安装及升级到最新版「建议收藏」

    tensorflow指定版本的安装及升级到最新版「建议收藏」安装anaconda,然后用python的pip可以安装特定版本的tensorflow,如:pipinstalltensorflow-gpu==1.4.0升级到最新:GPU版本:pipinstall–upgradetensorflow-gpuCPU版本:pipinstall–upgradetensorflow如何查看当前tensorflow版本:python……

    2022年6月25日
    37
  • TFS工作项模板自定义指南

    TFS工作项模板自定义指南概述 TFS 内置有 CMMI 模板 敏捷模板等过程模板 但是为了方便团队管理 TFS 提供自定义过程模板的功能 每个团队可以根据需要自定义模板界面 字段 流程等内容 本文档将会介绍如何自定义过程模板 以及制定的要求 环境准备获取修改权限过程模板修订完成后 需要导入到对应的团队项目中 此时要求执行导入的账号需要具有该项目的管理员权限 权限由服务器管理员开通 安装工具 VS 工具 与 TFS 服务器版本对应的 VS 工具 例如 172 16 10 116 服务器是 2010 版本的 TFS 服务器 则修改模板时需要通过 2

    2026年3月16日
    2
  • 【深入Java虚拟机】之二:Class类文件结构「建议收藏」

    【深入Java虚拟机】之二:Class类文件结构「建议收藏」Java是与平台无关的语言,这得益于Java源代码编译后生成的存储字节码的文件,即Class文件,以及Java虚拟机的实现。不仅使用Java编译器可以把Java代码编译成存储字节码的Class文件,使用JRuby等其他语言的编译器也可以把程序代码编译成Class文件,虚拟机并不关心Class的来源是什么语言,只要它符合一定的结构,就可以在Java中运行。Java语言中的各种变量、关键字和运算符的语义最终都是由多条字节码命令组合而成的,因此字节码命令所能提供的语义描述能力肯定会比Java语言本身更强大,这便为

    2022年5月5日
    42
  • 关于Themleaf学习总结

    关于Themleaf学习总结此篇记录学习 Themleaf 测试的相关用例 study01Thyme 的 HelloWorld 级别的例子简单介绍 Thymeleaf 的工作流程 study02 使用 spring thymeleaf cache false 的方式让静态资源热部署 使用 devtools 插件让静态资源和 java 文件热部署 study03Thyme

    2026年3月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号