Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example / MultiChannelAnalogInput.py is synchronization for every Channel #15

Open
rosickey opened this issue Jun 24, 2014 · 2 comments

Comments

@rosickey
Copy link

the example is synchronization for every Channel?

@zfaizal
Copy link

zfaizal commented Jul 17, 2015

What exactly are yo asking? It creates taskhandles for every channel.

@CPHLIVE
Copy link

CPHLIVE commented Oct 5, 2015

Hi! i have a question which i think is similar to this thread.

I am working with a NI Compact DAQ with a 9222, a 4 channel high freq voltage input. So far i have succeed in reading in the data for each channel, one at the time. But what i want to achieve is to read the channels simultaneously in sync. Can some of your wizards in here help me with this? I have copied my code below.

Please bear with me, i am fairly new to Python.

Best

Jeppe

import numpy
from PyDAQmx.DAQmxFunctions import *
from PyDAQmx.DAQmxConstants import *
import time
class MultiChannelAnalogInput():
"""Class to create a multi-channel analog input

Usage: AI = MultiChannelInput(physicalChannel)
    physicalChannel: a string or a list of strings
optional parameter: limit: tuple or list of tuples, the AI limit values
                    reset: Boolean
Methods:
    read(name), return the value of the input name
    readAll(), return a dictionary name:value
"""

#==============================================================================

Initate analog channels

#==============================================================================

def __init__(self,physicalChannel, limit = None, reset = False):
    if type(physicalChannel) == type(""):
        self.physicalChannel = [physicalChannel]
    else:
        self.physicalChannel  =physicalChannel
    self.numberOfChannel = physicalChannel.__len__()

    if limit is None:
        self.limit = dict([(name, (-5.0,5.0)) for name in self.physicalChannel])
    elif type(limit) == tuple:
        self.limit = dict([(name, limit) for name in self.physicalChannel])
    else:
        self.limit = dict([(name, limit[i]) for  i,name in enumerate(self.physicalChannel)])  

    if reset:
        DAQmxResetDevice(physicalChannel[0].split('/')[0] )

        self.BufferSize = 10

#==============================================================================

#==============================================================================
def configure(self):
# Create one task handle per Channel
taskHandles = dict([(name,TaskHandle(0)) for name in self.physicalChannel])
for name in self.physicalChannel:
DAQmxCreateTask("",byref(taskHandles[name]))
DAQmxCreateAIVoltageChan(taskHandles[name],name,"",DAQmx_Val_Diff,
self.limit[name][0],self.limit[name][1],
DAQmx_Val_Volts,None)

    self.taskHandles = taskHandles
def readAll(self):
    return dict([(name,self.read(name)) for name in self.physicalChannel])

def read(self,name = None):
    start_time = time.time()

    dataSize = 833
    data = numpy.zeros((dataSize,), dtype=numpy.float64)
    if name is None:
        name = self.physicalChannel[0]
    taskHandle = self.taskHandles[name]  
    DAQmxCfgSampClkTiming(taskHandle,"",dataSize,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,dataSize)         
    DAQmxStartTask(taskHandle)

data = AI_data_type()

    read = int32()
    DAQmxReadAnalogF64(taskHandle,dataSize,10.0,DAQmx_Val_GroupByChannel,data,dataSize,byref(read),None)
    DAQmxStopTask(taskHandle)


    print("--- %s seconds ---" % (time.time() - start_time))

if name == 'main':

DAQ = MultiChannelAnalogInput(["M1/ai2","M1/ai1"])
DAQ.configure()

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants