這是一個(gè)特定于產(chǎn)品的示例,演示了如何使用USB-CTR04或USB-CTR08計(jì)數(shù)器選項(xiàng)。計(jì)數(shù)器 0 配置為范圍計(jì)數(shù),將從 0 連續(xù)計(jì)數(shù)到 9999。 還演示了門控和計(jì)數(shù)器輸出。高電平時(shí)的門允許計(jì)數(shù),當(dāng)計(jì)數(shù)達(dá)到 5000 時(shí)輸出將切換,計(jì)數(shù)器 1 用于對轉(zhuǎn)換進(jìn)行計(jì)數(shù)。定時(shí)器輸出用作測試信號。要使用此示例,請將 TMR0 連接到 C0IN;將 DIO0 連接到 C0GT,將 C0O 連接到 C1IN。
附上 Python 代碼文件...
#!/usr/bin/env python
# -*- 編碼:UTF-8 -*-
"""
UL 調(diào)用演示:CtrDevice.c_config_scan()
目的:將計(jì)數(shù)器配置為 COUNT;
在第一個(gè)計(jì)數(shù)器上啟用門控;
在第一個(gè)計(jì)數(shù)器上啟用輸出;
用輸出指定范圍計(jì)數(shù);
第二個(gè)計(jì)數(shù)器只計(jì)算輸出轉(zhuǎn)換
從第一個(gè)柜臺
演示:顯示事件計(jì)數(shù)器數(shù)據(jù)
指定的編碼器
"""
從 __future__ 導(dǎo)入 print_function
從時(shí)間導(dǎo)入睡眠
從操作系統(tǒng)導(dǎo)入系統(tǒng)
從系統(tǒng)導(dǎo)入標(biāo)準(zhǔn)輸出
從 uldaq 導(dǎo)入(get_daq_device_inventory、InterfaceType、ScanStatus、
ScanOption、CInScanFlag、CounterMeasurementType、
CounterMeasurementMode、CounterEdgeDetection、
CounterTickSize、CounterDebounceMode、CounterDebounceTime、CounterRegisterType、
CConfigScanFlag, create_int_buffer, DaqDevice,
TmrIdleState、PulseOutOption、DigitalDirection、DigitalPortType)
定義主():
"""計(jì)數(shù)器輸入掃描與編碼器示例。"""
daq_device = 無
ctr_device = 無
狀態(tài) = ScanStatus.IDLE
interface_type = InterfaceType.USB
第一頻道 = 0
第二頻道 = 1
通道數(shù) = 2
采樣率 = 100.0 #赫茲
samples_per_channel = 100
scan_options = ScanOption.CONTINUOUS
scan_flags = CInScanFlag.DEFAULT + CInScanFlag.CTR32_BIT
timer_number = 0
頻率 = 10000.0 #Hz
占空比 = 0.5 # 50%
pulse_count = 0 # 連續(xù)
初始延遲 = 0.0
idle_state = TmrIdleState.LOW
選項(xiàng) = PulseOutOption.DEFAULT
嘗試:
# 獲取所有可用 DAQ 設(shè)備的描述符。
設(shè)備 = get_daq_device_inventory(interface_type)
number_of_devices = len(設(shè)備)
如果 number_of_devices == 0:
raise RuntimeError('錯(cuò)誤:未找到 DAQ 設(shè)備')
描述符索引 = 99
打印('找到',number_of_devices,'DAQ 設(shè)備:')
對于我在范圍內(nèi)(number_of_devices):
如果 devices[i] .product_name == " USB-CTR04 ":
描述符索引 = i
休息
描述符索引 = 整數(shù)(描述符索引)
如果descriptor_index 不在范圍內(nèi)(number_of_devices):
raise RuntimeError('錯(cuò)誤:無效的描述符索引')
# 從指定索引處的描述符創(chuàng)建DAQ設(shè)備。
daq_device = DaqDevice(devices[descriptor_index])
# 建立與DAQ設(shè)備的連接。
描述符 = daq_device.get_descriptor()
print('\n正在連接',descriptor.dev_string,'-請稍候...')
daq_device.connect(connection_code=0)
# 獲取CtrDevice對象并驗(yàn)證它是否有效。
ctr_device = daq_device.get_ctr_device()
mode = CounterMeasurementMode.GATING_ON | CounterMeasurementMode.OUTPUT_ON | CounterMeasurementMode.RANGE_LIMIT_ON
# 為編碼器配置計(jì)數(shù)器 0
ctr_device.c_config_scan(FirstChannel,
CounterMeasurementType.COUNT,
模式,
CounterEdgeDetection.RISING_EDGE,
CounterTickSize.TICK_20PT83ns,
CounterDebounceMode.NONE,
CounterDebounceTime.DEBOUNCE_0ns,
CConfigScanFlag.DEFAULT)
#counter 將計(jì)數(shù) 0 到 9999
ctr_device.c_load(FirstChannel,CounterRegisterType.MIN_LIMIT, 0)
ctr_device.c_load(FirstChannel,CounterRegisterType.MAX_LIMIT, 9999)
#counter 輸出線將從 0 到 4999 變高,然后從 5000 變低到 9999
ctr_device.c_load(FirstChannel,CounterRegisterType.OUTPUT_VAL0, 0)
ctr_device.c_load(FirstChannel,CounterRegisterType.OUTPUT_VAL1, 4999)
# 為周期配置計(jì)數(shù)器 1 以計(jì)算計(jì)數(shù)器 0 的輸出轉(zhuǎn)換
# 將 C1IN 連接到 C0O
ctr_device.c_config_scan(SecondChannel,
CounterMeasurementType.COUNT,
CounterMeasurementMode.DEFAULT,
CounterEdgeDetection.RISING_EDGE,
CounterTickSize.TICK_20PT83ns,
CounterDebounceMode.TRIGGER_AFTER_STABLE,
CounterDebounceTime.DEBOUNCE_500ns,
CConfigScanFlag.DEFAULT)
#wire TMR0 到 C0IN - 這是您的測試信號
tmr_device = daq_device.get_tmr_device()
(頻率,duty_cycle,initial_delay) = tmr_device.pulse_out_start(timer_number, frequency,
占空比,脈沖計(jì)數(shù),
初始延遲,空閑狀態(tài),
選項(xiàng))
dio_device = daq_device.get_dio_device()
dio_device.d_config_bit(DigitalPortType.AUXPORT, 0, DigitalDirection.OUTPUT)
#wire DIO0 到 C0GT - 將位設(shè)置為高以啟用計(jì)數(shù)
dio_device.d_bit_out(DigitalPortType.AUXPORT, 0, 1)
# 分配一個(gè)緩沖區(qū)來接收數(shù)據(jù)。
數(shù)據(jù) = create_int_buffer(ChannelCount, samples_per_channel)
print('\n',descriptor.dev_string,'ready',sep='')
print('函數(shù)演示:ctr_device.c_config_scan()')
打印('計(jì)數(shù)器:',FirstChannel,'-',SecondChannel)
打印('采樣率:',sample_rate,'Hz')
# 開始掃描
ctr_device.c_in_scan(FirstChannel,
第二頻道,
samples_per_channel,
采樣率,
掃描選項(xiàng),
掃描標(biāo)志,
數(shù)據(jù))
睡眠(0.5)
系統(tǒng)('清除')
嘗試:
為真:
嘗試:
狀態(tài), transfer_status = ctr_device.get_scan_status()
reset_cursor()
print('請輸入 CTRL + C 終止進(jìn)程\n')
打印('活動數(shù)據(jù)采集設(shè)備:',descriptor.dev_string,'(',
descriptor.unique_id, ')\n', sep='')
打印('實(shí)際掃描速率 = ','{:.6f}'.format(sample_rate),
'赫茲\n')
索引 = transfer_status.current_index
print('currentScanCount = ',
transfer_status.current_scan_count)
print('currentTotalCount = ',
transfer_status.current_total_count)
print('currentIndex = ', index, '\n')
print('chan =', (0), ': ', '{:d}'.format(data[index]))
print('chan =', (1), ': ', '{:d}'.format(data[index+1]))
睡眠(0.1)
如果狀態(tài) != ScanStatus.RUNNING:
休息
除了(值錯(cuò)誤,名稱錯(cuò)誤,語法錯(cuò)誤):
休息
除了鍵盤中斷:
經(jīng)過
tmr_device.pulse_out_stop(timer_number)
除了 RuntimeError 作為錯(cuò)誤:
打印('\n',錯(cuò)誤)
最后:
如果 daq_device:
如果狀態(tài) == ScanStatus.RUNNING:
ctr_device.scan_stop()
如果 daq_device.is_connected():
daq_device.disconnect()
daq_device.release()
def reset_cursor():
"""重置終端窗口中的光標(biāo)。"""
stdout.write('\033[1;1H')
如果 __name__ == '__main__':
主要的()