我正在尝试实现一个 python 接口(interface)。我对 c 结构的定义有疑问。如何在主结构中生成内部结构(LinV)?
手册中的定义:
typedef struct
{
float MaxExcitation;
struct LinVal
{
double MeasValue;
double RefValue;
} LinV[SEN_LIN_DATA_MAX];
} Data;
python 代码:
class LinV(Structure):
_fields_ = [('NeasValue', c_double),
('RefValue', c_double)]
class Data(Structure):
_fields_ = [('MaxExcitation', c_float),
('LinV', ???????)]
谢谢 马库斯
最佳答案
你只需乘以指定数组:
from ctypes import *
SEN_LIN_DATA_MAX = 3
class LinV(Structure):
_fields_ = [('NeasValue', c_double),
('RefValue', c_double)]
class Data(Structure):
_fields_ = [('MaxExcitation', c_float),
('LinV', LinV * SEN_LIN_DATA_MAX)]
d = Data(2.0,
(LinV(1.2, 3.2),
LinV(2.2, 2.2),
LinV(3.2, 1.2),
))
关于python:c-struct 和 wintypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490021/