JAVA虚拟仪表VISA通信实例

电子或通信测试中,一般通过PC 控制仪表 例如 示波器,频谱仪,信号源等完成测试工作。

控制仪表前,你必须得安装虚拟仪表VISA驱动,NI 和Keysight 都提供了VISA 驱动, NI 驱动安装安装看这里
https://editor.csdn.net/md/?articleId=124800415

由于NI接口是C# C++ 的 DLL文件,因此利用java 的JNA 技术调用DLL ;实现通信,代码来源于网络,感谢源码大神;

(1)利用JNA 编写VISA32接口 ,需要下载JNA-4.0.0 jar 包;并添加JNA 在在编译路径;注意"C:\WINDOWS\system32\VISA32.dll 是NI MAX软件 中的 VISA32.dll 文件存在位置;
在这里插入图片描述

package JIN_VISA;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.LongByReference;

public interface VISA32 extends Library
{

        // VISA32 INSTANCE = (VISA32) Native.loadLibrary("VISA32",
        // VISA32.class);
        VISA32 INSTANCE = (VISA32) Native.loadLibrary(
                "C:\\WINDOWS\\system32\\VISA32.dll", VISA32.class);

        public static final long VI_NULL = 0;
        public static final long VI_SUCCESS = 0;

        public int viOpenDefaultRM(LongByReference session);

        public int viOpen(NativeLong viSession, String rsrcName,
                NativeLong accessMode, NativeLong timeout,
                LongByReference session);

        public int viClose(NativeLong vi);

        public int viScanf(NativeLong vi, String readFmt, Object... args);

        public int viPrintf(NativeLong vi, String writeFmt, Object... args);

}

(2)编写intrument类实现接口 (Instrument.java)

package JIN_VISA;

import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.LongByReference;

public class Instrument {

LongByReference defaultSession;
LongByReference vipSession;
VISA32 visa32 = VISA32.INSTANCE;

public boolean open(String deviceType, String ip)
{
   
    defaultSession = new LongByReference(0);
    int result = visa32.viOpenDefaultRM(defaultSession);
    if (result != VISA32.VI_SUCCESS) {
        return false;
    }
    
    vipSession = new LongByReference(0);
    String cmd ="";
  
    if(ip.contains("GPIB"))
    	cmd = ip+"::INSTR";
    else
     cmd = "TCPIP0::<ip>::inst0::INSTR".replace("<ip>", ip);
    NativeLong a = new NativeLong(defaultSession.getValue());
    NativeLong b = new NativeLong(0);
    result = visa32.viOpen(a, cmd, b, b, vipSession);
    if (result != VISA32.VI_SUCCESS) {
        System.out.println(result);
        return false;
    }
    return true;
}

/**
 * 关闭设备.
 * 
 * @return 成功返回true,失败返回false
 */
public boolean close() {
    NativeLong a = new NativeLong(vipSession.getValue());
    int result = VISA32.INSTANCE.viClose(a);
    if (result != VISA32.VI_SUCCESS) {
        System.out.println(result);
        return false;
    }

    NativeLong b = new NativeLong(defaultSession.getValue());
    result = VISA32.INSTANCE.viClose(b);
    if (result != VISA32.VI_SUCCESS) {
        System.out.println(result);
        return false;
    }

    return true;
}

public boolean writeCmd(String cmdStr) {
    NativeLong a = new NativeLong(vipSession.getValue());
    int result = VISA32.INSTANCE.viPrintf(a, "%s\n", cmdStr);
    if (result != VISA32.VI_SUCCESS) {
        System.out.println(result);
        return false;
    }
    return true;
}

public String readResult() {
    NativeLong a = new NativeLong(vipSession.getValue());
    Memory mem = new Memory(200);
    int result = VISA32.INSTANCE.viScanf(a, "%t", mem);
    if (result != VISA32.VI_SUCCESS) {
        System.out.println(result);
        return null;
    }
    return mem.getString(0);
}

public int scanfInstrment()
{
    NativeLong a = new NativeLong(defaultSession.getValue());
    NativeLong b = new NativeLong(0);
	int x= visa32.viScanf( a, "");
	return x;
	
}

}

(3)测试

package JIN_VISA;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main

{

public static void main(String[] args) 
{

/**   demo
	Instrument ins1=new Instrument();
	ins1.open("smw", "192.168.12.53");
	ins1.writeCmd("*IDN?");
	System.out.println(ins1.readResult());
	ins1.close();
	*/
	}
	}

****加粗样式跟新一个Git 上比较稳定的 实现:

https://github.com/pfroud/JVisa 与此工程底层一样;但是再java 14上测试过;支持多仪表

在这里插入图片描述
在这里插入图片描述

简单的使用方法是 下载 JVisa-2.0.0-with-dependencies.jar (包含JNA 的jar 包)添加到引用 ,然后直接使用

在这里插入图片描述

import xyz.froud.jvisa.JVisaException;
import xyz.froud.jvisa.JVisaInstrument;
import xyz.froud.jvisa.JVisaResourceManager;

public class TestJvisa {
    public static void main(String[] args) throws JVisaException {
        JVisaResourceManager rm = new JVisaResourceManager();
        JVisaInstrument instrument1 = rm.openInstrument("TCPIP0::169.254.10.216::inst0::INSTR");
        String response = instrument1.queryString("*IDN?");
        System.out.println(response);
        instrument1.close();
        rm.close();
    }
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值