下面的代碼清單使用 daq_in_scan 函數來讀取熱電偶輸入(本文底部附有模塊)。此功能通常用于同時讀取USB-1616HS和USB-2523等設備上的設備子系統(模擬、數字). 但是,如果您需要以更快的速度掃描多個熱電偶,則還必須使用它來將通道與其 CJC 傳感器配對。在此示例中,我們使用它以每秒 100 個樣本讀取 8 個熱電偶輸入(以及相關的 CJC 通道)。緩沖區中返回的二進制數據包含 CJC 和通道數據。它被安排為 CJC、Channel0、CJC Channel1 等。必須注意在每個通道之前放置正確的 CJC。GetTCValues 函數用于將 CJC 和通道數據轉換為溫度值。使用該功能的關鍵是輸入第一個熱電偶/CJC 的位置和要轉換的樣本數。
下面附上 32 位代碼文件。要運行該示例,您必須安裝適用于 Windows 的 InstaCal 和 mcculw Python。由于選項卡縮進問題,不建議使用附加模塊復制/粘貼 Python 代碼。
免責聲明:
隨附的代碼或示例按原樣提供。它尚未作為產品進行測試或驗證,用于已部署的應用程序或系統,或用于危險環境。您承擔使用代碼或示例的所有風險。
文件:daq_in_scan_usb_1800.py
庫調用演示:mcculw.ul.daq_in_scan()
用途:同步掃描8個熱電偶通道
演示:如何將原始數據轉換為溫度
其他庫調用:mcculw.ul.daq_in_scan()
mcculw.ul.get_tc_values()
mcculw.ul.win_buf_free()
mcculw.ul.release_daq_device()
特殊要求:此示例過濾 USB-1808 系列。
從 __future__ 導入 absolute_import,除法,print_function
from builtins import * # @UnusedWildImport
從時間導入睡眠
# 1616HS 和 2500 系列不支持 ScaleData
# from ctypes import cast, POINTER, c_double
從 ctypes 導入 cast、POINTER、c_ushort、c_float
從 mcculw 進口 ul
從 mcculw.enums 導入 ScanOptions、ChannelType、ULRange、TempScale、DigitalPortType
從 mcculw.device_info 導入 DaqDeviceInfo
從 mcculw.ul 導入 ULError
從 mcculw.enums 導入 FunctionType,狀態
嘗試:
從 console_examples_util 導入 config_first_detected_device
導入錯誤除外:
從 .console_examples_util 導入 config_first_detected_device
def run_example():
use_device_detection = False
board_num = 0
# USB-1808 系列支持的 PID
率 = 100
points_per_channel = 64
memhandle = 無
嘗試:
# 使用 InstaCal 的零板
daq_dev_info = DaqDeviceInfo(board_num)
print('\n活動數據采集設備:', daq_dev_info.product_name, ' (',
daq_dev_info.unique_id, ')\n', sep='')
scan_options = ScanOptions.FOREGROUND
# 創建 daq_in_scan 通道配置列表
chan_list = []
chan_type_list = []
增益列表 = []
# 模擬頻道必須在列表的第一位
chan_list.append(0)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(0)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(1)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(1)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(1)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(2)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(2)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(3)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(3)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(4)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(4)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(5)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(4)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(6)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(5)
chan_type_list.append(ChannelType.CJC)
gain_list.append(ULRange.BIP10VOLTS)
chan_list.append(7)
chan_type_list.append(ChannelType.TC)
gain_list.append(ULRange.BIP10VOLTS)
num_chans = len(chan_list)
total_count = num_chans * points_per_channel
half_count = total_count / 2
# 為掃描分配內存并將其轉換為 ctypes 數組指針
memhandle = ul.scaled_win_buf_alloc(total_count)
# 1616HS 和 2500 系列不支持 ScaleData
# ctypes_array = cast(memhandle, POINTER(c_double))
ctypes_array = cast(memhandle, POINTER(c_ushort))
# 檢查緩沖區是否分配成功
如果不是內存句柄:
raise Exception('錯誤:分配內存失敗')
scan_options = ScanOptions.CONTINUOUS | 掃描選項.背景
# 開始掃描
actRate, preCount, postCount = ul.daq_in_scan(
board_num, chan_list, chan_type_list, gain_list, num_chans,
rate, 0, total_count, memhandle, scan_options)
狀態,curr_count,curr_index = ul.get_status(
board_num, FunctionType.DAQIFUNCTION)
# 用于切換讀取上下緩沖區的布爾標志
read_lower = 真
# 獲取 640 個樣本
獲取 = points_per_channel * num_chans * 10
而狀態!=狀態。空閑:
狀態,curr_count,curr_index = ul.get_status(
board_num, FunctionType.DAQIFUNCTION)
如果(curr_index > half_count)和(read_lower == True):
# 如果您也有電壓或數字輸入,請使用
ul.win_buf_to_array(memhandle, ctypes_array, 0, int(half_count))
# 每個通道的點數是 64,所以有 32 個要轉換,從零開始
e, temps = ul.get_tc_values(
board_num, chan_list, chan_type_list,
num_chans, memhandle, 0, int(points_per_channel/2), TempScale.CELSIUS)
對于范圍內的 j (256):
如果 j % 8 == 0:
打?。ǎ?/p>
print('{:2.2f}\t'.format(temps[j]), end ='')
read_lower = 假
elif (curr_index < half_count) 和 (read_lower == False):
# 如果您也有電壓或數字輸入,請使用
ul.win_buf_to_array(memhandle, ctypes_array, int(half_count), int(half_count))
# 每個通道的點數是 64,所以有 32 個要轉換,從中間點開始
e, temps = ul.get_tc_values(
board_num, chan_list, chan_type_list,
num_chans,memhandle,int(points_per_channel/2),int(points_per_channel/2),TempScale.CELSIUS)
對于范圍內的 j (256):
如果 j % 8 == 0:
打?。ǎ?/p>
print('{:2.2f}\t'.format(temps[j]), end='')
read_lower = 假
如果 curr_count > 獲?。?/p>
print('\n獲取的掃描次數 = {:d}'.format(int(curr_count / num_chans)))
休息
ul.stop_background(board_num, FunctionType.DAQIFUNCTION)
print("\n掃描成功完成。")
除了 ULError 為 e:
打印('\n', e)
最后:
如果內存處理:
# 在 finally 塊中釋放緩沖區以防止內存泄漏。
ul.win_buf_free(內存句柄)
如果使用_device_detection:
ul.release_daq_device(board_num)
如果 __name__ == '__main__':
運行示例()