以下 Python for Windows 示例演示了設(shè)備發(fā)現(xiàn)和連續(xù)后臺獲取。
開始后,將使用返回總計數(shù)和緩沖區(qū)索引位置的獲取狀態(tài)函數(shù)來監(jiān)視采集。當(dāng)數(shù)據(jù)超過一半(索引 > 一半)時,從設(shè)備緩沖區(qū)中檢索數(shù)據(jù)。發(fā)生這種情況時,它會將數(shù)據(jù)從設(shè)備緩沖區(qū)傳輸?shù)接脩艟彌_區(qū)。采集循環(huán)繼續(xù)監(jiān)視索引,當(dāng)它回滾到零時,從設(shè)備緩沖區(qū)的上半部分傳輸數(shù)據(jù)。它繼續(xù)在下半部和上半部之間來回或乒乓球。
數(shù)據(jù)通過 ScaleData 功能自動縮放到伏特。這需要使用函數(shù) scaled_win_buf_alloc 分配內(nèi)存和 scaled_win_buf_to_array 來傳輸數(shù)據(jù)。ScaleOption.ScaleData 通知 AInScan 函數(shù) ScaleData 功能正在使用中。為簡單起見,每次傳輸緩沖區(qū)時僅打印一行數(shù)據(jù)。
要安裝 Python for Windows 支持,請將以下 URL 復(fù)制到瀏覽器中并按照提供的說明進(jìn)行操作。
https://github.com/mccdaq/mcculw
此示例是使用 Python 3.8.5 和 PyCharm 構(gòu)建的。代碼模塊通過本文末尾的下載鏈接提供。
from __future__ import absolute_import、除法、print_function
導(dǎo)入時間
from builtins import * # @UnusedWildImport
從時間導(dǎo)入睡眠
從 ctypes 導(dǎo)入強(qiáng)制轉(zhuǎn)換、指針、c_double、c_ushort、c_ulong
從 mcculw 進(jìn)口 ul
從 mcculw.enums 導(dǎo)入 ScanOptions、FunctionType、Status
從 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 = True
def run_example():
board_num = 0
board_index = 0
find_device = " USB-1608G "
如果使用設(shè)備檢測:
board_num = -1
ul.ignore_instacal()
dev_list = ul.get_daq_device_inventory(InterfaceType.USB)
如果 len(dev_list) > 0:
對于 dev_list 中的設(shè)備:
如果 str(device) == find_device:
打印(f“找到{find_device}板號= {board_index}”)
打印(f“序列號:{device.unique_id}”)
打印(f“產(chǎn)品類型:{hex(device.product_id)}”)
board_num = board_index
ul.create_daq_device(board_num, device)
board_index = board_index + 1
如果 board_num == -1:
打印(f“設(shè)備{find_device}未找到”)
返回
別的:
print("未檢測到設(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(total_count / 2)
# SCALEDATA 選項,返回伏特而不是 A/D 計數(shù)
scan_options = ScanOptions.CONTINUOUS | ScanOptions.背景 | ScanOptions.SCALEDATA
memhandle = ul.scaled_win_buf_alloc(total_count)
buf_data = cast(memhandle, POINTER(c_double))
# 檢查緩沖區(qū)是否分配成功
如果不是內(nèi)存句柄:
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,memhandle,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):
label.append("CH" + str(ch_num) + "\t")
label.append("index\t")
label.append("count\t")
標(biāo)簽.附加(“差異”)
打印(行格式。格式(*標(biāo)簽))
# 布爾標(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ù)耐ǖ罀呙?/p>
# 數(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,
差異))
最后 = curr_count
read_lower = 假
elif (curr_index < half_count) 和 (read_lower == False):
diff = curr_count - 最后一個
ul.scaled_win_buf_to_array(memhandle, buf_data, int(half_count), 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,
差異))
最后 = 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)
print("掃描成功完成。")
除了 ULError 為 e:
util.print_ul_error(e)
最后:
# 釋放finally塊中的緩沖區(qū)以防止導(dǎo)致錯誤
# 內(nèi)存泄漏。
ul.win_buf_free(memhandle)
如果使用設(shè)備檢測:
ul.release_daq_device(board_num)
如果 __name__ == '__main__':
運行示例()