使用MATLAB GPIB连接到外部设备

时间:2010-05-13 19:22:14

标签: matlab serial-port gpib

有没有办法使用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)

3 个答案:

答案 0 :(得分:2)

对于您的GPIB问题,GPIB卡是否附带可调用库(如果您使用的是Windows,则为DLL)? Matlab有calling external libraries的界面。基本步骤是让Matlab使用LOADLIBRARY解析头文件,然后使用LIBFUNCTIONS查看可用函数,并使用CALLLIB调用函数。

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

答案 1 :(得分:1)

我正在使用National Instruments VISANI 488.2

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

enter image description here

我在MATLAB的.NET界面上使用NationalInstruments.VisaNS.MessageBasedSession

我已经编写了以下用于将NI VISA包装到MATLAB的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

答案 2 :(得分:0)

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

在此示例中,我向Rohde&Schwarz仪器发送了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