使ctypes结构可迭代

时间:2018-09-27 13:56:52

标签: python ctypes

由于我仍然是菜鸟,我只需要对python进行一些指导,我将以下C源代码片段转换为python。

C代码段:

Typedef struct packet_s {
union {
  uint16_t fcf;
  struct {
    uint16_t type:3;
    uint16_t security:1;
    uint16_t framePending:1;
    uint16_t ack:1;
    uint16_t ipan:1;
    uint16_t reserved:3;
    uint16_t destAddrMode:2;
    uint16_t version:2;
    uint16_t srcAddrMode:2;
  } fcf_s;
};

uint8_t seq;
uint16_t pan;
locoAddress_t destAddress;
locoAddress_t sourceAddress;

uint8_t payload[128];
} __attribute__((packed)) packet_t;

python:

class fcf_s(Structure):
    _fields_ = [
        ("type", c_uint, 3),
        ("security", c_uint, 1),
        ("framePending", c_uint, 1),
        ("ack", c_uint, 1),
        ("ipan", c_uint, 1),
        ("reserved", c_uint, 3),
        ("destAddrMode", c_uint, 2),
        ("version", c_uint, 2),
        ("srcAddrMode", c_uint, 2)
        ]

 class fcf_union(Union):
    _fields_ = [
       ("fcf", c_uint),
       ("fcf_s", fcf_s)
       ]

 class packet_s(Structure):
    _fields_ = [
       ("fcf_union", fcf_union),
       ("seq", c_uint),
       ("pan", c_uint),
       ("destAddress", c_uint64 * 8),
       ("sourceAddress", c_uint64 * 8),
       ("payload", c_uint * 128)
       ]

edit 1:用于转换为字节的函数

def writeValueToBytes(data, val, n):
"""
This function writes the value specified and convert it into bytes to 
 write in the array

Args:
        data: The array you want to write the value into.
        val: The value you want to write in the array.
        n: The size of the array.

Return:
        The modified array of bytes.
"""
for i in range(0, n):
    data[i] = int(((val >> (i * 8)) & C.MASK_LS_BYTE))
return data

一旦packet_s结构已经填充了正确的数据,我需要将其传递给函数,并将其拆分为字节,然后通过spi写入DWM1000 UWB无线电。

我得到的类型错误是“ TypeError:packet_s不可迭代”,我该如何解决?

谢谢。

1 个答案:

答案 0 :(得分:0)

这是正确的结构:

from ctypes import *

class fcf_s(Structure):
    _fields_ = (('type',c_uint16,3),
                ('security',c_uint16,1),
                ('framePending',c_uint16,1),
                ('ack',c_uint16,1),
                ('ipan',c_uint16,1),
                ('reserved',c_uint16,3),
                ('destAddrMode',c_uint16,2),
                ('version',c_uint16,2),
                ('srcAddrMode',c_uint16,2))

class _U(Union):
    _fields_ = (('fcf',c_uint16),
                ('fcf_s',fcf_s))

class packet_s(Structure):
    _pack_ = 1
    _anonymous_ = ('u',)
    _fields_ = (('u',_U),
                ('seq',c_uint8),
                ('pan',c_uint16),
                ('destAddress',c_uint64),
                ('sourceAddress',c_uint64),
                ('payload',c_uint8 * 128))

要获取要传输的字节,请使用bytes()

p = packet_s()
p.fcf = 0x1234
p.seq = 0xab
p.pan = 0x5678
p.destAddress = 0x0123456789abcdef
p.sourceAddress = 0xfedcba9876543210
p.payload = (c_uint8 * 128)(*range(128))

b = bytes(p)
print(b)

输出:

b'4\x12\xabxV\xef\xcd\xab\x89gE#\x01\x102Tv\x98\xba\xdc\xfe\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f'