代码之家  ›  专栏  ›  技术社区  ›  hkf

用MATLAB实现GPIB与外部设备的连接

  •  2
  • hkf  · 技术社区  · 16 年前

    % 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 回复  |  直到 15 年前
        1
  •  2
  •   mtrw    16 年前

    对于您的GPIB问题,GPIB卡是否带有可调用库(如果您在Windows上,则为DLL)?Matlab有一个 calling external libraries . 基本程序是让Matlab用 LOADLIBRARY ,然后使用 LIBFUNCTIONS CALLLIB .

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

        2
  •  1
  •   Wollmich    8 年前

    我在用 National Instruments VISA NI 488.2

    首先确保你检查了 VisaNS.NET API

    enter image description here

    NationalInstruments.VisaNS.MessageBasedSession 通过MATLAB的.NET接口。

    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?');
    

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

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

        3
  •  0
  •   psteffensen    7 年前

    我不知道RS232参数,但是对于一个带有tcpip的仪器,你可以很容易地发送SCPI命令。

    下面是一个示例,其中我向Rohde&发送了一个SCPI命令;施瓦茨仪器。无需签证或IVI。使用端口5025。

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

    完成后关闭连接:

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