def write(self, data): """ Data must be an iterable like a list of 1s and 0s Data is grouped by scan number. Each element in the array will write to each line in the digital output until exhausted and then will start from the beginning for the next sample. Sample rate is as set in creating the IO task. """ self.init() try: if len(data) % self.io_length: raise ValueError("data must be a length divisible by {}".format(self.io_length)) data_arr = numpy.zeros(len(data), uInt8) data_arr[:] = data except TypeError: if self.io_length != 1: raise ValueError("data must be a list of length divisible by {}".format(self.io_length)) data_arr = numpy.zeros(1, uInt8) data_arr[:] = [data] written = int32() DAQmxWriteDigitalLines(self.task, len(data_arr) // self.io_length, # Samples per channel 1, # Autostart task 2.0, # Timeout DAQmx_Val_GroupByScanNumber, # Interleaved data_arr, written, None)
def write(self, data): """ The task should be in stopped state when calling write, it automatically starts the task through the DAQmxWriteDigitalLines call. When write is finished it is back in a stopped state :param data: :return: """ self.init() try: if len(data) % self.io_length: raise ValueError("data must be a length divisible by {}".format(self.io_length)) except TypeError as e: raise ValueError("data must be in an list divisible by {}".format(self.io_length)) from e if len(data) == self.io_length: # Sample clock only works for more than one sample so duplicate the sample data = list(data) data.extend(data) DAQmxCfgSampClkTiming(self.task, None, float64(self.frequency), DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, uInt64(int(len(data) // self.io_length))) try: data_arr = numpy.zeros((len(data)), uInt8) data_arr[:] = data written = int32() DAQmxWriteDigitalLines(self.task, int(len(data) // self.io_length), 1, -1, DAQmx_Val_GroupByScanNumber, data_arr, written, None) self.task_state = "running" DAQmxWaitUntilTaskDone(self.task, -1) if written.value != len(data) // self.io_length: raise InstrumentError("Values not written correctly") finally: self.stop()
def read(self): self.init() data_arr = numpy.zeros(self.io_length, uInt8) samples_per_chan = int32() num_bytes_per_sample = int32() DAQmxReadDigitalLines(self.task, 1, # Samples per channel 2.0, # Timeout DAQmx_Val_GroupByScanNumber, # Interleaved data_arr, len(data_arr), byref(samples_per_chan), byref(num_bytes_per_sample), None) return data_arr