ul.daq_in_scan()函數(shù)的標(biāo)準(zhǔn)控制臺(tái)示例使用USB-1808,它支持一個(gè)稱為ScaleData的掃描選項(xiàng)。使用此選項(xiàng)可以輕松地使用字符串格式轉(zhuǎn)換數(shù)據(jù)。但是,USB-1616HS,PCI-2500和USB-2500系列沒(méi)有ScaleData選項(xiàng),因此必須使用無(wú)符號(hào)的16位字。代替將分配的緩沖區(qū)強(qiáng)制轉(zhuǎn)換為雙精度指針,此示例將其強(qiáng)制轉(zhuǎn)換為無(wú)符號(hào)的short。
#1616HS和2500系列不支持ScaleData
#ctypes_array = cast(內(nèi)存句柄,POINTER(c_double))
ctypes_array = cast(內(nèi)存句柄,POINTER(c_ushort))
無(wú)符號(hào)短路對(duì)于數(shù)字端口和計(jì)數(shù)器寄存器是很自然的,但對(duì)于電壓卻不是那么多。為了顯示電壓,使用ul.to_eng_units函數(shù)將16位字縮放為電壓浮點(diǎn)數(shù)。
下面是程序列表-頁(yè)面底部還附有下載文件。
“”
檔案:daq_in_scan_usb_1800.py
展示的庫(kù)調(diào)用:mcculw.ul.daq_in_scan()
目的:同步掃描模擬通道,
數(shù)字端口和計(jì)數(shù)器在前臺(tái)。
演示:在兩個(gè)模擬通道上收集數(shù)據(jù),一個(gè)
數(shù)字通道和計(jì)數(shù)器通道。
其他庫(kù)調(diào)用:mcculw.ul.scaled_win_buf_alloc()
mcculw.ul.win_buf_free()
mcculw.ul.release_daq_device()
特殊要求:此示例為USB-1808系列上的過(guò)濾器。
“”
來(lái)自__future__ import absolute_import,division,print_function
從內(nèi)置導(dǎo)入*#@UnusedWildImport
#USB-1616HS和PCI / USB-2500系列不支持ScaleData
#使用c_uShort讀取16位字
#從ctypes import cast,POINTER,c_double
從ctypes import cast,POINTER,c_ushort
從mcculw import ul
從mcculw.enums導(dǎo)入ScanOptions,ChannelType,ULRange,DigitalPortType
從mcculw.device_info導(dǎo)入DaqDeviceInfo
從mcculw.enums導(dǎo)入CounterMode
從mcculw.enums導(dǎo)入CounterDebounceMode
從mcculw.enums導(dǎo)入CounterDebounceTime
從mcculw.enums導(dǎo)入CounterEdgeDetection
從mcculw.enums導(dǎo)入CounterTickSize
嘗試:
從console_examples_util導(dǎo)入config_first_detected_device
除了ImportError:
從.console_examples_util導(dǎo)入config_first_detected_device
def run_example():
#默認(rèn)情況下,該示例檢測(cè)并顯示所有可用設(shè)備和
#選擇列出的第一個(gè)設(shè)備。使用dev_id_list變量進(jìn)行過(guò)濾
#通過(guò)設(shè)備ID檢測(cè)到的設(shè)備(有關(guān)設(shè)備ID,請(qǐng)參閱UL文檔)。
#如果use_device_detection設(shè)置為False,則board_num變量需要
#與使用Instacal配置的所需板號(hào)匹配。
#無(wú)法找到PCI板,因此本示例使用InstaCal板號(hào)。
use_device_detection =假
board_num = 0
#USB-1808系列支持的PID
率= 100
points_per_channel = 100
memhandle =無(wú)
嘗試:
#設(shè)備檢測(cè)對(duì)PCI板卡有效,因此將其禁用
如果use_device_detection:
config_first_detected_device(board_num,dev_id_list)
#使用InstaCal的零板
daq_dev_info = DaqDeviceInfo(board_num)
print('\ n活動(dòng)DAQ設(shè)備:',daq_dev_info.product_name,'(',
daq_dev_info.unique_id,')\ n',sep ='')
scan_options = ScanOptions.FOREGROUND
#創(chuàng)建daq_in_scan通道配置列表
chan_list = []
chan_type_list = []
gain_list = []
#模擬通道必須在列表中的第一位
chan_list.append(0)
chan_type_list.append(ChannelType.ANALOG)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(1)
chan_type_list.append(ChannelType.ANALOG)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(DigitalPortType.FIRSTPORTA)
chan_type_list.append(ChannelType.DIGITAL8)
gain_list.append(ULRange.NOTUSED)
chan_list.append(0)
chan_type_list.append(ChannelType.CTR32LOW)
gain_list.append(ULRange.NOTUSED)
chan_list.append(0)
chan_type_list.append(ChannelType.CTR32HIGH)
gain_list.append(ULRange.NOTUSED)
num_chans = len(chan_list)
total_count = num_chans * points_per_channel
#為掃描分配內(nèi)存,并將其轉(zhuǎn)換為ctypes數(shù)組指針
內(nèi)存句柄= ul.scaled_win_buf_alloc(總數(shù))
#1616HS和2500系列不支持ScaleData
#ctypes_array = cast(內(nèi)存句柄,POINTER(c_double))
ctypes_array = cast(內(nèi)存句柄,POINTER(c_ushort))
#注意:在win_buf_free為之后,ctypes數(shù)組將不再有效
#叫。
#可以使用win_buf_to_array或
#win_buf_to_array_32在釋放內(nèi)存之前。副本可以使用
#隨時(shí)。
#檢查緩沖區(qū)是否成功分配
如果不是成員:
引發(fā)Exception('錯(cuò)誤:無(wú)法分配內(nèi)存')
ul.c_config_scan(board_num,
0,
CounterMode.TOTALIZE | CounterMode.COUNTER | CounterMode.BIT_32,
CounterDebounceTime.DEBOUNCE500ns,
CounterDebounceMode.TRIGGER_AFTER_STABLE,
CounterEdgeDetection.RISING_EDGE,
CounterTickSize.TICK20PT83ns,
0)
#開(kāi)始掃描
actRate,preCount,postCount = ul.daq_in_scan(
board_num,chan_list,chan_type_list,gain_list,num_chans,
速率,0,total_count,內(nèi)存句柄,scan_options)
print('掃描成功完成。速率= {:d},掃描= {:d}'。format(actRate,postCount))
#創(chuàng)建一個(gè)格式字符串,以對(duì)齊列中的數(shù)據(jù)
row_format ='{:> 5}'+'{:> 10}'* num_chans
#打印頻道名稱標(biāo)題
標(biāo)簽= ['索引']
對(duì)于范圍(num_chans)中的ch_index:
channel_label = {
ChannelType.ANALOG:lambda:
'Ain'+ str(chan_list [ch_index]),
ChannelType.DIGITAL8:lambda:
''+ chan_list [ch_index] .name,
ChannelType.CTR32LOW:lambda:
'CinLow'+ str(chan_list [ch_index]),
ChannelType.CTR32HIGH:lambda:
'CinHigh'+ str(chan_list [ch_index]),
} [chan_type_list [ch_index]]()
labels.append(channel_label)
打印(row_format.format(* labels))
用于范圍內(nèi)的索引(points_per_channel):
display_data = [索引]
對(duì)于范圍(num_chans)中的ch_index:
data_label = {
ChannelType.ANALOG:lambda:
'{:.3f}'。format(ul.to_eng_units(board_num,ULRange.BIP10VOLTS,ctypes_array [ch_index])),
ChannelType.DIGITAL8:lambda:
'{:d}'。format(ctypes_array [ch_index]),
ChannelType.CTR32LOW:lambda:
'{:d}'。format(ctypes_array [ch_index]),
ChannelType.CTR32HIGH:lambda:
'{:d}'。format(ctypes_array [ch_index]),
} [chan_type_list [ch_index]]()
display_data.append(data_label)
#打印此行
打印(行格式。格式(*顯示數(shù)據(jù)))
例外,例如e:
打印('\ n',e)
最后:
如果是記憶體:
#在finally塊中釋放緩沖區(qū),以防止內(nèi)存泄漏。
ul.win_buf_free(內(nèi)存句柄)
如果use_device_detection:
ul.release_daq_device(board_num)
如果__name__ =='__main__':
run_example()