【问题标题】:python: c-struct and wintypespython: c-struct 和 wintypes
【发布时间】:2017-02-27 15:51:05
【问题描述】:

我正在尝试实现一个 python 接口。我对 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', ???????)]

谢谢 马库斯

【问题讨论】:

    标签: python c struct


    【解决方案1】:

    您只需乘以指定数组:

    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),
    ))  
    

    【讨论】:

      猜你喜欢
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      相关资源
      最近更新 更多