Page 1 of 1

Matlab and Phidgets 1063 interaction

Posted: Thu Oct 25, 2012 9:04 am
by zoran
I am trying to interact with a Phidgets 1063 from Matlab 2012a running under 32bit Windows7.

Controlling the stepper motor that is attached to the board works fine using the supplied stepper.m script. However, I cannot connect to the board to read the status of two digital input ports with connected switches. I have verified that things are properly connected using the Phidget Control Panel, where the digital ports indicate correct ON or OFF status when the corresponding switches are closed or open.

The Matlab script I am trying to use is listed below.

Any help on what I am doing wrong is greatly appreciated!



Matlab code:

function digitalin
% Setup path to Phidget libraries and load phidget21 Matlab library
addpath('C:\Program Files\Phidgets');
if ~libisloaded('phidget21')
loadlibrary phidget21 phidget21Matlab.h;

% Create CPhidgetHandle phid
devicePointer = libpointer('int32Ptr',0);
calllib('phidget21', 'CPhidgetStepper_create', devicePointer);
device = get(devicePointer, 'Value');

% Connect to phidget
calllib('phidget21', 'CPhidget_open', device, -1);

% Check connection
if calllib('phidget21', 'CPhidget_waitForAttachment', device, 2500) == 0
disp('Device opened')

% Check number of digital inputs
nDigitalInputs = int32(0);
calllib('phidget21', 'CPhidgetStepper_getInputCount', device, nDigitalInputs);
disp(['Number of digital inputs: ' num2str(nDigitalInputs)]);

% Check state of digital inputs
inputState = int32(0);
for i = 1:nDigitalInputs
calllib('phidget21', 'CPhidgetStepper_getInputState', device, i, inputState)
disp(['Digital input #' num2str(i) ' set to ' num2str(inputState)]);
disp('Could not open device!')

% Clean up
calllib('phidget21', 'CPhidget_close', device);
calllib('phidget21', 'CPhidget_delete', device);
disp('Device closed and deleted')

% Unloading the library too quickly causes issues.
unloadlibrary phidget21;
disp('phidget21 library unloaded')

Matlab command window output:

Device opened
Number of digital inputs: 0
Device closed and deleted
phidget21 library unloaded