首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MATLAB中将GPIB转换成PyVISA

在MATLAB中将GPIB转换成PyVISA
EN

Stack Overflow用户
提问于 2013-09-19 19:36:29
回答 1查看 645关注 0票数 1

我继承了一些用于通过GPIB连接编程XYZ阶段的MATLAB代码。为了使它与Python中的一些现有代码更兼容,我需要以某种方式翻译它,例如使用PyVISA包。我真的很想在这方面有所帮助!

所以,到目前为止,我得到的只是基本的东西,

代码语言:javascript
复制
from visa import *
stage = instrument("GPIB::2")

由此,我可以使用标识命令并正确获取我的设备的ID:

代码语言:javascript
复制
stage.write("*IDN?")

那么,您知道如何将下面的MATLAB转换成适当的PyVISA命令吗?我最大的问题是我不知道怎么翻译语法..。

代码语言:javascript
复制
classdef cascade12000b < handle
    properties(Constant)
        GPIB_ADDRESS = 28;
        DEVICE_TAG = 'Cascade 12000B Probe Station';
        DEVICE_ID = 2;
    end

    properties
        gpib_conn;
    end

    methods
        function [obj] = cascade12000b()
            obj.open();
        end

        function [x, y, z] = get_position(obj)
            [r] = obj.exec_command(sprintf(':MOV:ABS? %d', cascade12000b.DEVICE_ID));
            tmp = sscanf(r, '%d %d %d');
            x = tmp(1);
            y = tmp(2);
            z = tmp(3);
        end

        function [] = move_absolute(obj, x, y)
            [~, ~, z] = obj.get_position();
            obj.exec_command(sprintf(':MOV:ABS %d %d %d %d', cascade12000b.DEVICE_ID, x, y, z));
        end

        function [] = move_relative(obj, dx, dy)
            obj.exec_command(sprintf(':MOV:REL %d %d %d %d', cascade12000b.DEVICE_ID, dx, dy, 0));
        end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-24 13:08:16

像这样的东西会起作用

代码语言:javascript
复制
class Cascade12000b(object):
    """A Cascade12000b driver.

    :param connection: A connection object, used to communicate with the real device.
        The connection interface should conform to the following interface.
        It must have two methods:

        * `.write()` taking a string message
        * `.ask()` taking a string message, returning a string response

    :param int id: The device id

    """
    def __init__(self, connection, id=2):
        self.connection = connection
        self.id = int(id)

    def position(self):
        """Returns a tuple `(x,y,z)` with the position coordinates."""
        response = self.connection.ask(':MOV:ABS? {0:d}'.format(self.id))
        # assuming whitespace separated response
        return tuple(int(x) for x in reponse.split())

    def move_absolute(self, x, y, z=None):
        """Sets the position in absolute coordinates."""
        if z is None:
            _, _, z = self.position()
        self.connection.write(':MOV:ABS {0:d} {1:d} {2:d} {3:d}'.format(self.id, x, y, z)

    def move_relative(self, dx, dy, dz=0):
        """Sets the position in relative coordinates."""
        self.connection.write(':MOV:REL {0:d} {1:d} {2:d} {3:d}'.format(self.id, dx, dy, dz)

你会像这样用它

代码语言:javascript
复制
# Injecting the connection has the advantage that you can change the implementation, e.g. # to linux-gpib
>>>connection = visa.instrument('GPIB::28')
>>>device = Cascade12000b(connection)
>>>device.move_absolute(10, 13, 20)
>>>device.position()
10, 13, 20
>>>device.move_relative(2,2)
>>>device.position()
12,15,20

如果您必须编写多个设备驱动程序,您可能需要查看一些python包,如从站 (注意:我是“从”的作者)或兰茨

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18903446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档