def write_do(taskhandle, indata, intimeout=0): ''' Write given data to given task in taskhandle = Task handle indata = Samples to write as list of integers intimeout = Timeout for writing all samples, default 0 = try once out ErrorText = Error text, None if no errors ''' errortext = None #logger.info("Indata: {}".format(indata)) if isinstance(indata, int): samplecount = 1 datatype = uInt8 data = datatype(indata) else: samplecount = len(indata) datatype = uInt8 * samplecount data = datatype(*indata) result = int32() samples = int32(samplecount) autostart = bool32(False) timeout = float64(intimeout) try: taskhandle.WriteDigitalU8(samples, autostart, timeout, DAQmx_Val_GroupByChannel, data, byref(result), None) except DAQError as err: errortext = err return errortext
def __init__(self, config, consumer): Task.__init__(self) self.config = config self.consumer = consumer self.sample_buffer_size = (self.config.sampling_rate + 1) * self.config.number_of_ports * 2 self.samples_read = int32() self.remainder = [] # create voltage channels for i in xrange(0, 2 * self.config.number_of_ports, 2): self.CreateAIVoltageChan('{}/ai{}'.format(config.device_id, config.channel_map[i]), '', DAQmx_Val_Diff, -config.v_range, config.v_range, DAQmx_Val_Volts, None) self.CreateAIVoltageChan('{}/ai{}'.format(config.device_id, config.channel_map[i + 1]), '', DAQmx_Val_Diff, -config.dv_range, config.dv_range, DAQmx_Val_Volts, None) # configure sampling rate self.CfgSampClkTiming('', self.config.sampling_rate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, self.config.sampling_rate) # register callbacks self.AutoRegisterEveryNSamplesEvent(DAQmx_Val_Acquired_Into_Buffer, self.config.sampling_rate // 2, 0) self.AutoRegisterDoneEvent(0)
def write_analog_io_trigger(taskhandle, value, intimeout=100): ''' Write data from given task in Task handle Timeout for writing, default 100 out write data ''' samples = 2 datatype = float64 * samples data = datatype() for i in range(samples): data[i] = float(value) #logger.info("testlog, write analog io trigger -funtion for loop 1: {}".format(value)) for i in data: logger.info( "testlog, write analog io trigger -funtion, for loop 2: {}".format( str(i))) timeout = float64(intimeout) #logger.info("write analog io trigger -funtion, test1: {}".format(data)) taskhandle.WriteAnalogF64(2, False, timeout, DAQmx_Val_GroupByChannel, data, int32(), None) taskhandle.StartTask() taskhandle.WaitUntilTaskDone(-1) return data
def read_di(taskhandle, intimeout=0): ''' Read data from given task in Task handle Timeout for reading, default 0 = try once out Read data ''' value = uInt8() result = int32() bytespersample = int32() samples = int32(1) readarraysize = uInt32(1) timeout = float64(intimeout) taskhandle.ReadDigitalU8(samples, timeout, DAQmx_Val_GroupByScanNumber, value, readarraysize, byref(result), None) return value.value
def port_supports_buffered(device_name, port, clock_terminal=None): """Empirically determines if the digital port supports buffered output. Args: device_name (str): NI-MAX device name port (int): Which port to intro-spect clock_terminal (str, optional): String that specifies the clock terminal. Returns: bool: True if `port` supports buffered output. """ all_terminals = DAQmxGetDevTerminals(device_name) if clock_terminal is None: clock_terminal = all_terminals[0] npts = 16 task = Task() clock_terminal_full = '/' + device_name + '/' + clock_terminal data = np.zeros(npts, dtype=np.uint32) task.CreateDOChan(device_name + "/" + port, "", c.DAQmx_Val_ChanForAllLines) task.CfgSampClkTiming( clock_terminal_full, 100, c.DAQmx_Val_Rising, c.DAQmx_Val_FiniteSamps, npts ) written = int32() try: task.WriteDigitalU32( npts, False, 10.0, c.DAQmx_Val_GroupByScanNumber, data, byref(written), None ) except ( PyDAQmx.DAQmxFunctions.BufferedOperationsNotSupportedOnSelectedLinesError, PyDAQmx.DAQmxFunctions.PhysicalChanNotSupportedGivenSampTimingType653xError, ): return False except ( PyDAQmx.DAQmxFunctions.CantUsePort3AloneGivenSampTimingTypeOn653xError, PyDAQmx.DAQmxFunctions.CantUsePort1AloneGivenSampTimingTypeOn653xError, ): # Ports that throw this error on 653x devices do support buffered output, though # there are requirements that multiple ports be used together. return True except PyDAQmx.DAQmxFunctions.RouteNotSupportedByHW_RoutingError: # Try again with a different terminal current_terminal_index = all_terminals.index(clock_terminal) if current_terminal_index == len(all_terminals) - 1: # There are no more terminals. No terminals can be used as clocks, # therefore we cannot do externally clocked buffered output. return False next_terminal_to_try = all_terminals[current_terminal_index + 1] return port_supports_buffered(device_name, port, next_terminal_to_try) else: return True finally: task.ClearTask()
def supports_semiperiod_measurement(device_name): import warnings with warnings.catch_warnings(): # PyDAQmx warns about a positive return value, but actually this is how you are # supposed to figure out the size of the array required. warnings.simplefilter("ignore") # Pass in null pointer and 0 len to ask for what array size is needed: npts = PyDAQmx.DAQmxGetDevCISupportedMeasTypes(device_name, int32(), 0) # Create that array result = (int32 * npts)() PyDAQmx.DAQmxGetDevCISupportedMeasTypes(device_name, result, npts) return c.DAQmx_Val_SemiPeriod in [result[i] for i in range(npts)]
def read_analog_io_RSE(taskhandle, samples, intimeout=100): ''' Initialize task for reading from analog input port for Voltage measurement in devport = Device/port e.g. Dev1/ai2 Timeout for reading, default 100 out task = Task handle ''' datatype = ctypes.c_double * samples data = datatype() result = int32() readarraysize = uInt32(samples) timeout = float64(intimeout) taskhandle.ReadAnalogF64(samples, timeout, DAQmx_Val_GroupByChannel, data, readarraysize, byref(result), None) #sleep(0.1) for i in data: logger.info("Voltage level: {}".format(str(i))) return repr(data)
def read_analog_io_Diff(taskhandle, samples, intimeout=100): ''' Read data from given task in Task handle Timeout for reading, default 100 out Read data ''' datatype = ctypes.c_double * samples data = datatype() result = int32() readarraysize = uInt32(samples * 2) timeout = float64(intimeout) taskhandle.ReadAnalogF64(samples, timeout, DAQmx_Val_GroupByChannel, data, readarraysize, byref(result), None) #sleep(0.1) for i in data: logger.info("Voltage level: {}".format(str(i))) return repr(data)
def __init__(self, config, consumer): Task.__init__(self) self.config = config self.consumer = consumer self.sample_buffer_size = (self.config.sampling_rate + 1) * self.config.number_of_ports * 2 self.samples_read = int32() self.remainder = [] # create voltage channels for i in range(0, 2 * self.config.number_of_ports, 2): self.CreateAIVoltageChan( '{}/ai{}'.format(config.device_id, config.channel_map[i]), '', DAQmx_Val_Diff, -config.v_range, config.v_range, DAQmx_Val_Volts, None) self.CreateAIVoltageChan( '{}/ai{}'.format(config.device_id, config.channel_map[i + 1]), '', DAQmx_Val_Diff, -config.dv_range, config.dv_range, DAQmx_Val_Volts, None) # configure sampling rate self.CfgSampClkTiming('', self.config.sampling_rate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, self.config.sampling_rate)
def supports_semiperiod_measurement(device_name): """Empirically determines if the DAQ supports semiperiod measurement. Args: device_name (str): NI-MAX device name. Returns: bool: True if semi-period measurements are supported by the device. """ import warnings with warnings.catch_warnings(): # PyDAQmx warns about a positive return value, but actually this is how you are # supposed to figure out the size of the array required. warnings.simplefilter("ignore") # Pass in null pointer and 0 len to ask for what array size is needed: npts = PyDAQmx.DAQmxGetDevCISupportedMeasTypes(device_name, int32(), 0) # Create that array result = (int32 * npts)() PyDAQmx.DAQmxGetDevCISupportedMeasTypes(device_name, result, npts) return c.DAQmx_Val_SemiPeriod in [result[i] for i in range(npts)]
def wrapped(name): result = int32() func(name, byref(result)) return result.value