[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]使用JNA调用Visa32.dll,控制频谱仪的大体代码~~

上一篇:[备忘]注意,URLEncode值作为post内容提交时,需要过滤4个字母~~
下一篇:[转帖]一个好使的dom ready的javascript代码~~

添加日期:2012/8/23 16:32:56 快速返回   返回列表 阅读5707次


package jna;

import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

public class Instrument {

    /**
     * JNA接口定义.
     * 
     */
    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 int VI_EVENT_IO_COMPLETION = 0x3FFF2009;

        public static final int VI_TMO_INFINITE = 0xFFFFFFFF;
        public static final short VI_QUEUE = 1;
        public static final int VI_NULL = 0;
        public static final int VI_SUCCESS = 0;
        public static final int VI_SUCCESS_SYNC = 0x3FFF009B;

        public static final short VI_READ_BUF = 1;
        public static final short VI_WRITE_BUF = 2;

        public static final int VI_ATTR_JOB_ID = 0x3FFF4006;
        public static final int VI_ATTR_RET_COUNT = 0x3FFF4026;

        public int viOpenDefaultRM(IntByReference session);

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

        public int viStatusDesc(int vi, int status, Pointer desc);

        public int viClear(int vi);

        public int viEnableEvent(int vi, int eventType, short mechanism,
                int context);

        public int viDisableEvent(int vi, int eventType, short mechanism);

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

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

        public int viClose(int vi);

        public int viFlush(int vi, short mask);

        public int viReadAsync(int vi, Pointer buf, int count,
                IntByReference jobId);

        public int viWaitOnEvent(int vi, int inEventType, int timeout,
                IntByReference outEventType, IntByReference outContext);

        public int viGetAttribute(int vi, int attribute,
                IntByReference attrState);
    }

    IntByReference defaultSession;
    IntByReference vipSession;

    /**
     * 取得错误信息.
     * 
     * @param status
     *            状态码
     */
    public void TraceErr(int status) {
        Memory mem = new Memory(200);
        VISA32.INSTANCE.viStatusDesc(vipSession.getValue(), status, mem);
        System.out.println(mem.getString(0));
    }

    /**
     * 连接仪器.
     * 
     * @param deviceType
     *            仪器类型
     * @param ip
     *            仪器IP地址
     * @return 连接成功返回true,否则返回false
     */
    public boolean open(String deviceType, String ip) {
        VISA32 visa32 = VISA32.INSTANCE;

        // 取得Default Resource Manager
        defaultSession = new IntByReference(0);
        int result = visa32.viOpenDefaultRM(defaultSession);
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return false;
        }

        // JNA字符串转换的默认编码
        // System.setProperty("jna.encoding", "ascii");
        // String jnaEncoding = System.getProperty("jna.encoding");
        // System.out.println(jnaEncoding);

        // 创建连接
        vipSession = new IntByReference(0);
        String cmd = "TCPIP0::<ip>::inst0::INSTR".replace("<ip>", ip);
        result = visa32.viOpen(defaultSession.getValue(), cmd, VISA32.VI_NULL,
                VISA32.VI_NULL, vipSession);
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return false;
        }

        // 清扫战场
        result = visa32.viClear(vipSession.getValue());
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return false;
        }

        // 允许事件
        result = visa32.viEnableEvent(vipSession.getValue(),
                VISA32.VI_EVENT_IO_COMPLETION, VISA32.VI_QUEUE, VISA32.VI_NULL);
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return false;
        }

        // 复位
        if (!writeCmd("*RST")) {
            return false;
        }

        // 清扫
        if (!writeCmd("*CLS")) {
            return false;
        }

        return true;
    }

    /**
     * 发送命令到仪器.
     * 
     * @param cmdStr
     *            命令串
     * @return 成功返回true,否则返回false
     */
    public boolean writeCmd(String cmdStr) {
        int result = VISA32.INSTANCE.viPrintf(vipSession.getValue(), "%s\n",
                cmdStr);
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return false;
        }
        return true;
    }

    /**
     * 从仪器接收数据(该方法会超时,需要时间较长的查询命令应该使用query方法).
     * 
     * @return 接收到的数据字符串(包含\n).
     */
    public String readResult() {
        Memory mem = new Memory(200);
        int result = VISA32.INSTANCE.viScanf(vipSession.getValue(), "%t", mem);
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return null;
        }
        return mem.getString(0);
    }

    /**
     * 发送查询命令到仪器,并接收返回数据.
     * 
     * @param cmdStr
     *            查询命令
     * @return 从仪器接收到的数据字符串(包含\n),未接收到则返回null.
     */
    public String query(String cmdStr) {

        VISA32 visa32 = VISA32.INSTANCE;

        // 提交缓冲区
        int result = visa32.viFlush(vipSession.getValue(),
                (short) (VISA32.VI_WRITE_BUF | VISA32.VI_READ_BUF));
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return null;
        }

        // 发送命令到仪器
        if (!writeCmd(cmdStr)) {
            return null;
        }

        // 提交缓冲区
        result = visa32.viFlush(vipSession.getValue(), VISA32.VI_WRITE_BUF);
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return null;
        }

        // 异步读取
        Memory mem = new Memory(600);
        IntByReference JobId_Read = new IntByReference(0);
        result = visa32
                .viReadAsync(vipSession.getValue(), mem, 600, JobId_Read);
        if (result != VISA32.VI_SUCCESS_SYNC) {
            TraceErr(result);
            return null;
        }

        // 等待事件完成
        IntByReference eventType = new IntByReference(0);
        IntByReference event = new IntByReference(0);
        result = visa32.viWaitOnEvent(vipSession.getValue(),
                VISA32.VI_EVENT_IO_COMPLETION, VISA32.VI_TMO_INFINITE,
                eventType, event);
        if (result != VISA32.VI_SUCCESS) {
            TraceErr(result);
            return null;
        }

        // 读取JobID和retCount
        IntByReference JobId_Wait = new IntByReference(0);
        result = visa32.viGetAttribute(event.getValue(), VISA32.VI_ATTR_JOB_ID,
                JobId_Wait);

        IntByReference retCount = new IntByReference(0);
        result = visa32.viGetAttribute(event.getValue(),
                VISA32.VI_ATTR_RET_COUNT, retCount);

        // 关闭事件
        visa32.viClose(event.getValue());

        // 不符合条件,返回null
        if ((JobId_Wait.getValue() != JobId_Read.getValue())
                || (retCount.getValue() <= 0)) {

            return null;
        }

        // 返回接收到的数据
        return mem.getString(0);
    }

    /**
     * 关闭仪器.
     * 
     * @return 成功返回true,失败返回false
     */
    public boolean close() {

        // 关闭事件
        VISA32.INSTANCE.viDisableEvent(vipSession.getValue(),
                VISA32.VI_EVENT_IO_COMPLETION, VISA32.VI_QUEUE);

        // 关闭仪器连接
        int result = VISA32.INSTANCE.viClose(vipSession.getValue());
        if (result != VISA32.VI_SUCCESS) {
            return false;
        }

        // 关闭Default Resource Manager
        result = VISA32.INSTANCE.viClose(defaultSession.getValue());
        if (result != VISA32.VI_SUCCESS) {
            return false;
        }

        return true;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        Instrument device = new Instrument();
        boolean isOpen = device.open("xxx", "192.168.1.10");
        if (!isOpen) {
            System.out.println("open failed.");
            return;
        }

        // boolean result = device.writeCmd(":SENS:FREQ:CENT 400MHZ");
        // System.out.println(result);
        //
        // result = device.writeCmd(":SENS:FREQ:SPAN 10MHZ");
        // System.out.println(result);

        // String s = device.query("CALC:MARK:X?");
        // System.out.println(s);

        boolean result = device.writeCmd("*IDN?");
        System.out.println(result);

        String s = device.readResult();
        System.out.println(s);

        result = device.writeCmd(":SENS:FREQ:CENT 400MHZ");
        System.out.println(result);

        String b = device.query("CALC:MARK:Y?");
        System.out.println(b);

        device.close();

    }

}

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved