Using Matlab with Pickering LXI drivers

Return to Matlab landing page

For information on using Pickering VISA drivers in Matlab MATLAB and VISA Driver

As the Pickering drivers are standard Windows dll's, it can be accessed from most programming languages and MATLAB like other programming languages have capability to load and run Pickering drivers to control PXI cards. Functions such as connecting/disconnecting to card(s), opening/closing cards(s) and operating switches can be performed using a set of instruction (listed below).

The functions can either be executed directly on MATLAB workspace or in function (*.m) files.


Loading Libraries

To access libraries, use the the term "loadlibrary" , here is an example to access the LXI drivers PIPLX and PICMLX:


loadlibrary('picmlx_w32', 'c:\LXI\picmlx.h');
loadlibrary('piplx_w32', 'c:\LXI\piplx.h');
%Enter appropriate pathname for dll.
    

Once the libraries are loaded, in order to check the functions in the library use "libfunctionsview" command followed by the dll's name.

Lets take an example of PICMLX.

libfunctionsview('picmlx_w32')

The following window will appear displaying information on functions in external library


Connect to an LXI device

Calllib command calls the functions in the external dll. In order to establish a connection to the LXI device use "calllib" command to address the PICMLX_Connect function


%Create a int32Ptr to get a return value on Session ID
session=libpointer('int32Ptr', 0);

% Enter the input parameters for PICMLX connect
% Enter logical numeric values for board,port and timeout
% Enter 'PXI' in place of address to read PXI cards
calllib('picmlx_w32', 'PICMLX_Connect', board, address, port, timeout, session);

%If  ans = 0 (No error returned) the device is successfully connected
SessionId=session.Value;

% Return the value session ID
    

Open Specified Card

Once the connection is established, using the PIPLX_OpenSpecifiedCard function enables user to open a specific PXI card on the chassis. The function will uses the "calllib" command to access function in the PIPLX dll. The example below illustrates


%Create a uint32Ptr to get a return value on logical card number
cardnum=libpointer('uint32Ptr', 0);

% Enter the input parameters for PIPLX_connect
% Enter the SessionId obtained when connected to PXI card
% Set the BUS and SLOT to the address of the card.
calllib ('piplx_w32', 'PIPLX_OpenSpecifiedCard', SessionId, BUS, SLOT, cardnum);

% If the ans = 0 (No error returned) the card is successfully opened
CardNum=cardnum.Value;
% Return the value of card number
    

Operating Switches

After successfully establishing the connection and opening the card, user can now able to operate switches. PIPLX_OpBit allows user to operate switch one at a time.Here is a example


% Enter the input parameters for PIPLX_OpBit
% Use Session ID and card number obtained in previous steps
% OutSub - Output Sub Unit number
% BitNum - Output Bit number
% state - "1" ON  , "0" OFF
calllib('piplx_w32', 'PIPLX_OpBit', SessionId, CardNum, OutSub, BitNum, State);
    

Close Specified Card

To close a specific card, use PIPLX_CloseSpecifiedCard function in PIPLX library.


% Enter the current session ID and card number in the input parameters
calllib (piplx_w32', 'PIPLX_CloseSpecifiedCard', SessionId, CardNum)
    

Disconnect to PXI device

To disconnect to a PXI device, use PICMLX_Disconnect function in PICMLX library.


%Enter the current session ID in the input parameter
calllib ('picmlx_w32','PICMLX_Disconnect',SessionId);        
    

Unload Libraries

To unload library use the following command


unloadlibrary('picmlx_w32');
unloadlibrary('piplx_w32');   
    

A More User Friendly Approach

In order to perform the the list of functions explained above, a set of function files (*.m files) have been developed in MATLAB which can be downloaded from here.

These MATLAB function files will use the PIPX and PICMLX library functions in an efficient way to produce a user friendly interface. Here are the examples of the MATLAB functions


Load_Libraries

It will switch off the unnecessary warnings and performs error handling.


function Load_Libraries()
 
warning off all;
 
    try
      loadlibrary('picmlx_w32','c:\LXI\picmlx.h');
      loadlibrary('piplx_w32','c:\LXI\piplx.h');
    catch
      err2= MException('Library:FailtoLoad', ...
        'DLL Failed to Load');
      throw(err2)
    end
end
    

PI_Connect

This function is establishes the connection to the LXI device. In case of an error a message will appear to check parameters instead of an error number and Session Id will be produced as an output.


function [SessionId]=PI_Connect(board, address, port, timeout, session)
 
   try
      session=libpointer('int32Ptr',0);
      calllib('picmlx_w32', 'PICMLX_Connect', board, address, port, timeout, session);
      SessionId=session.Value;
   catch
      err1= MException('Calllib:FailtoExecute', ...
        'Calling Libraries function Failed to Execute, Check Parameters');
      throw(err1)
   end
end
    

PI_OpenSpecifiedCard

Takes user input of session ID, BUS, Slot to produce a card number as a return value.


function [CardNum]=PI_OpenSpecifiedCard(SessionId, BUS, SLOT, cardnum)
 
  try
    cardnum=libpointer('uint32Ptr',0);
    err = calllib ('piplx_w32','PIPLX_OpenSpecifiedCard', SessionId, BUS, SLOT, cardnum);
    CardNum=cardnum.Value;
    if (err ~= 0)
      err=err1;
    end
  catch
    err1= MException('Calllib:FailtoOpenSpecifiedCard', ...
      'Failed to Open Card, Check Parameters');
    throw(err1)
  end
end
    

PI_OpBit

Takes input parameters to operate switches. In case of an error a message will appear instead of a number.


function PI_OpBit(SessionId, CardNum, OutSub, BitNum, State)
 
  try
    err=calllib ('piplx_w32', 'PIPLX_OpBit', SessionId, CardNum, OutSub, BitNum, State);
    if (err ~= 0)
      err=err1;
    end
  catch
    err1= MException('Calllib:FailtoOpenSpecifiedCard', ...
      'Failed to Communicate, Check Parameters');
    throw(err1)
  end
end  
    

PI_CloseSpecifiedCard

Close the specified card using the session ID and Card Number.


function PI_CloseSpecifiedCard(SessionId,CardNum)
 
  try
    err=calllib ('piplx_w32','PIPLX_CloseSpecifiedCard',SessionId,CardNum);
    if (err ~= 0)
      err=err1;
    end
  catch
    err1= MException('Calllib:FailtoOpenSpecifiedCard', ...
        'Failed to Close Card, Check Parameters');
    throw(err1)
  end
 
end    
    


PI_Disconnect

Disconnects the session


function PI_Disconnect(SessionId)
 
  try
    err=calllib ('picmlx_w32', 'PICMLX_Disconnect', SessionId);
    if (err ~= 0)
      err=err1;
    end
  catch
    err1= MException('Calllib:FailtoDisconnect', ...
      'Failed to Disconnect, Check session number');
    throw(err1)
  end
end
    

Executable commands on MATLAB workspace

Here are example executable commands on workspace which will call the above functions. Address of BUS and SLOT may vary. Input parameters will be different for each system.

(Note: in this example the string 'PXI' is used instead of an IP address, this permits control of cards in the local PXI bus.)


Load_Libraries()
 
[SessionId]=PI_Connect(1, 'PXI', 1000, 2400, 1)[CardNum]=PI_OpenSpecifiedCard(SessionId, 6, 12, 1);
PI_OpBit(SessionId, CardNum, 1, 1, 1);
PI_CloseSpecifiedCard(SessionId, CardNum);
PI_Disconnect(SessionId);
    

Need help?



Support knowledgebase

Have questions or need some help? While we enjoy talking to you, you can find technical information for our entire product range here.

Technical know-how 


Long-term support

Because we manufacture our products in-house, we are able to provide you with guaranteed long-term support and low obsolescence.

Our support


Resource center

From success stories and product videos,
to product literature and newsletters, find the information you need about our switching and simulation solutions.

Get the resources you need


Worldwide presence

We supply and support our products across the globe, with seven offices in the Americas, Europe and Asia as well as additional representation. 

Find a local office or agent near you