以下適用于Windows的Python示例演示了設(shè)備發(fā)現(xiàn)和連續(xù)的后臺獲取。
開始后,使用獲取狀態(tài)功能監(jiān)視采集,該功能返回總計數(shù)和緩沖區(qū)索引位置。當(dāng)數(shù)據(jù)已滿一半(索引>一半)時,將從設(shè)備緩沖區(qū)中檢索數(shù)據(jù)。發(fā)生這種情況時,它將數(shù)據(jù)從設(shè)備緩沖區(qū)傳輸?shù)接脩艟彌_區(qū)。采集循環(huán)繼續(xù)監(jiān)視索引,并且當(dāng)索引翻轉(zhuǎn)到零時,將從設(shè)備緩沖區(qū)的上半部分傳輸數(shù)據(jù)。它繼續(xù)在下半部和上半部之間進(jìn)行此后n次或ping轉(zhuǎn)換。
數(shù)據(jù)通過ScaleData功能自動縮放到伏特。這需要使用scaled_win_buf_alloc函數(shù)分配內(nèi)存,并使用scaled_win_buf_to_array函數(shù) 傳輸數(shù)據(jù)。ScaleOption.ScaleData通知AInScan函數(shù)正在使用ScaleData功能。為簡單起見,每次傳輸緩沖區(qū)時僅打印一行數(shù)據(jù)。
要安裝Python for Windows支持,請將以下URL復(fù)制到瀏覽器中,然后按照提供的說明進(jìn)行操作。
本示例使用Python 3.8.5和PyCharm構(gòu)建。該代碼模塊通過本文結(jié)尾處的下載鏈接提供。
來自__future__ import absolute_import,div,print_function
導(dǎo)入時間
從內(nèi)置導(dǎo)入* * @UnusedWildImport
從進(jìn)口睡眠開始
從ctypes import cast,POINTER,c_double,c_ushort,c_ulong
從mcculw import ul
從mcculw.enums導(dǎo)入ScanOptions,FunctionType,狀態(tài)
從mcculw.ul導(dǎo)入ULError,a_input_mode
從mcculw.enums導(dǎo)入InterfaceType
從mcculw.enums導(dǎo)入ULRange
從mcculw.enums導(dǎo)入AnalogInputMode
use_device_detection =真
def run_example():
board_num = 0
board_index = 0
find_device =“ USB-1608G ”
如果use_device_detection:
board_num = -1
ul.ignore_instacal()
dev_list = ul.get_daq_device_inventory(InterfaceType.USB)
如果len(dev_list)> 0:
對于dev_list中的設(shè)備:
如果str(設(shè)備)== find_device:
打印(f“找到的{find_device}板號= {board_index}“)
print(f“序列號:{device.unique_id}”)
打印(f“產(chǎn)品類型:{hex(device.product_id)}”)
board_num = board_index
ul.create_daq_device(board_num,設(shè)備)
board_index = board_index + 1
如果board_num == -1:
打印(f“找不到設(shè)備{find_device}”)
返回
其他:
打印(“未檢測到設(shè)備”)
返回
#**********發(fā)現(xiàn)結(jié)束************
率= 1000
points_per_channel = 1000
low_chan = 0
high_chan = 3
num_chans = 4
total_count = points_per_channel * num_chans
half_count = int(總計數(shù)/ 2)
#SCALEDATA選項,返回伏特而不是A / D計數(shù)
scan_options = ScanOptions.CONTINUOUS | ScanOptions.BACKGROUND | ScanOptions.SCALEDATA
內(nèi)存句柄= ul.scaled_win_buf_alloc(總數(shù))
buf_data = cast(內(nèi)存句柄,POINTER(c_double))
#檢查緩沖區(qū)是否成功分配
如果不是成員:
print(“分配內(nèi)存失敗。”)
返回
a_input_mode(board_num,AnalogInputMode.SINGLE_ENDED)
嘗試:
#開始掃描
ul.a_in_scan(
board_num,low_chan,high_chan,total_count,
速率,ULRange.BIP10VOLTS,內(nèi)存句柄,scan_options)
#創(chuàng)建一個格式字符串,以對齊列中的數(shù)據(jù)
#加兩個curr_index和curr_count
row_format =“ {:8}” *(num_chans + 3)
#打印頻道名稱標(biāo)題
標(biāo)簽= []
對于范圍內(nèi)的ch_num(low_chan,high_chan + 1):
labels.append(“ CH” + str(ch_num)+“ \ t”)
labels.append(“ index \ t”)
labels.append(“ count \ t”)
labels.append(“ diff”)
打印(row_format.format(* labels))
#布爾標(biāo)志用于切換讀取上下緩沖區(qū)
read_lower =真
#開始更新顯示的值
狀態(tài),curr_count,curr_index = ul.get_status(
board_num,FunctionType.AIFUNCTION)
最后= 0
差異= 0
而狀態(tài)!= Status.IDLE并且curr_count <100000:
#確保數(shù)據(jù)點可用于顯示。
如果curr_count> 0:
#curr_index指向最后完成的開始
#在電路板和
#數(shù)據(jù)緩沖區(qū)。顯示每個的最新值
#頻道。
#display_data = []
如果(curr_index> half_count)和(read_lower == True):
diff = curr_count-最后
ul.scaled_win_buf_to_array(memhandle,buf_data,0,int(half_count))
打印(
'{:.3f} \ t {:.3f} \ t {:.3f} \ t {:.3f} \ t {:d} \ t {:d} \ t {:d}'。format(buf_data [0],buf_data [1],
buf_data [2],
buf_data [3],curr_index,
curr_count,
diff))
最后= curr_count
read_lower = False
elif(curr_index <half_count)和(read_lower == False):
diff = curr_count-最后
ul.scaled_win_buf_to_array(內(nèi)存句柄,buf_data,int(一半計數(shù)),int(一半計數(shù)))
打印(
'{:.3f} \ t {:.3f} \ t {:.3f} \ t {:.3f} \ t {:d} \ t {:d} \ t {:d}'。format(buf_data [0],buf_data [1],
buf_data [2],
buf_data [3],curr_index,
curr_count,
diff))
最后= curr_count
read_lower =真
睡眠(0.1)
狀態(tài),curr_count,curr_index = ul.get_status(
board_num,FunctionType.AIFUNCTION)
#停止后臺操作(即使
#掃描成功完成)
ul.stop_background(board_num,FunctionType.AIFUNCTION)
打印(“掃描成功完成。”)
除了ULError作為e:
util.print_ul_error(e)
最后:
#釋放finally塊中的緩沖區(qū),以防止導(dǎo)致錯誤
#內(nèi)存泄漏。
ul.win_buf_free(內(nèi)存句柄)
如果use_device_detection:
ul.release_daq_device(board_num)
如果__name__ =='__main__':
run_example()