以下 Python for Windows 示例演示了設(shè)備發(fā)現(xiàn)以及USB-CTR04以與計(jì)數(shù)器通道相同的速率對(duì)數(shù)字端口進(jìn)行采樣的能力。
這是使用 ul.daq_in_scan() 而不是 ul.c_in_scan() 的一次性采集,不連續(xù)。使用一次性或有限模式(默認(rèn)),ul.daq_in_scan() 函數(shù)將阻塞,直到數(shù)據(jù)被捕獲到緩沖區(qū)。
程序配置了兩個(gè)計(jì)數(shù)器通道,第一個(gè)計(jì)數(shù)器設(shè)置為范圍計(jì)數(shù)。輸出線被啟用并由輸出寄存器控制。第二計(jì)數(shù)器被配置為測量周期。這個(gè)想法是第一個(gè)計(jì)數(shù)器將輸入頻率除以 100,第二個(gè)計(jì)數(shù)器對(duì)其進(jìn)行測量。對(duì)于測試信號(hào),使用 TMR0 線(頻率脈沖輸出)。推薦連接方式為 TMR0-->C0IN 和 C0O-->C1IN
要安裝 Python for Windows 支持,請(qǐng)將以下 URL 復(fù)制到瀏覽器中并按照提供的說明進(jìn)行操作。
此示例是使用 Python 3.8.5 和 PyCharm 構(gòu)建的。代碼模塊通過本文末尾的下載鏈接提供。

"""
文件:daq_in_scan_usb_1800.py
展示的庫調(diào)用:mcculw.ul.daq_in_scan()
目的:同步掃描數(shù)字端口和計(jì)數(shù)器
在前臺(tái)使用USB-CTR04 64 位支持。
演示:從數(shù)字端口和兩個(gè)計(jì)數(shù)器輸入收集數(shù)據(jù)。
其他庫調(diào)用:mcculw.ul.ignore_instacal()
mcculw.ul.get_daq_device_inventory()
mcculw.ul.create_daq_device()
mcculw.ul.win_buf_alloc_64()
mcculw.ul.c_config_scan()
mcculw.ul.pulse_out_start()
mcculw.ul.daq_in_scan()
mcculw.ul.pulse_out_stop()
mcculw.ul.win_buf_free()
mcculw.ul.release_daq_device()
特殊要求:此示例過濾器適用于 USB-1808 系列。
"""
from __future__ import absolute_import、除法、print_function
from builtins import * # @UnusedWildImport
從 ctypes 導(dǎo)入轉(zhuǎn)換、指針、c_double、c_ulonglong、c_ubyte、c_ushort
從 mcculw 進(jìn)口 ul
從 mcculw.enums 導(dǎo)入 ScanOptions、ChannelType、ULRange、DigitalPortType
從 mcculw.enums 導(dǎo)入 InterfaceType、FunctionType
從 mcculw.enums 導(dǎo)入 CounterMode、CounterDebounceMode、CounterDebounceTime
從 mcculw.enums 導(dǎo)入 CounterEdgeDetection、CounterTickSize、CounterRegister
導(dǎo)入 ctypes
def run_example():
use_device_detection = True
內(nèi)存句柄 = 無
嘗試:
# **********開始設(shè)備發(fā)現(xiàn)************
board_num = 0
board_index = 0
find_device = " USB-CTR04 "
如果使用設(shè)備檢測:
board_num = -1
ul.ignore_instacal()
dev_list = ul.get_daq_device_inventory(InterfaceType.USB)
如果 len(dev_list) > 0:
對(duì)于 dev_list 中的設(shè)備:
如果 str(device) == find_device:
打印(f“找到{find_device}板號(hào)= {board_index}”)
打印(f“序列號(hào):{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è)備")
# **********設(shè)備發(fā)現(xiàn)結(jié)束************
# 將計(jì)數(shù)器配置為從 x 計(jì)數(shù)到 x+n 并帶有活動(dòng)輸出
ctr0_mode = CounterMode.TOTALIZE | CounterMode.RANGE_LIMIT_ON | CounterMode.OUTPUT_ON
如果 board_num > -1:
ul.c_config_scan(board_num,
0,
ctr0_mode,
CounterDebounceTime.DEBOUNCE_NONE,
CounterDebounceMode.TRIGGER_AFTER_STABLE,
CounterEdgeDetection.RISING_EDGE,
CounterTickSize.TICK20PT83ns,
0)
# 除以 100
ul.c_load_32(board_num, CounterRegister.MINLIMITREG0, 0)
ul.c_load_32(board_num, CounterRegister.MAXLIMITREG0, 99)
# 輸出將在零時(shí)切換為高電平,在 49 50% 占空比時(shí)切換為低電平
ul.c_load_32(board_num, CounterRegister.OUTPUTVAL0REG0, 0)
ul.c_load_32(board_num, CounterRegister.OUTPUTVAL1REG0, 49)
# 測量計(jì)數(shù)器 0 輸出的周期;將 C0O 連接到 C1IN。
ul.c_config_scan(board_num,
1、
CounterMode.PERIOD,
CounterDebounceTime.DEBOUNCE500ns,
CounterDebounceMode.TRIGGER_AFTER_STABLE,
CounterEdgeDetection.RISING_EDGE,
CounterTickSize.TICK20PT83ns,
0)
# tick_size 是分配給單次計(jì)數(shù)的時(shí)間量
刻度尺寸 = 0.000000020830
# 創(chuàng)建 daq_in_scan 通道配置列表
chan_list = []
chan_type_list = []
增益列表 = []
chan_list.append(0)
chan_type_list.append(ChannelType.CTRBANK0)
gain_list.append(ULRange.NOTUSED)
chan_list.append(0)
chan_type_list.append(ChannelType.CTRBANK1)
gain_list.append(ULRange.NOTUSED)
chan_list.append(0)
chan_type_list.append(ChannelType.CTRBANK2)
gain_list.append(ULRange.NOTUSED)
chan_list.append(0)
chan_type_list.append(ChannelType.CTRBANK3)
gain_list.append(ULRange.NOTUSED)
chan_list.append(1)
chan_type_list.append(ChannelType.CTRBANK0)
gain_list.append(ULRange.NOTUSED)
chan_list.append(1)
chan_type_list.append(ChannelType.CTRBANK1)
gain_list.append(ULRange.NOTUSED)
chan_list.append(1)
chan_type_list.append(ChannelType.CTRBANK2)
gain_list.append(ULRange.NOTUSED)
chan_list.append(1)
chan_type_list.append(ChannelType.CTRBANK3)
gain_list.append(ULRange.NOTUSED)
# 數(shù)字端口是 8 位,但它是
# 作為一個(gè) 16 位寄存器處理;使用 PADZERO 作為
# 其他 48 位的虛擬占位符
chan_list.append(DigitalPortType.AUXPORT)
chan_type_list.append(ChannelType.DIGITAL8)
gain_list.append(ULRange.NOTUSED)
chan_list.append(DigitalPortType.AUXPORT)
chan_type_list.append(ChannelType.PADZERO)
gain_list.append(ULRange.NOTUSED)
chan_list.append(DigitalPortType.AUXPORT)
chan_type_list.append(ChannelType.PADZERO)
gain_list.append(ULRange.NOTUSED)
chan_list.append(DigitalPortType.AUXPORT)
chan_type_list.append(ChannelType.PADZERO)
gain_list.append(ULRange.NOTUSED)
timer_num0 = 0
頻率 = 20000
占空比 = 0.5
# 啟動(dòng)脈沖定時(shí)器輸出(可選參數(shù)省略)
# 用作測試信號(hào)
# 將 TMR0 連接到 C0IN
# 計(jì)數(shù)器 0 將除以它的范圍限制量
實(shí)際頻率,實(shí)際占空比,_ = ul.pulse_out_start(
board_num、timer_num0、頻率、占空比)
num_channels = len(chan_list) # 16 位位置的數(shù)量
num_64_bit_channels = 3 # 64 位通道數(shù)
比率 = 2000
num_rows = 384 # 掃描次數(shù),必須是 num_64_bit_channels 的倍數(shù)
total_count = num_channels * num_rows # 12 * 96
# 為掃描分配內(nèi)存并將其轉(zhuǎn)換為 ctypes 數(shù)組指針
# 默認(rèn)寄存器大小為 64 位
memhandle = ul.win_buf_alloc_64(total_count)
# 檢查緩沖區(qū)是否分配成功
如果不是內(nèi)存句柄:
raise Exception('錯(cuò)誤:分配內(nèi)存失敗')
緩沖區(qū) = 演員表(內(nèi)存句柄,指針(c_ulonglong))
嘗試:
# 開始掃描
ul.daq_in_scan(board_num,
chan_list,
chan_type_list,
增益列表,
num_channels,
速度,
0,
總數(shù),
內(nèi)存句柄,
ScanOptions.DEFAULTIO)
除了作為 e 的例外:
print('daq_in_scan 錯(cuò)誤 >>>', e)
print('掃描成功完成。')
# 開始更新顯示的值
狀態(tài),curr_count,curr_index = ul.get_status(
board_num, FunctionType.DAQIFUNCTION)
打印(f“狀態(tài){狀態(tài)}計(jì)數(shù){curr_count}索引{curr_index}”)
# 停止 TMR0 和 TMR1
ul.pulse_out_stop(board_num, timer_num0)
打印(“指數(shù)\tCOIN\tC1IN\tDI”)
# 打印數(shù)據(jù)循環(huán) - 數(shù)字端口只有 8 位所以使用掩碼歸零
# 高位
指數(shù) = 0
掩碼 = 0xFF
對(duì)于我在范圍內(nèi)(num_rows):
如果緩沖區(qū)[索引 + 1] > 0:
# 如果計(jì)數(shù)不為零,則計(jì)算頻率
f = 1 / (buffer[index + 1] * tick_size)
別的:
f = 0.01
打印(f"{i:d}\t{buffer[index]:d}\t{f:5.2f} Hz \t{(buffer[index+2] & mask):d}")
索引 += num_64_bit_channels
除了作為 e 的例外:
打印('\n',e)
最后:
如果內(nèi)存句柄:
# 在 finally 塊中釋放緩沖區(qū)以防止內(nèi)存泄漏。
ul.win_buf_free(memhandle)
如果使用設(shè)備檢測:
ul.release_daq_device(board_num)
如果 __name__ == '__main__':
運(yùn)行示例()