Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading and writing digital using a NI DAQ Device PCIe6321 with Python

Reading and writing digital using a NI DAQ Device PCIe6321 with Python has low performance. I got up to 1 kHz. Here is the code. Python steadily works out an input signal of 300 Hz and the output is also synchronously 300 Hz. When the frequency is raised to 5 kHz, chaotic signals are obtained at the output, which are not synchronous with the input. And you need to raise the input frequency to 50 kHz. Is it possible to increase the exchange rate to 50 kHz. Or is it not possible in Python? Thanks for your attention and advice.

import math
import numpy as np
import time
import nidaqmx
import nidaqmx.system
system = nidaqmx.system.System.local()
system.driver_version
for device in system.devices:
    print(device)
with nidaqmx.Task() as input_task, nidaqmx.Task() as output_task:
    input_task.di_channels.add_di_chan("Dev1/port0/line2")
    output_task.do_channels.add_do_chan("Dev1/port1/line3")
    input_task.start()
    output_task.start()
    while True:
       input_data = input_task.read(number_of_samples_per_channel=1)
       output_task.write(np.array(input_data, dtype=bool).tolist())
0 Kudos
Message 1 of 3
(1,181 Views)

You should not be reading/writing single samples, this technique has significant overhead that limits your update rate.

 

You should always be reading/writing in chunks of samples, typically 100ms worth of samples.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 2 of 3
(1,153 Views)

As Santo has mentioned, you should use sample clock timing and read/write chunks of data per API call. You can refer to my modified python-ni-examples for some references.

-------------------------------------------------------
Applications Engineer | TME Systems
https://tmesystems.net/
-------------------------------------------------------
https://github.com/ZhiYang-Ong
0 Kudos
Message 3 of 3
(1,142 Views)