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 # 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 total_count = 10 # Allocate a buffer for the scan 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: mode = CounterMode.ENCODER + CounterMode.ENCODER_MODE_X1 \ + CounterMode.ENCODER_MODE_CLEAR_ON_Z_ON debounce_time = CounterDebounceTime.DEBOUNCE_NONE debounce_mode = 0 edge_detection = CounterEdgeDetection.RISING_EDGE tick_size = CounterTickSize.TICK20PT83ns mapped_channel = 2 # Configure the first counter channel for Encoder mode ul.c_config_scan(self.board_num, self.chan_num, mode, debounce_time, debounce_mode, edge_detection, tick_size, mapped_channel) # Run the scan ul.c_in_scan(self.board_num, self.chan_num, self.chan_num, total_count, rate, 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_32 # before the memory is freed. The copy can be used at any time. array = self.memhandle_as_ctypes_array_32(memhandle) # Display the values self.display_values(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 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 # Allocate a buffer for the scan 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.c_in_scan(self.board_num, low_chan, high_chan, total_count, rate, 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_32 # before the memory is freed. The copy can be used at any time. array = self.memhandle_as_ctypes_array_32(memhandle) # Display the values self.display_values(array, 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): rate = 390 total_count = 100 # Allocate a buffer for the scan 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: # Configure the counter ul.c_config_scan( self.board_num, self.chan_num, CounterMode.DECREMENT_ON, CounterDebounceTime.DEBOUNCE_NONE, 0, CounterEdgeDetection.FALLING_EDGE, CounterTickSize.TICK20PT83ns, 1) # Run the scan ul.c_in_scan( self.board_num, self.chan_num, self.chan_num, total_count, rate, 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_32 # before the memory is freed. The copy can be used at any time. 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): 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 = 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 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 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 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)