嗨,我只是需要一些关于 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)
]
编辑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 is not iterable”,我该如何解决?
提前致谢。