使用 MATLAB 与外部设备的 GPIB 连接

发布于 2024-09-02 03:50:10 字数 1117 浏览 6 评论 0原文

有没有办法使用 MATLAB 建立 GPIB 连接,而无需仪器控制工具箱? (我没有)。还有一种方法可以让 MATLAB 了解外部设备的 RS232 参数值(波特率、停止位等)。对于 RS232 连接,我有以下代码:

% This function is meant to send commands to Potentiostat Model 263A.

% A run includes turning the cell on, reading current for time t1, turning

% the cell off, waiting for time t2.

% t1 is the duration [secs] for which the Potentiostat must run (cell is on)

% t2 is the duration [secs] to on after off

% n is the number of runs 

% port is the serial port name such as COM1

function [s] = Potentiostat_control(t1,t2,n)

port = input('type port name such as COM1', 's')

s = serial(port);

set(s,'BaudRate', 9600, 'DataBits', 8, 'Parity', 'even', 'StopBits', 2 ,'Terminator', 'CR/LF'); 

fopen(s)

%fprintf(s,'RS232?')

disp(['Total runs requested = ' num2str(n)]) 

disp('i denotes number of runs executed so far..');

for i=1:n

    i

    %data1 = query(s, '*IDN?')

    fprintf(s,'%s','CELL 1'); % sends the command 'CELL 1'

    %fprintf(s,'%s','READI');

    pause(t1);

    fprintf(s,'%s','CELL 0');

    %fprintf(s,'%s','CLEAR');

    pause(t2);

end

fclose(s)

Is there a way to establish a GPIB connection using MATLAB without the instrument control Tool box? (I don't have it). Also is there a way for MATLAB to know what the external device's RS232 parameter values are ( Baud rate, stop bit etc..). For the RS232 connection I have the following code:

% This function is meant to send commands to Potentiostat Model 263A.

% A run includes turning the cell on, reading current for time t1, turning

% the cell off, waiting for time t2.

% t1 is the duration [secs] for which the Potentiostat must run (cell is on)

% t2 is the duration [secs] to on after off

% n is the number of runs 

% port is the serial port name such as COM1

function [s] = Potentiostat_control(t1,t2,n)

port = input('type port name such as COM1', 's')

s = serial(port);

set(s,'BaudRate', 9600, 'DataBits', 8, 'Parity', 'even', 'StopBits', 2 ,'Terminator', 'CR/LF'); 

fopen(s)

%fprintf(s,'RS232?')

disp(['Total runs requested = ' num2str(n)]) 

disp('i denotes number of runs executed so far..');

for i=1:n

    i

    %data1 = query(s, '*IDN?')

    fprintf(s,'%s','CELL 1'); % sends the command 'CELL 1'

    %fprintf(s,'%s','READI');

    pause(t1);

    fprintf(s,'%s','CELL 0');

    %fprintf(s,'%s','CLEAR');

    pause(t2);

end

fclose(s)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏见 2024-09-09 03:50:10

对于您的 GPIB 问题,GPIB 卡是否附带可调用库(如果您使用的是 Windows,则为 DLL)? Matlab 有一个调用外部库的接口。基本过程是让 Matlab 使用 LOADLIBRARY 解析头文件,然后使用 LIBFUNCTIONS 查看可用函数并使用 CALLLIB 调用函数。

对于您的RS232问题,我认为在没有外部文档的情况下,主机端没有任何方法可以了解设备端的参数。

For your GPIB question, does the GPIB card come with a callable library (DLL if you're on Windows)? Matlab has an interface for calling external libraries. The basic procedure is to have Matlab parse the header file using LOADLIBRARY, then view the available functions using LIBFUNCTIONS and call functions using CALLLIB.

For your RS232 question, I don't think there's any way for the host side to know the device side's parameters without external documentation.

十二 2024-09-09 03:50:10

我使用的是 National Instruments VISANI 488.2

首先确保您在 NI-VISA 设置中检查了 VisaNS.NET API,请参见下图:

在此处输入图像描述

我正在使用 NationalInstruments.VisaNS。 MessageBasedSession 通过 MATLAB 的 .NET 接口。

我编写了以下 MATLAB 类,将 NI VISA 包装到 MATLAB:

classdef Visa
    properties
        vi
        SrqMask
        SrqTimeout
    end
    methods
        function obj = Visa(resourceName)
            NET.addAssembly('NationalInstruments.VisaNS');
            obj.vi = NationalInstruments.VisaNS.MessageBasedSession(resourceName);
            obj.SrqMask = '*CLS;*ESE 1;*SRE 32';
            obj.SrqTimeout = 10000;
        end
        function obj = delete(obj)
            obj.vi.Dispose();
        end
        function obj = Dispose(obj)
            obj.vi.Dispose();
        end
        function obj = Write(obj, data)
            obj.vi.Write(data);
        end
        function data = ReadString(obj)
            data = char(obj.vi.ReadString());
        end
        function data = ReadByteArray(obj)
            data = obj.vi.ReadByteArray();
        end
        function data2 = Query(obj, data)
            data2 = char(obj.vi.Query(data));
        end
        function obj = SrqBegin(obj)
            obj.vi.EnableEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest, ...
                NationalInstruments.VisaNS.EventMechanism.Queue);
            obj.vi.DiscardEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest);
            obj.Write(obj.SrqMask);
        end
        function status = SrqEnd(obj)
            evt = obj.vi.WaitOnEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest, ...
                obj.SrqTimeout);
            evt.Dispose();
            status = obj.vi.ReadStatusByte();
            obj.vi.DisableEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest, ...
                NationalInstruments.VisaNS.EventMechanism.Queue);           
        end
        function obj = SrqWrite(obj, data)
            obj.SrqBegin();
            obj.Write(data);
            obj.SrqEnd();
        end
        function data2 = SrqQuery(obj, data)
            obj.SrqBegin();
            obj.Write(data);
            obj.SrqEnd();
            data2 = obj.ReadString();
        end
    end
end

我还添加了一些方法来处理 SRQ 请求。

例如,您可以使用以下代码控制 GPIB 仪器:

resourceName = 'GPIB0::20::INSTR'; % GPIB adapter 0, Instrument address 20
vi = Visa(resourceName);
idn = vi.QueryString('*IDN?');

MessageBasedSession 可用于通过 GPIB、以太网或 USB 与您的仪器进行通信。

另请参阅https://stackoverflow.com/a/49388678/7556646

I'm using National Instruments VISA and NI 488.2.

First make sure that you checked the VisaNS.NET API in the NI-VISA Setup, see the following figure:

enter image description here

I'm using the NationalInstruments.VisaNS.MessageBasedSession over the .NET interface from MATLAB.

I've written the following MATLAB class that wraps NI VISA to MATLAB:

classdef Visa
    properties
        vi
        SrqMask
        SrqTimeout
    end
    methods
        function obj = Visa(resourceName)
            NET.addAssembly('NationalInstruments.VisaNS');
            obj.vi = NationalInstruments.VisaNS.MessageBasedSession(resourceName);
            obj.SrqMask = '*CLS;*ESE 1;*SRE 32';
            obj.SrqTimeout = 10000;
        end
        function obj = delete(obj)
            obj.vi.Dispose();
        end
        function obj = Dispose(obj)
            obj.vi.Dispose();
        end
        function obj = Write(obj, data)
            obj.vi.Write(data);
        end
        function data = ReadString(obj)
            data = char(obj.vi.ReadString());
        end
        function data = ReadByteArray(obj)
            data = obj.vi.ReadByteArray();
        end
        function data2 = Query(obj, data)
            data2 = char(obj.vi.Query(data));
        end
        function obj = SrqBegin(obj)
            obj.vi.EnableEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest, ...
                NationalInstruments.VisaNS.EventMechanism.Queue);
            obj.vi.DiscardEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest);
            obj.Write(obj.SrqMask);
        end
        function status = SrqEnd(obj)
            evt = obj.vi.WaitOnEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest, ...
                obj.SrqTimeout);
            evt.Dispose();
            status = obj.vi.ReadStatusByte();
            obj.vi.DisableEvent(NationalInstruments.VisaNS.MessageBasedSessionEventType.ServiceRequest, ...
                NationalInstruments.VisaNS.EventMechanism.Queue);           
        end
        function obj = SrqWrite(obj, data)
            obj.SrqBegin();
            obj.Write(data);
            obj.SrqEnd();
        end
        function data2 = SrqQuery(obj, data)
            obj.SrqBegin();
            obj.Write(data);
            obj.SrqEnd();
            data2 = obj.ReadString();
        end
    end
end

I've added as well some methods to handle the SRQ request.

With the following code you can control GPIB instrument for example:

resourceName = 'GPIB0::20::INSTR'; % GPIB adapter 0, Instrument address 20
vi = Visa(resourceName);
idn = vi.QueryString('*IDN?');

A MessageBasedSession can be used to communicate with your instrument over GPIB, Ethernet or USB.

See as well https://stackoverflow.com/a/49388678/7556646.

_失温 2024-09-09 03:50:10

我不知道RS232参数,但是对于带有tcpip的仪器,您可以非常轻松地发送SCPI命令。

以下是我向罗德与施瓦茨仪器发送 SCPI 命令的示例。无需 VISA 或 IVI。使用端口 5025。

t = tcpip('147.214.90.136', 5025); 
fopen(t); 
fprintf(t, '*IDN?');
fprintf(1, DataReceived)

完成后关闭连接:

fclose(t); 
delete(t); 
clear t 

I don't know about the RS232 parameters, but for an instrument with tcpip, you can send SCPI commands really easy.

Here is an example where I send a SCPI command to a Rohde&Schwarz instuments. No VISA or IVI needed. Use port 5025.

t = tcpip('147.214.90.136', 5025); 
fopen(t); 
fprintf(t, '*IDN?');
fprintf(1, DataReceived)

Then close the connection when you are done:

fclose(t); 
delete(t); 
clear t 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文