def start_scan(self): # Build the data array points_per_channel = 1000 rate = 1000 num_points = self.num_chans * points_per_channel scan_options = ScanOptions.BACKGROUND | ScanOptions.CONTINUOUS ao_range = self.ao_props.available_ranges[0] self.memhandle = ul.win_buf_alloc(num_points) # Check if the buffer was successfully allocated if not self.memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: data_array = self.memhandle_as_ctypes_array(self.memhandle) freq = self.add_example_data(data_array, ao_range, rate, points_per_channel) self.freq_label["text"] = str(freq) + "Hz" ul.daq_out_scan(self.board_num, self.chan_list, self.chan_type_list, self.gain_list, self.num_chans, rate, num_points, self.memhandle, scan_options) # Start updating the displayed values self.update_displayed_values() except ULError as e: self.show_ul_error(e) self.set_ui_idle_state() return
def start_scan(self): rate = 100 count = 1000 # Allocate a buffer for the scan self.memhandle = ul.win_buf_alloc(count) # Check if the buffer was successfully allocated if not self.memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.set_ui_idle_state() return try: # Configure the port (if necessary) if self.port.is_port_configurable: ul.d_config_port(self.board_num, self.port.type, DigitalIODirection.IN) # Run the scan ul.d_in_scan(self.board_num, self.port.type, count, rate, self.memhandle, ScanOptions.BACKGROUND) except ULError as e: self.show_ul_error(e) self.set_ui_idle_state() return # Convert the memhandle to a ctypes array # Note: the ctypes array will no longer be valid after win_buf_free is called. # A copy of the buffer can be created using win_buf_to_array # before the memory is freed. The copy can be used at any time. self.ctypes_array = self.memhandle_as_ctypes_array(self.memhandle) # Start updating the displayed values self.update_displayed_values()
def __init__(self, board_num: int, dev_handle: ul.DaqDeviceDescriptor, sampling: params.Sampling): self.dev_info = DaqDeviceInfo(board_num) self.sampling = sampling self.ao_range = self.dev_info.get_ao_info().supported_ranges[0] self.channels = [] self.num_ao_channels: int = self.dev_info.get_ao_info().num_chans self.points_per_channel: int = 1024 self.buffer_size = self.num_ao_channels * self.points_per_channel dev.Device.__init__(self, self.board_num, self.dev_info) if MCCDAQ.__registered_board_nums.index(board_num) == -1: ul.ignore_instacal() ul.create_daq_device(board_num, dev_handle) MCCDAQ.__registered_board_nums.append(board_num) MCCDAQ.__memhandles.append(ul.win_buf_alloc(self.buffer_size)) self.memhandle = MCCDAQ.__memhandles.index(self.board_num) if not self.memhandle: raise Exception('MCCDAQChannel: Failed to allocate memory') self.cdata = cast(self.memhandle, POINTER(c_ushort)) for channel_idx in range(self.num_ao_channels): self.channels.append(MCCDAQChannel(self, channel_idx))
def start_scan(self): rate = 100 points_per_channel = 10 total_count = points_per_channel * self.num_chans # Allocate a buffer for the scan memhandle = ul.win_buf_alloc(total_count) # Check if the buffer was successfully allocated if not memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: # Configure the digital port for input ul.d_config_port(self.board_num, DigitalPortType.FIRSTPORTA, DigitalIODirection.IN) # Configure the counter channel ul.c_config_scan(self.board_num, 0, CounterMode.STOP_AT_MAX, CounterDebounceTime.DEBOUNCE_NONE, 0, CounterEdgeDetection.RISING_EDGE, CounterTickSize.TICK20PT83ns, 0) # Run the scan ul.daq_in_scan(self.board_num, self.chan_list, self.chan_type_list, self.gain_list, self.num_chans, rate, 0, total_count, memhandle, 0) # Convert the TC values (optional parameter omitted) err, temp_data_array = ul.get_tc_values(self.board_num, self.chan_list, self.chan_type_list, self.num_chans, memhandle, 0, points_per_channel, TempScale.CELSIUS) if err == ErrorCode.OUTOFRANGE: messagebox.showwarning("Warning", "Temperature data is out of range") # Cast the memhandle to a ctypes pointer # Note: the ctypes array will only be valid until win_buf_free # is called. array = self.memhandle_as_ctypes_array(memhandle) # Display the values self.display_values(array, temp_data_array, total_count) except ULError as e: self.show_ul_error(e) finally: # Free the allocated memory ul.win_buf_free(memhandle) self.start_button["state"] = tk.NORMAL
def scan_loop(self): rate = 100 points_per_channel = 10 low_chan = 0 # Ignored by a_in_scan when queue is enabled high_chan = 3 # Ignored by a_in_scan when queue is enabled num_channels = high_chan - low_chan + 1 total_count = points_per_channel * num_channels # Ignored by a_in_scan when queue is enabled range_ = self.ai_props.available_ranges[0] # Allocate a buffer for the scan if self.ai_props.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= 16 memhandle = ul.win_buf_alloc(total_count) else: # Use the win_buf_alloc_32 method for devices with a resolution > # 16 memhandle = ul.win_buf_alloc_32(total_count) # Check if the buffer was successfully allocated if not memhandle: messagebox.showerror("Error", "Failed to allocate memory") return try: # Run the scan ul.a_in_scan(self.board_num, low_chan, high_chan, total_count, rate, range_, memhandle, 0) # Convert the memhandle to a ctypes array # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy can # be used at any time. if self.ai_props.resolution <= 16: # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16 array = self.memhandle_as_ctypes_array(memhandle) else: # Use the memhandle_as_ctypes_array_32 method for devices with # a resolution > 16 array = self.memhandle_as_ctypes_array_32(memhandle) # Display the values self.display_values(array, total_count) self.after(1000, self.scan_loop) except ULError as e: self.show_ul_error(e) finally: # Free the allocated memory ul.win_buf_free(memhandle)
def start_scan(self): rate = 100 points_per_channel = 100 total_count = points_per_channel * self.num_chans scan_options = (ScanOptions.BACKGROUND | ScanOptions.CONTINUOUS | ScanOptions.EXTTRIGGER) # Allocate a buffer for the scan self.memhandle = ul.win_buf_alloc(total_count) # Check if the buffer was successfully allocated if not self.memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: # Set the start trigger settings ul.daq_set_trigger(self.board_num, TriggerSource.ANALOG_SW, TriggerSensitivity.RISING_EDGE, self.chan_list[0], self.chan_type_list[0], self.gain_list[0], 2, 0, TriggerEvent.START) # Set the stop trigger settings ul.daq_set_trigger(self.board_num, TriggerSource.COUNTER, TriggerSensitivity.ABOVE_LEVEL, self.chan_list[2], self.chan_type_list[2], self.gain_list[2], 2, 0, TriggerEvent.START) # Run the scan ul.daq_in_scan(self.board_num, self.chan_list, self.chan_type_list, self.gain_list, self.num_chans, rate, 0, total_count, self.memhandle, scan_options) # Cast the memhandle to a ctypes pointer # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # before the memory is freed. The copy can be used at any time. self.array = self.memhandle_as_ctypes_array(self.memhandle) except ULError as e: # Free the allocated memory ul.win_buf_free(self.memhandle) self.show_ul_error(e) return # Start updating the displayed values self.update_displayed_values()
def start_scan(self): rate = 100 points_per_channel = 100 total_count = points_per_channel * self.num_chans scan_options = ScanOptions.BACKGROUND | ScanOptions.CONTINUOUS # Allocate a buffer for the scan if self.resolution <= 16: self.memhandle = ul.win_buf_alloc(total_count) else: self.memhandle = ul.win_buf_alloc_32(total_count) # Check if the buffer was successfully allocated if not self.memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: # Run the scan ul.daq_in_scan( self.board_num, self.chan_list, self.chan_type_list, self.gain_list, self.num_chans, rate, 0, total_count, self.memhandle, scan_options) # Cast the memhandle to a ctypes pointer # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy can # be used at any time. if self.resolution <= 16: # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16 self.array = self.memhandle_as_ctypes_array(self.memhandle) else: # Use the memhandle_as_ctypes_array_32 method for devices with a # resolution > 16 self.array = self.memhandle_as_ctypes_array_32(self.memhandle) except ULError as e: # Free the allocated memory ul.win_buf_free(self.memhandle) self.show_ul_error(e) return # Start updating the displayed values self.update_displayed_values()
def start_scan(self): rate = 100 points_per_channel = 10 total_count = points_per_channel * self.num_chans # Allocate a buffer for the scan if self.resolution <= 16: memhandle = ul.win_buf_alloc(total_count) else: memhandle = ul.win_buf_alloc_32(total_count) # Check if the buffer was successfully allocated if not memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: # Run the scan ul.daq_in_scan(self.board_num, self.chan_list, self.chan_type_list, self.gain_list, self.num_chans, rate, 0, total_count, memhandle, 0) # Cast the memhandle to a ctypes pointer # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy can # be used at any time. if self.resolution <= 16: # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16 array = cast(memhandle, POINTER(c_ushort)) else: # Use the memhandle_as_ctypes_array_32 method for devices with # a resolution > 16 array = cast(memhandle, POINTER(c_ulong)) # Display the values self.display_values(array, total_count) except ULError as e: show_ul_error(e) finally: # Free the allocated memory ul.win_buf_free(memhandle) self.start_button["state"] = tk.NORMAL
def start_scan(self): rate = 100 points_per_channel = 100 total_count = points_per_channel * self.num_chans scan_options = ScanOptions.BACKGROUND | ScanOptions.CONTINUOUS # Allocate a buffer for the scan self.memhandle = ul.win_buf_alloc(total_count) # Check if the buffer was successfully allocated if not self.memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: # Configure the setpoints ul.daq_set_setpoints(self.board_num, self.limit_a_list, self.limit_b_list, self.setpoint_flags_list, self.setpoint_output_list, self.output_1_list, self.output_2_list, self.output_mask_1_list, self.output_mask_2_list, self.setpoint_count) # Run the scan ul.daq_in_scan(self.board_num, self.chan_list, self.chan_type_list, self.gain_list, self.num_chans, rate, 0, total_count, self.memhandle, scan_options) # Cast the memhandle to a ctypes pointer # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # before the memory is freed. The copy can be used at any time. self.array = cast(self.memhandle, POINTER(c_ushort)) except ULError as e: # Free the allocated memory ul.win_buf_free(self.memhandle) show_ul_error(e) return # Start updating the displayed values self.update_displayed_values()
def send_data(self): # Build the data array num_chans = min(self.ao_info.num_chans, 4) num_points = num_chans ao_range = self.ao_info.supported_ranges[0] memhandle = ul.win_buf_alloc(num_points) # Check if the buffer was successfully allocated if not memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.send_data["state"] = tk.NORMAL return try: data_array = cast(memhandle, POINTER(c_ushort)) full_scale_count = (2**self.ao_info.resolution) - 1 value_step = full_scale_count / (num_chans + 1) for point_num in range(0, num_points): raw_value = int(value_step * (point_num + 1)) data_array[point_num] = raw_value self.raw_data_labels[point_num]["text"] = str(raw_value) # ul.to_eng_units cannot be used here, as it uses the analog # input resolution. Instead, do the conversion on our own. volts = self.ao_to_eng_units(raw_value, ao_range, self.ao_info.resolution) self.volts_labels[point_num]["text"] = ('{:.3f}'.format(volts)) ul.a_out_scan(self.board_num, 0, num_chans - 1, num_points, 100, ao_range, memhandle, 0) except ULError as e: show_ul_error(e) finally: ul.win_buf_free(memhandle)
def apply_and_listen(waveform_1d, nzeros_front, nzeros_back, in_channel_start=0, in_channel_end=0, out_channel_start=0, out_channel_end=0, rate=1000000, board_number=0, ul_range=ULRange.BIP10VOLTS, quiet=False, **kwargs): """ Apply a waveform and listen to collect data. Simultaneous output and collection of data. args: waveform_1d (numpy.array): Serialized waveform nzeros_front (int): Number of zeros padding front of waveform nzeros_back (int): Number of zeros padding back of waveform in_channel_start (int= 0): Specify which start channel to use when listening and collecting incoming waveform. in_channel_end (int= 0): Specify which end channel to use when listening and collecting incoming waveform. out_channel_start (int= 0): Specify which start channel to use when outputting the waveform. out_channel_end (int = 0): Specify which end channel to use when outputting waveform. rate (int = 1000000): Rate for daq board_number (int = 0): ul_range (ULRange): Range for daq quiet (bool): Specify verbosity returns: (memhandle_in, memhandle_out, data_array_in, data_array_out, count_in, time) waveform_1d should be serialized into 1d for all channels output comes on channels continuous from out_channel_start to out_channel_end return: memhandle_in, memhandle_out, data_array_in, data_array_out, count_in """ count_out = len(waveform_1d) nchannel_out = out_channel_end - out_channel_start + 1 nchannel_in = in_channel_end - in_channel_start + 1 rate = int(rate / nchannel_in) len_data_without_zeros = (len(waveform_1d) - nzeros_front - nzeros_back) period_of_wf = (int(len_data_without_zeros / nchannel_out) / rate) if not quiet: print('period:', period_of_wf * 1e6, 'us') trigger_rate = int((count_out - nzeros_front - nzeros_back) / nchannel_out) # Allocate the buffer and cast it to an unsigned short memhandle_out = ul.win_buf_alloc(count_out) data_array_out = ctypes.cast(memhandle_out, ctypes.POINTER( ctypes.c_ushort)) #data_array now points to the correct memory # Calculate and store the waveform in Windows buffer for i, y in enumerate(waveform_1d): data_array_out[i] = int(y) count_in = int(nchannel_in * count_out / (nchannel_out)) memhandle_in = ul.win_buf_alloc(count_in) data_array_in = ctypes.cast(memhandle_in, ctypes.POINTER(ctypes.c_ushort)) options = (None, ) # Output the waveform #import pdb; pdb.set_trace() ul.a_in_scan(board_number, in_channel_start, in_channel_end, count_in, rate, ul_range, memhandle_in, ScanOptions.EXTTRIGGER | ScanOptions.BACKGROUND) ul.a_out_scan(board_number, out_channel_start, out_channel_end, count_out, rate, ul_range, memhandle_out, ScanOptions.EXTTRIGGER | ScanOptions.BACKGROUND) ul.pulse_out_start(0, 0, rate, 0.5) while ul.get_status( 0, FunctionType.AOFUNCTION).status != 0: #poor mans foreground continue ul.pulse_out_stop(0, 0) ul.stop_background(0, FunctionType.AOFUNCTION) ul.stop_background(0, FunctionType.AIFUNCTION) timestep = period_of_wf / len_data_without_zeros time = [] for i in range(int(count_out / nchannel_out)): shiftedi = i - nzeros_front time.append(shiftedi * timestep) time = np.array(time) return memhandle_in, memhandle_out, data_array_in, data_array_out, count_in, time
#choosing parameters board_num = 0 channel1 = 0 channel2 = 1 ai_range = ULRange.BIP5VOLTS sample_rate = 2000 no_samples = 20000 #convert memhandle to array def memhandle_as_ctypes_array(memhandle): return ctypes.cast(memhandle, ctypes.POINTER(ctypes.c_ushort)) #store data as an array memhandle = ul.win_buf_alloc(no_samples) ctypes_array = memhandle_as_ctypes_array(memhandle) #taking measurements ul.a_in_scan(board_num, channel1, channel2, no_samples, sample_rate, ULRange.BIP5VOLTS, memhandle, ScanOptions.FOREGROUND) #create empty arrays for each channel array_ch1 = [0] * int(no_samples / 2) array_ch2 = [0] * int(no_samples / 2) angle = np.arctan2(array_ch1, array_ch2) for i in range(int(no_samples / 2)): array_ch1[i] = ctypes_array[2 * i] array_ch2[i] = ctypes_array[2 * i + 1]
def run(): DaqDeviceScan(master=tk.Tk()).mainloop() board_num = 0 rate = 1000 points_per_channel = 30 if use_device_detection: ul.ignore_instacal() if not configDevice(board_num): print("Gerät konnte nicht gefunden werden!") return ai_props = aiProps(board_num) low_channel = 0 high_channel = min(7, ai_props.num_ai_chans - 1) num_channels = high_channel - low_channel + 1 total_amount = points_per_channel * num_channels ai_range = ai_props.available_ranges[0] scan_opt = ScanOptions.FOREGROUND if ScanOptions.SCALEDATA in ai_props.supported_scan_options: scan_opt |= ScanOptions.SCALEDATA memhandle = ul.scaled_win_buf_alloc(total_amount) c_array = memhandle_as_ctypes_array_scaled(memhandle) elif ai_props.resolution <= 16: memhandle = ul.win_buf_alloc(total_amount) c_array = memhandle_as_ctypes_array(memhandle) else: memhandle = ul.win_buf_alloc_32(memhandle) if not memhandle: print("Speicher konnte nicht allokiert werden") restart = False try: wr = csv.writer(open("test5.csv","w"),delimiter=";") ul.a_in_scan(board_num, low_channel, high_channel, total_amount, rate, ai_range, memhandle, scan_opt) print("Scan erfolgreich!") print("Daten: ") test = ul.a_in_32(board_num, 0, ai_range, scan_opt) test = ul.to_eng_units_32(board_num, ai_range, test) print("test value:") print(test) row_format = "{:>5}" + "{:>10}" * num_channels labels = [] labels.append("Index") for ch_num in range(low_channel, high_channel + 1): labels.append("CH" + str(ch_num)) print(row_format.format(*labels)) data_index = 0 for index in range(points_per_channel): display_data = [index] for _ in range(num_channels): if ScanOptions.SCALEDATA in scan_opt: eng_value = c_array[data_index] else: eng_value = ul.to_eng_units( board_num, ai_range, c_array[data_index]) data_index += 1 display_data.append('{:.3f}'.format(eng_value)) wr.writerow(display_data) print(row_format.format(*display_data)) except ULError as e: pass finally: ul.win_buf_free(memhandle) if use_device_detection: ul.release_daq_device(board_num)
def run_example(): # By default, the example detects and displays all available devices and # selects the first device listed. Use the dev_id_list variable to filter # detected devices by device ID (see UL documentation for device IDs). # If use_device_detection is set to False, the board_num variable needs to # match the desired board number configured with Instacal. use_device_detection = True dev_id_list = [] board_num = 0 rate = 100 points_per_channel = 10 memhandle = None try: if use_device_detection: config_first_detected_device(board_num, dev_id_list) daq_dev_info = DaqDeviceInfo(board_num) if not daq_dev_info.supports_analog_input: raise Exception('Error: The DAQ device does not support ' 'analog input') print('\nActive DAQ device: ', daq_dev_info.product_name, ' (', daq_dev_info.unique_id, ')\n', sep='') ai_info = daq_dev_info.get_ai_info() low_chan = 0 high_chan = min(3, ai_info.num_chans - 1) num_chans = high_chan - low_chan + 1 total_count = points_per_channel * num_chans ai_range = ai_info.supported_ranges[0] scan_options = ScanOptions.FOREGROUND if ScanOptions.SCALEDATA in ai_info.supported_scan_options: # If the hardware supports the SCALEDATA option, it is easiest to # use it. scan_options |= ScanOptions.SCALEDATA memhandle = ul.scaled_win_buf_alloc(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array_scaled method for scaled # buffers. ctypes_array = cast(memhandle, POINTER(c_double)) elif ai_info.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= 16 memhandle = ul.win_buf_alloc(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16. ctypes_array = cast(memhandle, POINTER(c_ushort)) else: # Use the win_buf_alloc_32 method for devices with a resolution > 16 memhandle = ul.win_buf_alloc_32(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array_32 method for devices with a # resolution > 16 ctypes_array = cast(memhandle, POINTER(c_ulong)) # Note: the ctypes array will no longer be valid after win_buf_free is # called. # A copy of the buffer can be created using win_buf_to_array or # win_buf_to_array_32 before the memory is freed. The copy can be used # at any time. # Check if the buffer was successfully allocated if not memhandle: raise Exception('Error: Failed to allocate memory') # Start the scan ul.a_in_scan(board_num, low_chan, high_chan, total_count, rate, ai_range, memhandle, scan_options) print('Scan completed successfully. Data:') # Create a format string that aligns the data in columns row_format = '{:>5}' + '{:>10}' * num_chans # Print the channel name headers labels = ['Index'] for ch_num in range(low_chan, high_chan + 1): labels.append('CH' + str(ch_num)) print(row_format.format(*labels)) # Print the data data_index = 0 for index in range(points_per_channel): display_data = [index] for _ in range(num_chans): if ScanOptions.SCALEDATA in scan_options: # If the SCALEDATA ScanOption was used, the values # in the array are already in engineering units. eng_value = ctypes_array[data_index] else: # If the SCALEDATA ScanOption was NOT used, the # values in the array must be converted to # engineering units using ul.to_eng_units(). eng_value = ul.to_eng_units(board_num, ai_range, ctypes_array[data_index]) data_index += 1 display_data.append('{:.3f}'.format(eng_value)) # Print this row print(row_format.format(*display_data)) except Exception as e: print('\n', e) finally: if memhandle: # Free the buffer in a finally block to prevent a memory leak. ul.win_buf_free(memhandle) if use_device_detection: ul.release_daq_device(board_num)
def start_scan(self): self.low_chan = self.get_low_channel_num() self.high_chan = self.get_high_channel_num() self.num_chans = self.high_chan - self.low_chan + 1 if self.low_chan > self.high_chan: messagebox.showerror( "Error", "Low Channel Number must be greater than or equal to High " "Channel Number") self.set_ui_idle_state() return rate = 100 points_per_channel = 1000 total_count = points_per_channel * self.num_chans ai_range = self.ai_info.supported_ranges[0] # Allocate a buffer for the scan if self.ai_info.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= # 16 self.memhandle = ul.win_buf_alloc(total_count) # Convert the memhandle to a ctypes array # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16 self.ctypes_array = cast(self.memhandle, POINTER(c_ushort)) else: # Use the win_buf_alloc_32 method for devices with a resolution # > 16 self.memhandle = ul.win_buf_alloc_32(total_count) # Use the memhandle_as_ctypes_array_32 method for devices with a # resolution > 16 self.ctypes_array = cast(self.memhandle, POINTER(c_ulong)) # Note: the ctypes array will no longer be valid after # win_buf_free is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy # can be used at any time. # Check if the buffer was successfully allocated if not self.memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.set_ui_idle_state() return # Create the frames that will hold the data self.recreate_data_frame() try: # Start the scan ul.a_in_scan(self.board_num, self.low_chan, self.high_chan, total_count, rate, ai_range, self.memhandle, ScanOptions.BACKGROUND) except ULError as e: show_ul_error(e) self.set_ui_idle_state() return # Start updating the displayed values self.update_displayed_values()
def start_scan(self): low_chan = self.get_low_channel_num() high_chan = self.get_high_channel_num() if low_chan > high_chan: messagebox.showerror( "Error", "Low Channel Number must be greater than or equal to " "High Channel Number") self.start_button["state"] = tk.NORMAL return rate = 1000 points_per_channel = 10 num_channels = high_chan - low_chan + 1 total_count = points_per_channel * num_channels range_ = self.ai_props.available_ranges[0] trig_type = TrigType.TRIG_ABOVE low_threshold_volts = 0.1 high_threshold_volts = 1.53 # Allocate a buffer for the scan if self.ai_props.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= 16 memhandle = ul.win_buf_alloc(total_count) else: # Use the win_buf_alloc_32 method for devices with a resolution > # 16 memhandle = ul.win_buf_alloc_32(total_count) # Check if the buffer was successfully allocated if not memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: low_threshold, high_threshold = self.get_threshold_counts( range_, low_threshold_volts, high_threshold_volts) ul.set_trigger(self.board_num, trig_type, low_threshold, high_threshold) # Run the scan ul.a_in_scan(self.board_num, low_chan, high_chan, total_count, rate, range_, memhandle, ScanOptions.EXTTRIGGER) # Convert the memhandle to a ctypes array # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy can # be used at any time. if self.ai_props.resolution <= 16: # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16 array = self.memhandle_as_ctypes_array(memhandle) else: # Use the memhandle_as_ctypes_array_32 method for devices with # a resolution > 16 array = self.memhandle_as_ctypes_array_32(memhandle) # Display the values self.display_values(array, range_, total_count, low_chan, high_chan) except ULError as e: self.show_ul_error(e) finally: # Free the allocated memory ul.win_buf_free(memhandle) self.start_button["state"] = tk.NORMAL
def start_scan(self): low_chan = self.get_low_channel_num() high_chan = self.get_high_channel_num() if low_chan > high_chan: messagebox.showerror( "Error", "Low Channel Number must be greater than or equal to High " "Channel Number") self.start_button["state"] = tk.NORMAL return rate = 100 points_per_channel = 10 num_channels = high_chan - low_chan + 1 total_count = points_per_channel * num_channels ai_range = self.ai_info.supported_ranges[0] # Allocate a buffer for the scan if self.ai_info.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution # <= 16 memhandle = ul.win_buf_alloc(total_count) else: # Use the win_buf_alloc_32 method for devices with a resolution # > 16 memhandle = ul.win_buf_alloc_32(total_count) # Check if the buffer was successfully allocated if not memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: # Run the scan ul.a_in_scan(self.board_num, low_chan, high_chan, total_count, rate, ai_range, memhandle, 0) # Cast the memhandle to a ctypes pointer # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy # can be used at any time. if self.ai_info.resolution <= 16: # Use the memhandle_as_ctypes_array method for devices with # a resolution <= 16 array = cast(memhandle, POINTER(c_ushort)) else: # Use the memhandle_as_ctypes_array_32 method for devices # with a resolution > 16 array = cast(memhandle, POINTER(c_ulong)) # Display the values self.display_values(array, ai_range, total_count, low_chan, high_chan) except ULError as e: show_ul_error(e) finally: # Free the allocated memory ul.win_buf_free(memhandle) self.start_button["state"] = tk.NORMAL
def start_input_scan(self): self.input_low_chan = self.get_input_low_channel_num() self.input_high_chan = self.get_input_high_channel_num() self.num_input_chans = (self.input_high_chan - self.input_low_chan + 1) self.periodtime = int( self.periodbox.get()) # variable of the duration in sec self.periodtimevar = self.periodtime # a placeholder of periodtime which can be changed if self.input_low_chan > self.input_high_chan: messagebox.showerror( "Error", "Low Channel Number must be greater than or equal to High " "Channel Number") self.set_input_ui_idle_state() return rate = int( self.input_Samplingrate.get()) # data sampling rate per second # self.samplingrate = rate points_per_channel = self.test_time() total_count = points_per_channel * self.num_input_chans range_ = self.ai_props.available_ranges[0] scan_options = ScanOptions.BACKGROUND | ScanOptions.CONTINUOUS # Allocate a buffer for the scan if self.ai_props.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= # 16 self.input_memhandle = ul.win_buf_alloc(total_count) else: # Use the win_buf_alloc_32 method for devices with a resolution # > 16 self.input_memhandle = ul.win_buf_alloc_32(total_count) if not self.input_memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.set_input_ui_idle_state() return # Create the frames that will hold the data self.recreate_input_data_frame() try: # Run the scan ul.a_in_scan(self.board_num, self.input_low_chan, self.input_high_chan, total_count, rate, range_, self.input_memhandle, scan_options) except ULError as e: self.show_ul_error(e) self.set_input_ui_idle_state() return # Convert the input_memhandle to a ctypes array # Note: the ctypes array will no longer be valid after win_buf_free is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy can # be used at any time. if self.ai_props.resolution <= 16: # self.copied_array = ul.win_buf_to_array(self.iput_memhandle) # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16 self.ctypes_array = self.memhandle_as_ctypes_array( self.input_memhandle) else: # Use the memhandle_as_ctypes_array_32 method for devices with a # resolution > 16 self.ctypes_array = self.memhandle_as_ctypes_array_32( self.input_memhandle) # Start updating the displayed values self.update_input_displayed_values(range_) # Start the arena output self.tempo = 2.2 self.update_arena_output()
def run_example(): board_num = 0 rate = 100 points_per_channel = 10 if use_device_detection: ul.ignore_instacal() if not util.config_first_detected_device(board_num): print("Could not find device.") return ai_props = AnalogInputProps(board_num) if ai_props.num_ai_chans < 1: util.print_unsupported_example(board_num) return low_chan = 0 high_chan = min(3, ai_props.num_ai_chans - 1) num_chans = high_chan - low_chan + 1 total_count = points_per_channel * num_chans ai_range = ai_props.available_ranges[0] scan_options = ScanOptions.FOREGROUND if ScanOptions.SCALEDATA in ai_props.supported_scan_options: # If the hardware supports the SCALEDATA option, it is easiest to # use it. scan_options |= ScanOptions.SCALEDATA memhandle = ul.scaled_win_buf_alloc(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array_scaled method for scaled # buffers. ctypes_array = util.memhandle_as_ctypes_array_scaled(memhandle) elif ai_props.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= 16 memhandle = ul.win_buf_alloc(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16. ctypes_array = util.memhandle_as_ctypes_array(memhandle) else: # Use the win_buf_alloc_32 method for devices with a resolution > 16 memhandle = ul.win_buf_alloc_32(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array_32 method for devices with a # resolution > 16 ctypes_array = util.memhandle_as_ctypes_array_32(memhandle) # Note: the ctypes array will no longer be valid after win_buf_free is # called. # A copy of the buffer can be created using win_buf_to_array or # win_buf_to_array_32 before the memory is freed. The copy can be used # at any time. # Check if the buffer was successfully allocated if not memhandle: print("Failed to allocate memory.") return try: # Start the scan ul.a_in_scan(board_num, low_chan, high_chan, total_count, rate, ai_range, memhandle, scan_options) print("Scan completed successfully. Data:") # Create a format string that aligns the data in columns row_format = "{:>5}" + "{:>10}" * num_chans # Print the channel name headers labels = [] labels.append("Index") for ch_num in range(low_chan, high_chan + 1): labels.append("CH" + str(ch_num)) print(row_format.format(*labels)) # Print the data data_index = 0 for index in range(points_per_channel): display_data = [index] for _ in range(num_chans): if ScanOptions.SCALEDATA in scan_options: # If the SCALEDATA ScanOption was used, the values # in the array are already in engineering units. eng_value = ctypes_array[data_index] else: # If the SCALEDATA ScanOption was NOT used, the # values in the array must be converted to # engineering units using ul.to_eng_units(). eng_value = ul.to_eng_units(board_num, ai_range, ctypes_array[data_index]) data_index += 1 display_data.append('{:.3f}'.format(eng_value)) # Print this row print(row_format.format(*display_data)) except ULError as e: util.print_ul_error(e) finally: # Free the buffer in a finally block to prevent errors from causing # a memory leak. ul.win_buf_free(memhandle) if use_device_detection: ul.release_daq_device(board_num)
def __init__(self, biprange, srate): super(DaqThread, self).__init__() # QObject.__init__(self) self.biprange = biprange # self.mutex = QMutex() self.rate = srate self.chunksize = 1024 # int(self.rate/1000) self.board_num = 1 self.file_ls = [] self.chunk_ls = [] self.chunk_np = np.zeros(self.chunksize) ######## # The size of the UL buffer to create, in seconds buffer_size_seconds = 15 # The number of buffers to write num_buffers_to_write = 8 # VER si eliminar o dejar para que confirme si recibe el device y # lo libere al final, esta bueno para no tener que abrir instacal self.use_device_detection = False # Cambiar a True en version final if self.use_device_detection: ul.ignore_instacal() if not util.config_first_detected_device(self.board_num): QtGui.QMessageBox.information( self, "Connect device", "Could not find device") # check message box return ai_props = AnalogInputProps(self.board_num) # In case more channels are added in the future self.low_chan = 0 self.high_chan = 0 num_chans = self.high_chan - self.low_chan + 1 # Create a circular buffer that can hold buffer_size_seconds worth of # data, or at least 10 points (this may need to be adjusted to prevent # a buffer overrun) points_per_channel = max(self.rate * buffer_size_seconds, 10) # Some hardware requires that the total_count is an integer multiple # of the packet size. For this case, calculate a points_per_channel # that is equal to or just above the points_per_channel selected # which matches that requirement. if ai_props.continuous_requires_packet_size_multiple: packet_size = ai_props.packet_size remainder = points_per_channel % packet_size if remainder != 0: points_per_channel += packet_size - remainder # In case more channels are added in the future self.ul_buffer_count = points_per_channel * num_chans # Pick range from settings Combobox self.ai_range = ai_props.available_ranges[self.biprange] # Write the UL buffer to the file num_buffers_to_write times self.points_to_write = self.ul_buffer_count * num_buffers_to_write # # When handling the buffer, we will read 1/10 of the buffer at a time self.write_chunk_size = self.chunksize # int(self.ul_buffer_count / 10) self.scan_options = (ScanOptions.BACKGROUND | ScanOptions.CONTINUOUS) self.memhandle = ul.win_buf_alloc(self.ul_buffer_count) # Allocate an array of doubles temporary storage of the data self.write_chunk_array = (c_ushort * self.write_chunk_size)() # Check if the buffer was successfully allocated if not self.memhandle: QtGui.QMessageBox.critical(self, "No Buffer", "Failed to allocate memory.") print("No Buffer") ul.stop_background(self.board_num, FunctionType.AIFUNCTION) return
def run_example(): board_num = 0 if use_device_detection: ul.ignore_instacal() if not util.config_first_detected_device(board_num): print("Could not find device.") return ao_props = AnalogOutputProps(board_num) if ao_props.num_chans < 1: util.print_unsupported_example(board_num) return low_chan = 0 high_chan = min(3, ao_props.num_chans - 1) num_chans = high_chan - low_chan + 1 rate = 100 points_per_channel = 1000 total_count = points_per_channel * num_chans ao_range = ao_props.available_ranges[0] # Allocate a buffer for the scan memhandle = ul.win_buf_alloc(total_count) # Convert the memhandle to a ctypes array # Note: the ctypes array will no longer be valid after win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # before the memory is freed. The copy can be used at any time. ctypes_array = util.memhandle_as_ctypes_array(memhandle) # Check if the buffer was successfully allocated if not memhandle: print("Failed to allocate memory.") return frequencies = add_example_data(board_num, ctypes_array, ao_range, num_chans, rate, points_per_channel) for ch_num in range(low_chan, high_chan + 1): print("Channel " + str(ch_num) + " Output Signal Frequency: " + str(frequencies[ch_num - low_chan])) try: # Start the scan ul.a_out_scan(board_num, low_chan, high_chan, total_count, rate, ao_range, memhandle, ScanOptions.BACKGROUND) # Wait for the scan to complete print("Waiting for output scan to complete...", end="") status = Status.RUNNING while status != Status.IDLE: print(".", end="") # Slow down the status check so as not to flood the CPU time.sleep(0.5) status, _, _ = ul.get_status(board_num, FunctionType.AOFUNCTION) print("") print("Scan completed successfully.") except ULError as e: util.print_ul_error(e) finally: # Free the buffer in a finally block to prevent errors from causing # a memory leak. ul.win_buf_free(memhandle) if use_device_detection: ul.release_daq_device(board_num)
def run_example(): # By default, the example detects and displays all available devices and # selects the first device listed. Use the dev_id_list variable to filter # detected devices by device ID (see UL documentation for device IDs). # If use_device_detection is set to False, the board_num variable needs to # match the desired board number configured with Instacal. use_device_detection = True dev_id_list = [] board_num = 0 memhandle = None try: if use_device_detection: config_first_detected_device(board_num, dev_id_list) daq_dev_info = DaqDeviceInfo(board_num) if not daq_dev_info.supports_analog_output: raise Exception('Error: The DAQ device does not support ' 'analog output') print('\nActive DAQ device: ', daq_dev_info.product_name, ' (', daq_dev_info.unique_id, ')\n', sep='') ao_info = daq_dev_info.get_ao_info() low_chan = 0 high_chan = min(3, ao_info.num_chans - 1) num_chans = high_chan - low_chan + 1 rate = 100 points_per_channel = 1000 total_count = points_per_channel * num_chans ao_range = ao_info.supported_ranges[0] # Allocate a buffer for the scan memhandle = ul.win_buf_alloc(total_count) # Convert the memhandle to a ctypes array # Note: the ctypes array will no longer be valid after win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # before the memory is freed. The copy can be used at any time. ctypes_array = cast(memhandle, POINTER(c_ushort)) # Check if the buffer was successfully allocated if not memhandle: raise Exception('Error: Failed to allocate memory') frequencies = add_example_data(board_num, ctypes_array, ao_range, num_chans, rate, points_per_channel) for ch_num in range(low_chan, high_chan + 1): print('Channel', ch_num, 'Output Signal Frequency:', frequencies[ch_num - low_chan]) # Start the scan ul.a_out_scan(board_num, low_chan, high_chan, total_count, rate, ao_range, memhandle, ScanOptions.BACKGROUND) # Wait for the scan to complete print('Waiting for output scan to complete...', end='') status = Status.RUNNING while status != Status.IDLE: print('.', end='') # Slow down the status check so as not to flood the CPU sleep(0.5) status, _, _ = ul.get_status(board_num, FunctionType.AOFUNCTION) print('') print('Scan completed successfully') except Exception as e: print('\n', e) finally: if memhandle: # Free the buffer in a finally block to prevent a memory leak. ul.win_buf_free(memhandle) if use_device_detection: ul.release_daq_device(board_num)
def run_example(): board_num = 0 rate = 100 points_per_channel = 1000 if use_device_detection: ul.ignore_instacal() if not util.config_first_detected_device(board_num): print("Could not find device.") return ai_props = AnalogInputProps(board_num) if ai_props.num_ai_chans < 1: util.print_unsupported_example(board_num) return low_chan = 0 high_chan = min(3, ai_props.num_ai_chans - 1) num_chans = high_chan - low_chan + 1 total_count = points_per_channel * num_chans ai_range = ai_props.available_ranges[0] scan_options = ScanOptions.BACKGROUND if ScanOptions.SCALEDATA in ai_props.supported_scan_options: # If the hardware supports the SCALEDATA option, it is easiest to # use it. scan_options |= ScanOptions.SCALEDATA memhandle = ul.scaled_win_buf_alloc(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array_scaled method for scaled # buffers. ctypes_array = util.memhandle_as_ctypes_array_scaled(memhandle) elif ai_props.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= 16 memhandle = ul.win_buf_alloc(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16. ctypes_array = util.memhandle_as_ctypes_array(memhandle) else: # Use the win_buf_alloc_32 method for devices with a resolution > 16 memhandle = ul.win_buf_alloc_32(total_count) # Convert the memhandle to a ctypes array. # Use the memhandle_as_ctypes_array_32 method for devices with a # resolution > 16 ctypes_array = util.memhandle_as_ctypes_array_32(memhandle) # Note: the ctypes array will no longer be valid after win_buf_free is # called. # A copy of the buffer can be created using win_buf_to_array or # win_buf_to_array_32 before the memory is freed. The copy can be used # at any time. # Check if the buffer was successfully allocated if not memhandle: print("Failed to allocate memory.") return try: # Start the scan ul.a_in_scan(board_num, low_chan, high_chan, total_count, rate, ai_range, memhandle, scan_options) # Create a format string that aligns the data in columns row_format = "{:>12}" * num_chans # Print the channel name headers labels = [] for ch_num in range(low_chan, high_chan + 1): labels.append("CH" + str(ch_num)) print(row_format.format(*labels)) # Start updating the displayed values status, curr_count, curr_index = ul.get_status(board_num, FunctionType.AIFUNCTION) while status != Status.IDLE: # Make sure a data point is available for display. if curr_count > 0: # curr_index points to the start of the last completed # channel scan that was transferred between the board and # the data buffer. Display the latest value for each # channel. display_data = [] for data_index in range(curr_index, curr_index + num_chans): if ScanOptions.SCALEDATA in scan_options: # If the SCALEDATA ScanOption was used, the values # in the array are already in engineering units. eng_value = ctypes_array[data_index] else: # If the SCALEDATA ScanOption was NOT used, the # values in the array must be converted to # engineering units using ul.to_eng_units(). eng_value = ul.to_eng_units(board_num, ai_range, ctypes_array[data_index]) display_data.append('{:.3f}'.format(eng_value)) print(row_format.format(*display_data)) # Wait a while before adding more values to the display. time.sleep(0.5) status, curr_count, curr_index = ul.get_status( board_num, FunctionType.AIFUNCTION) # Stop the background operation (this is required even if the # scan completes successfully) ul.stop_background(board_num, FunctionType.AIFUNCTION) print("Scan completed successfully.") except ULError as e: util.print_ul_error(e) finally: # Free the buffer in a finally block to prevent errors from causing # a memory leak. ul.win_buf_free(memhandle) if use_device_detection: ul.release_daq_device(board_num)
def start_scan(self): self.low_chan = self.get_low_channel_num() self.high_chan = self.get_high_channel_num() self.num_chans = self.high_chan - self.low_chan + 1 if self.low_chan > self.high_chan: messagebox.showerror( "Error", "Low Channel Number must be greater than or equal to High " "Channel Number") self.set_ui_idle_state() return rate = 100 points_per_channel = 1000 # Some hardware requires that the total_count is an integer multiple # of the packet size. For this case, calculate a points_per_channel # that is equal to or just above the points_per_channel selected # which matches that requirement. if self.ai_props.continuous_requires_packet_size_multiple: packet_size = self.ai_props.packet_size remainder = points_per_channel % packet_size if remainder != 0: points_per_channel += packet_size - remainder total_count = points_per_channel * self.num_chans range_ = self.ai_props.available_ranges[0] scan_options = ScanOptions.BACKGROUND | ScanOptions.CONTINUOUS # Allocate a buffer for the scan if self.ai_props.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= # 16 self.memhandle = ul.win_buf_alloc(total_count) else: # Use the win_buf_alloc_32 method for devices with a resolution # > 16 self.memhandle = ul.win_buf_alloc_32(total_count) if not self.memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.set_ui_idle_state() return # Create the frames that will hold the data self.recreate_data_frame() try: # Run the scan ul.a_in_scan(self.board_num, self.low_chan, self.high_chan, total_count, rate, range_, self.memhandle, scan_options) except ULError as e: self.show_ul_error(e) self.set_ui_idle_state() return # Convert the memhandle to a ctypes array # Note: the ctypes array will no longer be valid after win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # or win_buf_to_array_32 before the memory is freed. The copy can # be used at any time. if self.ai_props.resolution <= 16: # Use the memhandle_as_ctypes_array method for devices with a # resolution <= 16 self.ctypes_array = self.memhandle_as_ctypes_array(self.memhandle) else: # Use the memhandle_as_ctypes_array_32 method for devices with a # resolution > 16 self.ctypes_array = self.memhandle_as_ctypes_array_32( self.memhandle) # Start updating the displayed values self.update_displayed_values(range_)
def start_scan(self): range_ = self.ai_props.available_ranges[0] low_chan = self.get_low_channel_num() high_chan = self.get_high_channel_num() trig_type = self.get_trigger_type() trig_value_eng = self.get_trigger_level() trig_value = ul.from_eng_units( self.board_num, range_, trig_value_eng) if low_chan > high_chan: messagebox.showerror( "Error", "Low Channel Number must be greater than or equal to High " "Channel Number") self.start_button["state"] = tk.NORMAL return rate = 100 points_per_channel = 10 num_channels = high_chan - low_chan + 1 total_count = points_per_channel * num_channels pretrig_points_per_channel = 5 total_pretrig_count = pretrig_points_per_channel * num_channels # Allocate a buffer for the scan if self.ai_props.resolution <= 16: # Use the win_buf_alloc method for devices with a resolution <= # 16 memhandle = ul.win_buf_alloc(total_count) else: messagebox.showerror( "Error", "This example can only be used with boards with a " "resolution less than or equal to 16.") self.start_button["state"] = tk.NORMAL return # Check if the buffer was successfully allocated if not memhandle: messagebox.showerror("Error", "Failed to allocate memory") self.start_button["state"] = tk.NORMAL return try: # Set the trigger settings (the level will be used for # both thresholds, since the irrelevant threshold is ignored # for TRIG_ABOVE and TRIG_BELOW ul.set_trigger( self.board_num, trig_type, trig_value, trig_value) # Run the scan ul.a_pretrig( self.board_num, low_chan, high_chan, total_pretrig_count, total_count, rate, range_, memhandle, 0) # Convert the memhandle to a ctypes array # Note: the ctypes array will only be valid until win_buf_free # is called. # A copy of the buffer can be created using win_buf_to_array # before the memory is freed. The copy can be used at any time. array = self.memhandle_as_ctypes_array(memhandle) # Display the values self.display_values(array, range_, total_count, low_chan, high_chan) except ULError as e: self.show_ul_error(e) finally: # Free the allocated memory ul.win_buf_free(memhandle) self.start_button["state"] = tk.NORMAL