以下適用于Windows的Python示例演示了設備發現和連續的后臺獲取。
一旦開始,將使用獲取狀態功能監視采集,該功能返回總計數和緩沖區索引位置。當數據已滿一半(索引>一半)時,將從設備緩沖區中檢索數據。發生這種情況時,它將數據從設備緩沖區傳輸到用戶緩沖區。采集循環繼續監視索引,并且當索引翻轉到零時,將從設備緩沖區的上半部分傳輸數據。它繼續在下半部和上半部之間進行此后n次或ping轉換。
通道0配置為讀取正交編碼器,而通道1將提供累加計數。
要安裝Python for Windows支持,請將以下URL復制到瀏覽器中,然后按照提供的說明進行操作。
https://github.com/mccdaq/mcculw
本示例使用Python 3.8.5和PyCharm構建。本文末尾通過下載鏈接提供了代碼模塊。
來自__future__ import absolute_import,division,print_function
導入時間
從內置導入*#@UnusedWildImport
從進口睡眠開始
從ctypes import cast,POINTER,c_double,c_ushort,c_ulong
從mcculw import ul
從mcculw.enums導入ScanOptions,FunctionType,狀態
從mcculw.ul導入ULError,a_input_mode
從mcculw.enums導入InterfaceType
從mcculw.enums導入CounterMode
從mcculw.enums導入CounterDebounceMode
從mcculw.enums導入CounterDebounceTime
從mcculw.enums導入CounterEdgeDetection
從mcculw.enums導入CounterTickSize
use_device_detection =真
def run_example():
board_num = 0
board_index = 0
find_device =“ USB-QUAD08 ”
如果use_device_detection:
board_num = -1
ul.ignore_instacal()
dev_list = ul.get_daq_device_inventory(InterfaceType.USB)
如果len(dev_list)> 0:
對于dev_list中的設備:
如果str(設備)== find_device:
打印(f“找到的{find_device}板號= {board_index}“)
print(f“序列號:{device.unique_id}”)
打印(f“產品類型:{hex(device.product_id)}”)
board_num = board_index
ul.create_daq_device(board_num,設備)
board_index = board_index + 1
如果board_num == -1:
打印(f“找不到設備{find_device}”)
返回
別的:
打印(“未檢測到設備”)
返回
#**********發現結束************
率= 200
points_per_channel = 40
low_chan = 0
high_chan = 1
num_chans = 2
total_count = points_per_channel * num_chans
half_count = int(總計數/ 2)
#SCALEDATA選項,返回伏特而不是A / D計數
scan_options = ScanOptions.CONTINUOUS | ScanOptions.BACKGROUND | ScanOptions.CTR32BIT
memhandle = ul.win_buf_alloc_32(總數)
buf_data = cast(內存句柄,POINTER(c_ulong))
#檢查緩沖區是否成功分配
如果不是成員:
print(“分配內存失敗。”)
返回
嘗試:
ul.c_config_scan(board_num,
low_chan,
CounterMode.ENCODER | CounterMode.ENCODER_MODE_BIT_32,
CounterDebounceTime.DEBOUNCE500ns,
CounterDebounceMode.TRIGGER_AFTER_STABLE,
CounterEdgeDetection.RISING_EDGE,
CounterTickSize.TICK20PT83ns,
low_chan)
ul.c_config_scan(board_num,
高陳
CounterMode.TOTALIZE | CounterMode.BIT_32,
CounterDebounceTime.DEBOUNCE500ns,
CounterDebounceMode.TRIGGER_AFTER_STABLE,
CounterEdgeDetection.RISING_EDGE,
CounterTickSize.TICK20PT83ns,
high_chan)
#開始掃描
ul.c_in_scan(
board_num,low_chan,high_chan,total_count,
費率,記憶句柄,scan_options)
#創建一個格式字符串,以對齊列中的數據
#加兩個curr_index和curr_count
row_format =“ {:8}” *(num_chans)
#打印頻道名稱標題
標簽= []
對于范圍內的ch_num(low_chan,high_chan + 1):
labels.append(“ CH” + str(ch_num)+“ \ t”)
打印(row_format.format(* labels))
#布爾標志用于切換讀取上下緩沖區
read_lower =真
#開始更新顯示的值
狀態,curr_count,curr_index = ul.get_status(
board_num,FunctionType.CTRFUNCTION)
最后= 0
差異= 0
當狀態!= Status.IDLE并且curr_count <5000時:
#確保數據點可用于顯示。
如果curr_count> 0:
#curr_index指向最后完成的開始
#在電路板和控制面板之間傳輸的通道掃描
#數據緩沖區。顯示每個的最新值
# 渠道。
#display_data = []
如果(curr_index> half_count)和(read_lower == True):
ul.win_buf_to_array_32(內存句柄,buf_data,0,int(half_count))
print('{:d} \ t \ t {:d}'。format(buf_data [0],buf_data [1]))
read_lower = False
elif(curr_index <half_count)和(read_lower == False):
ul.win_buf_to_array_32(內存句柄,buf_data,int(一半計數),int(一半計數))
print('{:d} \ t \ t {:d}'。format(buf_data [0],buf_data [1]))
read_lower =真
睡眠(0.1)
狀態,curr_count,curr_index = ul.get_status(
board_num,FunctionType.CTRFUNCTION)
#停止后臺操作(即使
#掃描成功完成)
ul.stop_background(board_num,FunctionType.AIFUNCTION)
打印(“掃描成功完成。”)
除了ULError作為e:
打印(“ UL錯誤\ n”,e)
最后:
#釋放finally塊中的緩沖區,以防止導致錯誤
#內存泄漏。
ul.win_buf_free(內存句柄)
如果use_device_detection:
ul.release_daq_device(board_num)
如果__name__ =='__main__':
run_example()