def setupNICard(self): ## create the NI task that reads data from the NI card. #self.taskHandle = TaskHandle() #DAQmxCreateTask("pyEnvDAQ",byref(self.taskHandle)) ## (see http://zone.ni.com/reference/en-XX/help/370471AE-01/daqmxcfunc/daqmxcreatetask/ ) ##[DAQmxCreateAIVoltageChan(self.taskHandle, "Dev1/ai"+str(self.channelAddresses[i]), self.channelNames[i+1], DAQmx_Val_Cfg_Default, int(self.channelMins[i+1]), \ ## int(self.channelMaxes[i+1]), DAQmx_Val_Volts, None) for i in range(self.numOfRecordedChannels)] # we use i+1 instead of i to skip the time channel ##print("numOfNIChannels: ",self.numOfNIChannels) #channelIndices = ["Dev1/ai"+str(self.channelIndex[i]) for i in range(self.numOfNIChannels)] # note that this also contains the time channel ##[print("channel index: ",channelIndices[i]," name: ",self.channelNames[i]," min ",int(self.channelMins[i]), \ ## "max ",int(self.channelMaxes[i]),"\n") for i in range(1,self.numOfNIChannels)] #[DAQmxCreateAIVoltageChan(self.taskHandle, channelIndices[i], self.channelNames[i], DAQmx_Val_Cfg_Default, int(self.channelMins[i]), \ # int(self.channelMaxes[i]), DAQmx_Val_Volts, None) for i in range(1,self.numOfNIChannels)] # we start from 1 to skip the time channel # #DAQmxCfgSampClkTiming(self.taskHandle,"",1/self.MEASUREMENT_PERIOD_s,DAQmx_Val_Rising,DAQmx_Val_ContSamps,self.numOfNIChannels-1) # -1 because we don't actually record the time channel ## (see http://zone.ni.com/reference/en-XX/help/370471AE-01/daqmxcfunc/daqmxcfgsampclktiming/ ) ## #try: # DAQmxStartTask(self.taskHandle) #except DAQError: # self.printError("execution of DAQmxStartTask failed - THe-Monitor is probably on \"Run\". Try \"Pause\"ing it and restarting PyEnvDAQ.") self.taskHandle = TaskHandle() #DAQmxLoadTask("enviroment_measurement",byref(self.taskHandle)) DAQmxLoadTask("PyEnvDAQTask", byref(self.taskHandle)) DAQmxStartTask(self.taskHandle)
def init(self): if self.task_state == "": self.task = TaskHandle() DAQmxCreateTask(b"", byref(self.task)) DAQmxCreateCITwoEdgeSepChan( self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), b"", float64(self.min_val), float64(self.max_val), DAQmx_Val_Seconds, self.first_edge_type, self.second_edge_type, b"", ) if self.source_terminal: tmp_data = c_char_p(self.source_terminal.encode()) DAQmxSetCITwoEdgeSepFirstTerm( self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), tmp_data, ) if self.destination_terminal: tmp_data = c_char_p(self.destination_terminal.encode()) DAQmxSetCITwoEdgeSepSecondTerm( self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), tmp_data, ) self.task_state = "init"
def init(self): if self.task_state == "": self.task = TaskHandle() DAQmxCreateTask(b"", byref(self.task)) DAQmxCreateDOChan(self.task, self.task_string, b"", DAQmx_Val_ChanPerLine) self.task_state = "init"
def init(self): if self.task_state == "": self.task = TaskHandle() DAQmxCreateTask(b"", byref(self.task)) DAQmxCreateDIChan(self.task, self.task_string, b"", DAQmx_Val_ChanPerLine) self.task_state = "init" if self.task_state in ["init", "stopped"]: self.start()
def create_tasks(self): ''' Create TaskHandles for each channel ''' task_handles = dict([(name, TaskHandle(0)) for name in self.physical_channel]) for name in self.physical_channel: DAQmxCreateTask('', byref(task_handles[name])) self.task_handles = task_handles
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_RSE, self.limit[name][0], self.limit[name][1], DAQmx_Val_Volts, None) self.taskHandles = taskHandles
def init(self): if self.task_state == "": self.task = TaskHandle() DAQmxCreateTask(b"", byref(self.task)) DAQmxCreateCITwoEdgeSepChan(self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), b"", float64(self.min_val), float64(self.max_val), DAQmx_Val_Seconds, self.first_edge_type, self.second_edge_type, b"") if self.source_terminal: tmp_data = c_char_p(self.source_terminal.encode()) DAQmxSetCITwoEdgeSepFirstTerm(self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), tmp_data) if self.validate_terminals: tmp_data = c_char_p("".encode()) DAQmxGetCITwoEdgeSepFirstTerm(self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), tmp_data, uInt32(16)) if self.destination_terminal not in tmp_data.value.decode('utf-8'): raise InstrumentError( "Destination terminal is set to {}, should be /{}/{}".format(tmp_data.value.decode('utf-8'), self.device_name, self.destination_terminal)) if self.destination_terminal: tmp_data = c_char_p(self.destination_terminal.encode()) DAQmxSetCITwoEdgeSepSecondTerm(self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), tmp_data) if self.validate_terminals: tmp_data = c_char_p("".encode()) DAQmxGetCITwoEdgeSepSecondTerm(self.task, "{}/{}".format(self.device_name, self.counter_chan).encode(), tmp_data, uInt32(16)) if self.destination_terminal not in tmp_data.value.decode('utf-8'): raise InstrumentError( "Destination terminal is set to {}, should be /{}/{}".format(tmp_data.value.decode('utf-8'), self.device_name, self.destination_terminal)) self.task_state = "init"
def serve(self): while True: now = datetime.datetime.now() print('sampling frequency : {}'.format(self.samplingFreq)) print('samples per channel : {}'.format( self.numSampsPerChan)) print('maximum number of segment : {}'.format( self.maxSegmentNum)) print('number of channels : {}'.format(self.channelNum)) dt = 1.0 / self.samplingFreq taskHandle = TaskHandle() try: # DAQmx Configure Code DAQmxCreateTask("", byref(taskHandle)) def createChannel(devID, channelIDs): try: DAQmx_Val_dict = { 'DIFF': DAQmx_Val_Diff, 'RSE': DAQmx_Val_RSE, 'NRSE': DAQmx_Val_NRSE } device_and_channelsL = [ "Dev" + str(devID) + "/ai" + str(channelID) for channelID in channelIDs ] device_and_channels = ", ".join(device_and_channelsL) DAQmxCreateAIVoltageChan( taskHandle, device_and_channels, "", DAQmx_Val_dict[self.terminal_config], -10.0, 10.0, DAQmx_Val_Volts, None) print( "at DAQmxCreateAIVoltageChan, created succesfully with channelNum = " + str(self.channelNum) + ".") return 1 except DAQError as err: print("at DAQmxCreateAIVoltageChan, DAQmx Error: %s" % err) self.logFile.write( "at DAQmxCreateAIVoltageChan, DAQmx Error: %s" % err) self.logFile.flush() return 0 for devID in [1, 2, 3, 4, 0]: if createChannel(devID, self.channelIDs): break # param: taskHandle # param: source (const char[]) # param: samplingFreq : sapmling frequency (Hz) # param: activeEdge # param: sampleMode (int32) : DAQmx_Val_FiniteSamps # or DAQmx_Val_ContSamps or DAQmx_Val_HWTimedSinglePoint # param: numSampsPerChan (int) : number of samples per channel DAQmxCfgSampClkTiming( taskHandle, "", self.samplingFreq, DAQmx_Val_Rising, DAQmx_Val_ContSamps, # DAQmx_Val_FiniteSamps, self.numSampsPerChan * self.channelNum) # elf.numSampsPerChan) ''' convRateFactor = 5 new_convRate = float64(self.samplingFreq * self.channelNum * convRateFactor) DAQmxSetAIConvRate(taskHandle, new_convRate) convRate = float64() DAQmxGetAIConvRate(taskHandle, byref(convRate)) # print('convRate =', convRate) ''' # DAQmx Start Code DAQmxStartTask(taskHandle) # DAQmxSetReadOverWrite(taskHandle, DAQmx_Val_OverwriteUnreadSamps) for timestep in tqdm.tqdm(range(1, self.maxSegmentNum + 1)): data = np.zeros((self.numSampsPerChan * self.channelNum, ), dtype=np.float64) now = self.read_data(taskHandle, data) # print('data.shape =', data.shape) sampleNum = data.shape[0] // self.channelNum if self.channelNum == 2: eeg_data = data[:sampleNum] ch2_data = data[sampleNum:] else: eeg_data = data[:sampleNum] dataToClient = '' for sampleID in range(sampleNum): current_time = self.updateTimeStamp(now, sampleID, dt) ftime = current_time.strftime('%H:%M:%S.') ftime += '%06d' % current_time.microsecond dataToClient += ftime + '\t' + str(eeg_data[sampleID]) if self.channelNum == 2: dataToClient += '\t' + str( ch2_data[sampleID]) + '\n' else: dataToClient += '\n' # dataToClient = dataToClient.rstrip() # print('in server, dataToClient.shape =', dataToClient.shape) # print('in server, dataToClient =', dataToClient) self.client.process(dataToClient) # record log presentTime = timeFormatting.presentTimeEscaped() if self.recordWaves: self.logFile.write(presentTime + ', ' + str(eeg_data.shape[0]) + ', ' + str(eeg_data) + '\n') else: self.logFile.write(presentTime + ', ' + str(eeg_data.shape[0]) + '\n') self.logFile.flush() except DAQError as err: print("DAQmx Error: %s" % err) self.logFile.write("DAQmx Error: %s" % err) self.logFile.flush() finally: if taskHandle: DAQmxStopTask(taskHandle) DAQmxClearTask(taskHandle)