Python - ctypes - 访问缓冲区 return 作为 dll 库中的指针 - 数据翻译 SDK
Python - ctypes - accessing buffer return as pointer in dll library - datatranslation SDK
我正在使用 Datatranlation 的 DataAcq SDK 开发一个项目。
我正在尝试访问缓冲区并设置它的值。
在 C:
中如何使用它的示例
/* allocate the output buffer, sets handler for buffer */
CHECKERROR (olDmCallocBuffer(0,0,(ULNG) size,2,&hbuf));
/* Get pointer to buffer */
CHECKERROR (olDmGetBufferPtr(board.hbuf,(LPVOID*)&lpbuf));
/* fill the output buffer*/
lpbuf[i] = (UINT) some value
/* Put the buffer to the DAC */
CHECKERROR (olDaPutBuffer(board.hdass, hbuf));
我正在尝试使用 ctypes 在 Python 中做同样的事情:
_buffer = ctypes.c_void_p()
# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
ctypes.c_uint(0x0000),
ctypes.c_uint(2),
ctypes.c_uint(4),
ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))
但是我如何访问和设置上述 C 示例中的元素?
我可以在 Python 中进行引用我可以修改但是如何将其解析回 olDaPutBuffer 函数?
编辑:
通过 ctypes
中的 memmove
解决了它:
_buffer = ctypes.c_void_p()
# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
ctypes.c_uint(0x0000),
ctypes.c_uint(2),
ctypes.c_uint(4),
ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))
c_type_buf = ctypes.c_buffer(some values)
ctypes.memmove(buf_ptr, ctypes.byref(c_type_buf), len(c_type_buf))
通过 ctypes
中的 memmove
解决了它:
_buffer = ctypes.c_void_p()
# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
ctypes.c_uint(0x0000),
ctypes.c_uint(2),
ctypes.c_uint(4),
ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))
# Create new buffer
c_type_buf = ctypes.c_buffer(some values)
# Copy new buffer to address of the original buffer
ctypes.memmove(buf_ptr, ctypes.byref(c_type_buf), len(c_type_buf))
我正在使用 Datatranlation 的 DataAcq SDK 开发一个项目。
我正在尝试访问缓冲区并设置它的值。
在 C:
中如何使用它的示例/* allocate the output buffer, sets handler for buffer */
CHECKERROR (olDmCallocBuffer(0,0,(ULNG) size,2,&hbuf));
/* Get pointer to buffer */
CHECKERROR (olDmGetBufferPtr(board.hbuf,(LPVOID*)&lpbuf));
/* fill the output buffer*/
lpbuf[i] = (UINT) some value
/* Put the buffer to the DAC */
CHECKERROR (olDaPutBuffer(board.hdass, hbuf));
我正在尝试使用 ctypes 在 Python 中做同样的事情:
_buffer = ctypes.c_void_p()
# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
ctypes.c_uint(0x0000),
ctypes.c_uint(2),
ctypes.c_uint(4),
ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))
但是我如何访问和设置上述 C 示例中的元素? 我可以在 Python 中进行引用我可以修改但是如何将其解析回 olDaPutBuffer 函数?
编辑:
通过 ctypes
中的 memmove
解决了它:
_buffer = ctypes.c_void_p()
# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
ctypes.c_uint(0x0000),
ctypes.c_uint(2),
ctypes.c_uint(4),
ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))
c_type_buf = ctypes.c_buffer(some values)
ctypes.memmove(buf_ptr, ctypes.byref(c_type_buf), len(c_type_buf))
通过 ctypes
中的 memmove
解决了它:
_buffer = ctypes.c_void_p()
# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
ctypes.c_uint(0x0000),
ctypes.c_uint(2),
ctypes.c_uint(4),
ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))
# Create new buffer
c_type_buf = ctypes.c_buffer(some values)
# Copy new buffer to address of the original buffer
ctypes.memmove(buf_ptr, ctypes.byref(c_type_buf), len(c_type_buf))