Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Python和ctype访问C全局变量结构_Python_C_Ctypes_Simulink - Fatal编程技术网

如何通过Python和ctype访问C全局变量结构

如何通过Python和ctype访问C全局变量结构,python,c,ctypes,simulink,Python,C,Ctypes,Simulink,我必须将python与外部so库集成。不幸的是,C代码使用了一个全局变量SimpleTest_Y(结构),我需要访问它才能修改值 这里是C代码 SimpleTest.c文件 SimpleTest.h文件 initialize方法的python调用是有效的,但是当我尝试访问SimpleTest_Y.Out1时,我得到以下错误: print SimpleTest_Y.Out1 AttributeError:“\u FuncPtr”对象没有属性“Out1” 我想我无法访问外部C库上定义的全局变量 注

我必须将python与外部so库集成。不幸的是,C代码使用了一个全局变量
SimpleTest_Y
(结构),我需要访问它才能修改值

这里是C代码

SimpleTest.c文件 SimpleTest.h文件 initialize方法的python调用是有效的,但是当我尝试访问
SimpleTest_Y.Out1
时,我得到以下错误:

print SimpleTest_Y.Out1
AttributeError:“\u FuncPtr”对象没有属性“Out1”

我想我无法访问外部C库上定义的全局变量

注意:这是一个结构,不是一个正常的var

您需要使用该方法来访问全局变量

这项工作:

import ctypes

class Inp(ctypes.Structure):
   _fields_ = [('In1', ctypes.c_float),
               ('In2', ctypes.c_float)]

class Out(ctypes.Structure):
   _fields_ = [('Out1', ctypes.c_float)]

myLib = ctypes.CDLL('./SimpleTest.so')

SimpleTest_initialize = myLib.SimpleTest_initialize
SimpleTest_initialize()

SimpleTest_Y = Out.in_dll(myLib, 'SimpleTest_Y')

print SimpleTest_Y.Out1
您需要使用该方法来访问全局变量

这项工作:

import ctypes

class Inp(ctypes.Structure):
   _fields_ = [('In1', ctypes.c_float),
               ('In2', ctypes.c_float)]

class Out(ctypes.Structure):
   _fields_ = [('Out1', ctypes.c_float)]

myLib = ctypes.CDLL('./SimpleTest.so')

SimpleTest_initialize = myLib.SimpleTest_initialize
SimpleTest_initialize()

SimpleTest_Y = Out.in_dll(myLib, 'SimpleTest_Y')

print SimpleTest_Y.Out1

您给出的头文件示例不完整,无法编译。更新后,它是由Simulink生成的。SimpleTest.h中的字段都是
double
,但您的ctypes代码使用
c\u float
而不是
c\u double
。您给出的头文件示例不完整,无法编译。更新后,它是由Simulink生成的。SimpleTest.h中的所有字段都是
double
,但是您的ctypes代码使用
c\u float
而不是
c\u double
print SimpleTest_Y.Out1
import ctypes

class Inp(ctypes.Structure):
   _fields_ = [('In1', ctypes.c_float),
               ('In2', ctypes.c_float)]

class Out(ctypes.Structure):
   _fields_ = [('Out1', ctypes.c_float)]

myLib = ctypes.CDLL('./SimpleTest.so')

SimpleTest_initialize = myLib.SimpleTest_initialize
SimpleTest_initialize()

SimpleTest_Y = Out.in_dll(myLib, 'SimpleTest_Y')

print SimpleTest_Y.Out1