Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple Digital Output Channels with Python

Solved!
Go to solution

Hello,

 

I am attempting to control a NI-9482 using Python and am running into some difficulties when trying to control all 4 channels. When I add only a single channel using:

 

DO_task.do_channels.add_do_chan("cDAQ1Mod6/port0/line0")

 

and then write a 1 to the channel to turn it on using DO_task.write(1) I get an error.

 

If I setup all 4 channels by changing line0 to line0:3 it will turn the first line on or off successfully using the write task, however if I give it a list of 4 binary values it won't work. Other python examples I've seen use a list to send digital values to multiple channels/lines but my attached code only seems to control line 0. Some pointers would be helpful.

0 Kudos
Message 1 of 3
(536 Views)
Solution
Accepted by Singus

Hello!

 

When adding DI or DO, each line needs to be written out- combining lines into one doesn’t work. Using syntax like: task.di_channels.add_di_chan("test/port1/line0:7") causes an error. In addition, the write function only takes Boolean values, 1 and 0 cause an error.

 

Do this instead for writing a DO where True is HI, and False is low, “test” is the device name:

 

with nidaqmx.Task() as task:
   task.do_channels.add_do_chan("test/port0/line0")
   task.do_channels.add_do_chan("test/port0/line1")
   task.do_channels.add_do_chan("test/port0/line2")
   level = True
   task.write([level, level, level])

 

This syntax also works if you need different values for each line. You could also define a different variable for False in the above example eg. level_0 = False - task.write([level, level_0, level])

 

with nidaqmx.Task() as task:
   task.do_channels.add_do_chan("test/port0/line0")
   level = True
   task.write(level)
with nidaqmx.Task() as task:
   task.do_channels.add_do_chan("test/port0/line1")
   level = True
   task.write(level)
with nidaqmx.Task() as task:
   task.do_channels.add_do_chan("test/port0/line2")
   level = True
   task.write(level)

 

for reading a DI, do this:

 

with nidaqmx.Task() as task:
   task.di_channels.add_di_chan("test/port1/line0")
   task.di_channels.add_di_chan("test/port1/line1")
   task.di_channels.add_di_chan("test/port1/line2")
   data = task.read()

 

The data will be a list of Boolean values. Eg: [True, True, False]. The data can be combined in a nested list if you use multiple ports. Eg: [[True, True, True], [False, False, False]].

 

I have also found that with certain devices like the PXI-6508/9, the write and read tasks need to be run twice. Running the same function twice doesn’t always work- the task needs to be written out again as a different function. This is tedious, but it works. Otherwise the task doesn’t write or read the correct values. Using the stop or close functions doesn’t seem to fix this issue. Other devices don’t seem to be affected by this, so I am not sure why this is.

I've also attached the python file- hope this helps!

 

Apex Waves Logo.png

0 Kudos
Message 2 of 3
(489 Views)

Thanks! This has helped me over a big hurdle I've been having!

0 Kudos
Message 3 of 3
(451 Views)