如何生成Python ctypes实例

2024-09-27 00:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用pyueye库来运行ML摄像头,但遇到了ctypes的问题。有一个函数需要一个类型为“ctypes instance”的参数,尽管尝试了所有可能的变体,但我仍然不知道如何使用ctypes库生成这个参数。此库中没有python文档,但我尝试使用的函数的C文档是:

Syntax

INT is_SetAutoParameter (HIDS hCam, INT param, double* pval1, double* pval2)

Example 1

//Enable auto gain control:

double dEnable = 1;

int ret = is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_GAIN, &dEnable, 0);

我在python中收到的代码和后续错误是:

nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_SET_ENABLE_AUTO_GAIN, ctypes.byref(ctypes.c_long(1)), ctypes.byref(ctypes.c_long(0)))

Error:
ret = _is_SetAutoParameter(_hCam, _param, ctypes.byref(pval1), ctypes.byref(pval2))
TypeError: byref() argument must be a ctypes instance, not 'CArgObject'

关于ctypes实例有什么建议吗?谢谢

编辑:最小可复制示例

from pyueye import ueye
import ctypes

class Turfcam:
    def main(self):
        turfcam.take_photo()

    def take_photo(self):
        hCam = ueye.HIDS(0)
        pval1 = ctypes.c_double(1)
        pval2 = ctypes.c_double(0)
        nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_SET_ENABLE_AUTO_GAIN, ctypes.byref(pval1), ctypes.byref(pval2))

        # Camera Init
        nRet = ueye.is_InitCamera(hCam, None)

if __name__ == "__main__":
    turfcam = Turfcam()
    turfcam.main()

Tags: autoisenablehcamctypesgaindoubleset
1条回答
网友
1楼 · 发布于 2024-09-27 00:17:28

pyueye库有一些最基本的文档:

_is_SetAutoParameter = _bind("is_SetAutoParameter", [ctypes.c_uint, ctypes.c_int, ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double)], ctypes.c_int)


def is_SetAutoParameter(hCam, param, pval1, pval2):
    """
    :param hCam: c_uint (aka c-type: HIDS)
    :param param: c_int (aka c-type: INT)
    :param pval1: c_double (aka c-type: double \*)
    :param pval2: c_double (aka c-type: double \*)
    :returns: success, or no success, that is the answer
    :raises NotImplementedError: if function could not be loaded
    """
    if _is_SetAutoParameter is None:
        raise NotImplementedError()

    _hCam = _value_cast(hCam, ctypes.c_uint)
    _param = _value_cast(param, ctypes.c_int)

    ret = _is_SetAutoParameter(_hCam, _param, ctypes.byref(pval1), ctypes.byref(pval2))

    return ret

包装器正在执行byref,因此只需要传递ctypes对象。uEye docs表示参数可以是输入或输出参数,因此始终创建c_double对象,并根据传递的函数的需要初始化它们。请注意,对于输出参数,必须为对象指定一个名称,以便它在要查询的调用之后存在。输入参数可以直接将ctypes.c_double(value)传递给函数,但为了一致性,我总是为对象指定名称:

例如:

pval1 = ctypes.c_double()
pval2 = ctypes.c_double() # not used for output on IS_GET_ENABLE_AUTO_GAIN 
nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_GET_ENABLE_AUTO_GAIN, pval1, pval2)
print(pval1.value) # query the returned value

相关问题 更多 >

    热门问题