Freepie有一个dll文件,可以用来将数据传递到程序中。以下是我的脚本:

import time
import ctypes
from ctypes import byref, c_int, POINTER


class freepie_io_6dof_data(ctypes.Structure):
    __fields__ = [
        ("Yaw", ctypes.c_float()),
        ("Pitch", ctypes.c_float()),
        ("Roll", ctypes.c_float()),
        ("X", ctypes.c_float()),
        ("Y", ctypes.c_float()),
        ("Z", ctypes.c_float())]

q = ctypes.CDLL("E:\\Program Files (x86)\\FreePIE\\freepie_io.dll")  # Load DLL
slots = q.freepie_io_6dof_slots()
data = freepie_io_6dof_data()
data.Y = ctypes.c_float(0)
q.freepie_io_6dof_write.argtypes = [c_int, c_int, POINTER(freepie_io_6dof_data)]
while True:
    q.freepie_io_6dof_write(0, 1, byref(data))
    print(data)
    time.sleep(0.5)

然而,当freepie获取数据时,每次运行程序时它都会以不同的数字显示,通常是6.34523234E-36。预期的输出是0,我哪里出错了?

最佳答案

主代码中的拼写错误导致了错误的性能。将__fields__更改为_fields_并从ctypes.c_float中移除括号后,代码工作正常!
以下是最终代码:

import time
import ctypes
from ctypes import byref, c_int, POINTER


class freepie_io_6dof_data(ctypes.Structure):
    _fields_ = [
        ("Yaw", ctypes.c_float),
        ("Pitch", ctypes.c_float),
        ("Roll", ctypes.c_float),
        ("X", ctypes.c_float),
        ("Y", ctypes.c_float),
        ("Z", ctypes.c_float)]

q = ctypes.CDLL("E:\\Program Files (x86)\\FreePIE\\freepie_io.dll")  # Load DLL
slots = q.freepie_io_6dof_slots()
data = freepie_io_6dof_data()
data.Y = ctypes.c_float(0)
q.freepie_io_6dof_write.argtypes = [c_int, c_int, POINTER(freepie_io_6dof_data)]
while True:
    q.freepie_io_6dof_write(0, 1, byref(data))
    print(data)
    time.sleep(0.5)

关于python - 将结构传递给ctypes中的dll(freepie),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53860804/

10-11 22:55