共通モジュールの作成
This content is not available in your language yet.
Python の ctypes モジュールを用いて puclib.dll を呼び出す際、DLL ロードや C 構造体・型定義などの共通処理を puclib.py としてモジュール化します。以降のすべての Python サンプルプログラムは、本ページで作成する puclib.py をインポートして動作させる前提となります。
-
共通ラッパーモジュール (
puclib.py) の作成各スクリプトで使い回す定数・構造体・SDK初期化ロジックをまとめた
puclib.pyを作成し、作業フォルダーに配置します。puclib.py import ctypesfrom ctypes import c_uint32, c_uint64, c_ushort, c_ubyte, c_double, c_void_p, Structure, POINTERimport os# 1. 定数定義PUC_SUCCEEDED = 0PUC_DETECT_TARGET_HAWK = 0x00000002PUC_DETECT_TARGET_UC1 = 0x00000001PUC_COLOR_MONO = 0PUC_COLOR_COLOR = 1PUC_MAX_DEVICE = 16PUC_Q_COUNT = 64# 2. 構造体定義class PUC_DETECT_FILTER(Structure):_fields_ = [("nDeviceTypeFlags", c_uint32),("nAdapterIPAddress", c_uint32),("nTargetIPAddress", c_uint32),("nReserved", c_uint32 * 5),]class PUC_DETECT_INFO(Structure):_fields_ = [("nDeviceCount", c_uint32),("nDeviceNoList", c_uint32 * PUC_MAX_DEVICE),("nDeviceType", c_uint32 * PUC_MAX_DEVICE),("nDeviceIPAddress", c_uint32 * PUC_MAX_DEVICE),]class PUC_XFER_DATA_INFO(Structure):_fields_ = [("pData", POINTER(c_ubyte)),("nDataSize", c_uint32),("nSequenceNo", ctypes.c_uint16),("isDamaged", c_ubyte),]# 3. DLL ロードおよび API 型設定関数def load_sdk(dll_path=r"C:\infinicam_sdk\bin\x64\puclib.dll"):if not os.path.exists(dll_path):raise FileNotFoundError(f"エラー: 指定されたパスに DLL が見つかりません: {dll_path}")lib = ctypes.WinDLL(dll_path)# 関数の引数型・戻り値型を一括設定lib.PUC_Initialize.restype = c_uint32lib.PUC_DetectDevice2.argtypes = [POINTER(PUC_DETECT_FILTER), POINTER(PUC_DETECT_INFO)]lib.PUC_DetectDevice2.restype = c_uint32lib.PUC_OpenDevice2.argtypes = [c_uint32, POINTER(c_void_p)]lib.PUC_OpenDevice2.restype = c_uint32lib.PUC_GetColorType.argtypes = [c_void_p, POINTER(c_uint32)]lib.PUC_GetColorType.restype = c_uint32lib.PUC_SetWhiteBalanceGain.argtypes = [c_double, c_double, c_double]lib.PUC_SetWhiteBalanceGain.restype = c_uint32lib.PUC_GetResolution.argtypes = [c_void_p, POINTER(c_uint32), POINTER(c_uint32)]lib.PUC_GetResolution.restype = c_uint32lib.PUC_SetResolution.argtypes = [c_void_p, c_uint32, c_uint32]lib.PUC_SetResolution.restype = c_uint32lib.PUC_SetFramerateShutter.argtypes = [c_void_p, c_uint32, c_uint32]lib.PUC_SetFramerateShutter.restype = c_uint32lib.PUC_SetSyncInMode.argtypes = [c_void_p, c_uint32, c_uint32]lib.PUC_SetSyncInMode.restype = c_uint32lib.PUC_GetQuantization2.argtypes = [c_void_p, POINTER(c_ushort)]lib.PUC_GetQuantization2.restype = c_uint32lib.PUC_GetXferDataSize.argtypes = [c_void_p, POINTER(c_uint32)]lib.PUC_GetXferDataSize.restype = c_uint32lib.PUC_GetSingleXferData.argtypes = [c_void_p, POINTER(PUC_XFER_DATA_INFO)]lib.PUC_GetSingleXferData.restype = c_uint32lib.PUC_DecodeData.argtypes = [POINTER(c_ubyte), c_uint32, c_uint32, c_uint32, c_uint32, c_uint32, POINTER(c_ubyte), POINTER(c_ushort)]lib.PUC_DecodeData.restype = c_uint32lib.PUC_DecodeDCData.argtypes = [POINTER(c_ubyte), c_uint32, c_uint32, c_uint32, c_uint32, POINTER(c_ubyte)]lib.PUC_DecodeDCData.restype = c_uint32lib.PUC_GetSerialNo.argtypes = [c_void_p, POINTER(c_uint64)]lib.PUC_GetSerialNo.restype = c_uint32lib.PUC_GetDeviceVersion.argtypes = [c_void_p, POINTER(c_uint32)]lib.PUC_GetDeviceVersion.restype = c_uint32lib.PUC_CloseDevice.argtypes = [c_void_p]lib.PUC_CloseDevice.restype = c_uint32return lib -
HelloWorld スクリプト (
hello_world.py) の作成同じ作業フォルダーに
hello_world.pyを作成し、puclib.pyを呼び出してデバイスの接続テストを行う以下のコードを記述します。hello_world.py from ctypes import c_uint32, c_uint64, c_void_p, byreffrom puclib import (load_sdk, PUC_SUCCEEDED, PUC_DETECT_TARGET_HAWK,PUC_DETECT_FILTER, PUC_DETECT_INFO)def main():print("INFINICAM GbE-2 初期化中 (Python)...")# 1. SDK のロードと初期化puclib = load_sdk()ret = puclib.PUC_Initialize()if ret != PUC_SUCCEEDED:print(f"エラー: SDKの初期化に失敗しました。ErrorCode: {ret}")return# 2. ネットワーク上の GbE-2 デバイスを検索detect_filter = PUC_DETECT_FILTER(nDeviceTypeFlags=PUC_DETECT_TARGET_HAWK)detect_info = PUC_DETECT_INFO()ret = puclib.PUC_DetectDevice2(byref(detect_filter), byref(detect_info))if ret != PUC_SUCCEEDED or detect_info.nDeviceCount == 0:print(f"エラー: GbE-2 カメラが見つかりませんでした。ErrorCode: {ret}")returnprint(f"GbE-2 カメラを検出しました。(検出台数: {detect_info.nDeviceCount})")# 3. 検出した最初のデバイスをオープンh_device = c_void_p()ret = puclib.PUC_OpenDevice2(0, byref(h_device))if ret != PUC_SUCCEEDED:print(f"エラー: デバイスのオープンに失敗しました。ErrorCode: {ret}")returnprint("SUCCESS: GbE-2 が正常にオープンされました!")# 4. カメラ情報の取得serial_no = c_uint64()if puclib.PUC_GetSerialNo(h_device, byref(serial_no)) == PUC_SUCCEEDED:print(f"シリアルナンバー: {serial_no.value}")version = c_uint32()if puclib.PUC_GetDeviceVersion(h_device, byref(version)) == PUC_SUCCEEDED:print(f"デバイスバージョン: {version.value}")# 5. デバイスのクローズpuclib.PUC_CloseDevice(h_device)if __name__ == "__main__":main() -
スクリプトの実行
ターミナル(またはコマンドプロンプト)で実行します。
Terminal window python hello_world.pyコンソールにシリアルナンバーとデバイスバージョンが表示されれば正常終了です。
共通モジュール化のポイント
Section titled “共通モジュール化のポイント”1. 冗長な型定義の集約
Section titled “1. 冗長な型定義の集約”puclib.py に PUC_DETECT_FILTER などの構造体クラスや load_sdk() を定義しておくことで、各機能スクリプトで毎回数十行の argtypes / restype を記述する必要がなくなります。
2. 今後のサンプルコードでの利用
Section titled “2. 今後のサンプルコードでの利用”これ以降の Python サンプル(single_image.mdx や各種ライブラリ連携など)では、すべて先頭で from puclib import ... を実行して SDK を操作します。
