Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Help sought for USB-6501 digital I/O line testing codes

Hello,
 
I am on a project changing Lab-PC+ codes to conform to new USB-6501. I tested the USB-6501 with the test panels and the example codes. They all worked fine - individula channels (lines) as well as the whole port. But when I wrote the simple test codes as below, it worked only for channel 0 - i.e. voltage changed to 0 V and 5 V when I turned the line0 bit OFF and ON. But when I changed to line1 - line7 (with proper changes in the array w-data[] elements), the voltages stay at 5 V. I wonder if someone can point me what I am doing wrong here. My test codes for line0 are shown below.  { I change w_data[1] with "Dev1/port0/line1" when I test the next bit and view the changes on the corresponding output of the 6501 with a DVM.} 
 
#include <NIDAQmx.h>
int main(int argc, char *argv[])
{
 TaskHandle taskHandle=0;
 uInt8       w_data[8]= {1,1,1,1,1,1,1,1};
  
  w_data[0] = 0;
  DAQmxCreateTask("",&taskHandle);
  DAQmxCreateDOChan(taskHandle,"Dev1/port0/line0","",DAQmx_Val_ChanForAllLines);
  DAQmxStartTask(taskHandle);
  DAQmxWriteDigitalLines(taskHandle,1,1,10.0,DAQmx_Val_GroupByChannel, w_data, NULL,NULL);
  DAQmxStopTask(taskHandle);
  DAQmxClearTask(taskHandle);
  
  w_data[0] = 1;
  DAQmxCreateTask("",&taskHandle);
  DAQmxCreateDOChan(taskHandle,"Dev1/port0/line0","",DAQmx_Val_ChanForAllLines);
  DAQmxStartTask(taskHandle);
  DAQmxWriteDigitalLines(taskHandle,1,1,10.0,DAQmx_Val_GroupByChannel, w_data, NULL,NULL);
  DAQmxStopTask(taskHandle);
  DAQmxClearTask(taskHandle);
 return 0;
}
 
0 Kudos
Message 1 of 10
(4,999 Views)
Hi jasbo,

Have you already looked at the example programs that are shipped with DAQmx?  You code is already very similar to the example program for generating digital values.  This example is located in C:\Program Files\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Digital\Generate Values (depending on where you installed DAQmx) and is called WriteDigChan.c.

/*********************************************************************
*
* ANSI C Example program:
*    WriteDigChan.c
*
* Example Category:
*    DO
*
* Description:
*    This example demonstrates how to write values to a digital
*    output channel.
*
* Instructions for Running:
*    1. Select the digital lines on the DAQ device to be written.
*    2. Select a value to write.
*    Note: The array is sized for 8 lines, if using a different
*          amount of lines, change the number of elements in the
*          array to equal the number of lines chosen.

*
* Steps:
*    1. Create a task.
*    2. Create a Digital Output channel. Use one channel for all
*       lines.
*    3. Call the Start function to start the task.
*    4. Write the digital Boolean array data. This write function
*       writes a single sample of digital data on demand, so no
*       timeout is necessary.
*    5. Call the Clear Task function to clear the Task.
*    6. Display an error if any.
*
* I/O Connections Overview:
*    Make sure your signal output terminals match the Lines I/O
*    Control. In this case wire the item to receive the signal to the
*    first eight digital lines on your DAQ Device.
*

*********************************************************************/

#include <stdio.h>
#include <NIDAQmx.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(void)
{
    int32       error=0;
    TaskHandle  taskHandle=0;
    uInt8       data[8]={0,1,0,1,1,1,1,1};
    char        errBuff[2048]={'\0'};

    /*********************************************/
    // DAQmx Configure Code
    /*********************************************/
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    DAQmxErrChk (DAQmxCreateDOChan(taskHandle,"Dev1/port0/line0:7","",DAQmx_Val_ChanForAllLines));

    /*********************************************/
    // DAQmx Start Code
    /*********************************************/
    DAQmxErrChk (DAQmxStartTask(taskHandle));

    /*********************************************/
    // DAQmx Write Code
    /*********************************************/
    DAQmxErrChk (DAQmxWriteDigitalLines(taskHandle,1,1,10.0,DAQmx_Val_GroupByChannel,data,NULL,NULL));

Error:
    if( DAQmxFailed(error) )
        DAQmxGetExtendedErrorInfo(errBuff,2048);
    if( taskHandle!=0 ) {
        /*********************************************/
        // DAQmx Stop Code
        /*********************************************/
        DAQmxStopTask(taskHandle);
        DAQmxClearTask(taskHandle);
    }
    if( DAQmxFailed(error) )
        printf("DAQmx Error: %s\n",errBuff);
    printf("End of program, press Enter key to quit\n");
    getchar();
    return 0;
}


Something to note in the above example "*    Note: The array is sized for 8 lines, if using a different
*          amount of lines, change the number of elements in the
*          array to equal the number of lines chosen."

Since you are using only one element, you can change your array size to 1.  See if that helps at all. 

Regards,
Raajit L
National Instruments
0 Kudos
Message 2 of 10
(4,973 Views)

Thanks, Raajit. Changing : " uint8  w_data[1] = {1} " works. So it is the array usage that I got confused in that call.  I will check any cross-talk to adjacent channels using a scope to see if any interference exists. I anticipate no cross-talk. If I see anything, I will report back here.  Again thank you very much for your reply. 

0 Kudos
Message 3 of 10
(4,959 Views)

I also have the same problem.

 

Port access is OK, bit0 access is also OK, turn on/off correctly when I write 1/0 to bit0, but other bits behave strangely, no rules can be summarized.

 

Does anyone have any idea?

 

Thanks in advance. 

0 Kudos
Message 4 of 10
(4,437 Views)

Lily311,

 

When you say they behave starangely what do you mean?  Are you writing a value to each of the bits?  Do have your array configured properly as mentioned above?  Can you describe to us what you are doing?

 

Sincerely,
Jason Daming
Applications Engineer
National Instruments
http://www.ni.com/support
0 Kudos
Message 5 of 10
(4,423 Views)

Hi, Jason,

 

Thanks for reply.

 

Basically when I write 1 to each bit, such as using DAQmxBaseWriteDigitalU8 to write data uInt8 0x01 for bit 0, uInt8 0x02 for bit 1, uInt8 0x04 for bit 2, etc, it works fine.

But when I write 0 to any one bit, all the other bits are unexpectedly cleared to 0. 

0 Kudos
Message 6 of 10
(4,417 Views)

Lily311,

 

You mentioned that bit 0 seemed to work?  Are you writing 8 bits or just 1?  Can you post the code in question?  Can you run the WriteDigPort.c example code (seems to work for me)?

Sincerely,
Jason Daming
Applications Engineer
National Instruments
http://www.ni.com/support
0 Kudos
Message 7 of 10
(4,406 Views)

I've said at my first post that "Port access is OK", I just have problem in bit access. The example you mentioned is port access, I think.

 

bit0 access actually have the same problem, I just didn't notice at that time that writing 0 to one bit will affect all others bits.

 

My code snippet, which writes bit by bit to a 8 bit port. 

Note: Totally I create 8 tasks for each bit of that port. 

 

for (int i=0; i<8; i++)

{

int nPos = 7-i;

if ([regStr characterAtIndex:nPos] == '0')

{

w_data[0] = 0;

DAQmxErrChk (DAQmxBaseWriteDigitalU8(taskHandleB[i],1,1,1.0,DAQmx_Val_GroupByChannel,w_data,&written,NULL));

}

else if ([regStr characterAtIndex:nPos] == '1')

{

w_data[0] = pow(2, i);

DAQmxErrChk (DAQmxBaseWriteDigitalU8(taskHandleB[i],1,1,1.0,DAQmx_Val_GroupByChannel,w_data,&written,NULL));

}

}

 

Thanks & Regards,

Echo 

0 Kudos
Message 8 of 10
(4,399 Views)

Echo,

 

I have unfortunate news.  According to the DAQmx Base C Refrence Help on DAQmxBaseCreateDOChan, which you need to perform the output: "NI-DAQmx Base supports grouping the digital lines of a port as a single channel, not multiple channels."

Sincerely,
Jason Daming
Applications Engineer
National Instruments
http://www.ni.com/support
0 Kudos
Message 9 of 10
(4,388 Views)

Thanks anyway.

 

Regards,

Echo 

0 Kudos
Message 10 of 10
(4,379 Views)