Main Content

Transition Your Code to VISA-GPIB Interface

The gpib function, its object functions, and its properties will be removed. Use the VISA-GPIB interface with visadev instead.

gpib Interfacevisadev InterfaceExample
instrhwinfovisadevlistDiscover GPIB Instruments
visavisadevConnect to GPIB Instrument
fwrite and freadwrite and readWrite and Read Binary or String Data
fprintfwritelineRead Terminated String
fscanf, fgetl, and fgetsreadlineRead Terminated String
Read and Parse String Data
querywritereadWrite and Read Back Data
binblockwrite and binblockreadwritebinblock and readbinblockWrite and Read Binblock Data
flushinput, flushoutput, and clrdeviceflushFlush Data from Memory
TerminatorconfigureTerminatorSet Terminator
BytesAvailableFcnCount, BytesAvailableFcnMode, BytesAvailableFcn, and BytesAvailableconfigureCallbackSet Up Callback Function
spollvisastatus 
triggervisatrigger 
gpib Propertiesvisadev Properties 

Discover GPIB Instruments

This example shows how to discover GPIB instruments using the recommended functionality.

FunctionalityUse This Instead
instrhwinfo('gpib','ni')
list = visadevlist;
list.ResourceName(list.Type=="gpib")

For more information, see visadevlist.

Connect to GPIB Instrument

These examples show how to connect to a GPIB instrument and disconnect from it using the recommended functionality.

FunctionalityUse This Instead
g = gpib('ni',0,1)
fopen(g)
g = visadev("GPIB0::1::0::INSTR");
fclose(g)
delete(g)
clear g
clear g

For more information, see visadev.

Write and Read Binary or String Data

These examples show how to perform a binary write and read, and how to write and read nonterminated string data, using the recommended functionality.

FunctionalityUse This Instead
% g is a gpibobject
fwrite(g,1:5)
data = fread(g,5)
data =

     1
     2
     3
     4
     5
% g is a visadev object
write(g,1:5)
data = read(g,5)
data =

     1     2     3     4     5
% g is a gpib object
fwrite(g,'hello','char')
length = 5;
data = fread(g,length,'char')
data =

   104
   101
   108
   108
   111
data = char(data)'
data =

    'hello'
% g is a visadev object
write(g,"hello","string")
length = 5;
data = read(g,length,"string")
data =

    "hello"

For more information, see write or read.

Read Terminated String

This example shows how to perform a terminated string write and read using the recommended functionality.

FunctionalityUse This Instead
% g is a gpib object
g.Terminator = 'CR/LF';
fprintf(g,'SOUR:%d:FREQ',ch)
data = fscanf(g,'%e')
data =

    11.9000
% g is a visadev object
configureTerminator(g,"CR/LF")
str = sprintf("SOUR:%d:FREQ",ch)
writeline(g,str)
data = readline(g)
data = 

    "11.9000"
data = sscanf(data,'%e')
data = 

    11.9000
% g is a gpib object
g.Terminator = 'CR/LF';
fprintf(g,'hello')
data = fgetl(g)
data =

    'hello'

fgetl reads until the specified terminator is reached and then discards the terminator.

% g is a visadev object
configureTerminator(g,"CR/LF")
writeline(g,"hello")
data = readline(g)
data = 

    "hello"
% g is a gpib object
g.Terminator = 'CR/LF';
fprintf(g,'hello')
data = fgets(g)
data =

    'hello
     '

fgets reads until the specified terminator is reached and then returns the terminator.

For more information, see writeline or readline.

Read and Parse String Data

This example shows how to read and parse string data using the recommended functionality.

FunctionalityUse This Instead
% g is a gpib object
data = scanstr(g,';')
data =

  3×1 cell array

    {'a'}
    {'b'}
    {'c'}
% g is a visadev object
data = readline(g)
data = 

    "a;b;c"
data = strsplit(data,";")
data = 

  1×3 string array

    "a"    "b"    "c"

For more information, see readline.

Write and Read Back Data

This example shows how to write ASCII terminated data and read ASCII terminated data back using the recommended functionality.

FunctionalityUse This Instead
% g is a gpib object
data = query(g,'ctrlcmd')
data =

    'success'
% g is a visadev object
data = writeread(g,"ctrlcmd")
data = 

    "success"

For more information, see writeline or readline.

Write and Read Binblock Data

This example shows how to write data with the IEEE standard binary block protocol using the recommended functionality.

FunctionalityUse This Instead
% g is a gpib object
binblockwrite(g,1:5);
data = binblockread(g)
data =

     1
     2
     3
     4
     5
% g is a visadev object
writebinblock(g,1:5)
data = readbinblock(g)
data =

     1     2     3     4     5

For more information, see writebinblock or readbinblock.

Flush Data from Memory

This example shows how to flush data from the buffer using the recommended functionality.

FunctionalityUse This Instead
% g is a gpib object
flushinput(g)
clrdevice(g)
% g is a visadev object
flush(g,"input")
% g is a gpib object
flushoutput(g)
clrdevice(g)
% g is a visadev object
flush(g,"output")
% g is a gpib object
flushinput(g)
flushoutput(g)
clrdevice(g)
% g is a visadev object
flush(g)

For more information, see flush.

Set Terminator

These examples show how to set the terminator using the recommended functionality.

FunctionalityUse This Instead
% g is a gpib object
g.Terminator = "CR/LF";
% g is a visadev object
configureTerminator(g,"CR/LF")
% g is a gpib object
g.Terminator = {"CR/LF" [10]};
% g is a visadev object
configureTerminator(g,"CR/LF",10)

For more information, see configureTerminator.

Set Up Callback Function

These examples show how to set up a callback function using the recommended functionality.

FunctionalityUse This Instead
% g is a gpib object
g.BytesAvailableFcnCount = 5
g.BytesAvailableFcnMode = "byte"
g.BytesAvailableFcn = @mycallback

function mycallback(src,evt)
   data = fread(src,src.BytesAvailableFcnCount);
   disp(evt)
   disp(evt.Data)
end
    Type: 'BytesAvailable'
    Data: [1×1 struct]

    AbsTime: [2019 12 21 16 35 9.7032]
% g is a visadev object
configureCallback(g,"byte",5,@mycallback);
function mycallback(src,evt)
   data = read(src,src.BytesAvailableFcnCount);
   disp(evt)
end
  ByteAvailableInfo with properties:

    BytesAvailableFcnCount: 5
                   AbsTime: 21-Dec-2019 12:23:01
% g is a gpib object
g.Terminator = "CR"
g.BytesAvailableFcnMode = "terminator"
g.BytesAvailableFcn = @mycallback

function mycallback(src,evt)
   data = fscanf(src);
   disp(evt)
   disp(evt.Data)
end
    Type: 'BytesAvailable'
    Data: [1×1 struct]

    AbsTime: [2019 12 21 16 35 9.7032]
% g is a visadev object
configureTerminator(g,"CR")
configureCallback(g,"terminator",@mycallback)

function mycallback(src,evt)
   data = readline(src);
   disp(evt)
end
  TerminatorAvailableInfo with properties:

                   AbsTime: 21-Dec-2019 12:23:01

For more information, see configureCallback.

See Also

Related Topics