Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

using NIDAQmx with Matlab

Hi there,

I'm trying to create a digital output with Matlab. The output of the code doesn't report any errors, but there are no signals on any pins. Before running the programm, pins are set as hight, after running the programm, the lines of the channel, which has been created ('Dev1/port1/line0:7') are set as 'low'.  So I'm sure that the channel has been created, but I don't know whats going wrong, when I'm trying to WriteDigitalLines. Here's the code....anybody any clue?

clear

all

close

all

clear

global

if

~libisloaded('nicaiu') % checks if library is loaded

loadlibrary(

'nicaiu.dll', 'NIDAQmx.h', 'mfilename', 'temp')

end

DAQmx_Val_ChanForAllLines=int32(0);

autoStart = uint32(1);

timeout = double(10);

dataLayout = uint32(0);

% writeArray = [1,0,0,1,1,0,0,1];

read=libpointer(

'int32Ptr',0);

reserved=libpointer(

'uint32Ptr',[]);

numSampsPerChannelWritten=libpointer(

'int32Ptr',0);

numSampsPerChan=int32(0);

TaskLoad=0;

if

TaskLoad

taskh1=0;

calllib(

'nicaiu','DAQmxLoadTask','SpeakerSteuer',taskh1);

else

taskh1=libpointer(

'uint32Ptr',0);

taskname=

'SpeakerSteuer';

[TaskCreated TaskName CreatePointer]=calllib(

'nicaiu','DAQmxCreateTask',taskname,taskh1); % create Task taskh1

taskh1=libpointer(

'uint32Ptr',taskh1);

[ChanCreated lines nameToAssignToLines]=calllib(

'nicaiu','DAQmxCreateDOChan',get(taskh1,'Value'),'Dev1/port1/line0:7','',DAQmx_Val_ChanForAllLines)

%%int32 DAQmxCreateAIVoltageChan (TaskHandle taskHandle, const char physicalChannel[], const char nameToAssignToChannel[], int32 terminalConfig, float64 minVal, float64 maxVal, int32 units, const char customScaleName[]);

end

StartTask = calllib(

'nicaiu','DAQmxStartTask',get(taskh1,'Value'));

[WriteDigital Data SampsChan ReserPTR] = calllib(

'nicaiu','DAQmxWriteDigitalLines', get(taskh1,'Value'), numSampsPerChan, autoStart, timeout, uint32(0), uint8(255) , numSampsPerChannelWritten, reserved)

%[y,x,c,v]=calllib('lib', function, taskHandle, int32 numSampsPerChan,bool32 autoStart, float64 timeout, bool32 dataLayout, uInt8 writeArray[],int32 *sampsPerChanWritten, bool32 *reserved)

StopTask = calllib(

'nicaiu','DAQmxStopTask',get(taskh1,'Value'))

ClearTask =calllib(

'nicaiu','DAQmxClearTask',get(taskh1,'Value'))

% unloadlibrary 'nicaiu'; % unload library

0 Kudos
Message 1 of 3
(3,640 Views)
First of all, the digital output on multifunction daq hardware form national instruments is inverted per default. This means if you want all lines to be high, you have to set 0 instead of 255. You can change this behaivour in MAX, but the default is inverted logic.
 
Second, there are some things in your code that should be changed: If you want to do static IO (no timing) you should use the function DAQmxWriteDigitalScalarU32 instead of DAQmxWriteDigitalLines.   DAQmxWriteDigitalLines is meant to transfer arrays of data to the driver, for the case you are duing buffered generation.
If you want to use the function you must set the numSampsPerChannel to 1, not 0.
 
André
 
0 Kudos
Message 2 of 3
(3,617 Views)
Thanks a lot,
 
this post was really helpful, it works now.
When Using WriteDigitalLines I also tried to set numSampsPerChannel to 1, but it didn't work (200562: DAQmxErrorInvalidDigDataWrite).
So I used :
[WriteDigital ReserPTR] = calllib('nicaiu','DAQmxWriteDigitalScalarU32', get(taskh1,'Value'), autoStart, timeout, uint32(255), reserved)
as recommended to set all pins high (I'm using a PCI 6503 card, I don't know if the pins are inverted on other cards) and it works now.
 
biiiiiiiiiiiiig THX
 
0 Kudos
Message 3 of 3
(3,607 Views)