Question

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)
Was it helpful?

Solution

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.

OTHER TIPS

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.

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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top