Connecting GPIB to an External Device Using MATLAB

Is there a way to establish a GPIB connection using MATLAB without the tool control toolbox? (I don't have it). There is also a way that MATLAB can know what the parameter values ​​of an external RS232 device are (baud rate, stop bit, etc.). For 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)

      

+2


a source to share


3 answers


For your GPIB question, is there a callable library on the GPIB card (DLL if you are on Windows)? Matlab has an interface for calling external libraries . The main procedure is for Matlab to parse the header file with LOADLIBRARY

, then view the available functions with LIBFUNCTIONS

and call the functions using CALLLIB

.



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

+1


a source


I am using National Instruments VISA and NI 488.2 .

First make sure you check VisaNS.NET API

in the NI-VISA setup, see the following figure:

enter image description here

I am using NationalInstruments.VisaNS.MessageBasedSession

through the .NET interface from MATLAB.

I wrote the following MATLAB class that brings 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 have also added some methods to handle SRQ request.

In the following code, you can control the GPIB tool, 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 via GPIB, Ethernet, or USB.

See. Also fooobar.com/questions/1118171 / ... .

+1


a source


I don't know about the RS232 parameters, but for a tcpip instrument, you can send SCPI commands very easily.

Here is an example where I am sending SCPI command to Rohde & Schwarz instances. No visa or IVI required. Use port 5025.

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

      

Then close the connection when you're done:

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

      

0


a source







All Articles