在Python ctypes类(Structure)中按字节访问
这是一个跟我之前提问相关的后续问题,之前的问题没有得到回答。
编辑和整理过的内容
来自我的模块:
class t_data_block(Structure):
_fields_ = [("current_readings",c_ulong * PORTS_NUM),
("power_reading",c_ulong)]
class t_sys_flags(Structure):
"System flags"
_fields_ = [("Alarm1",c_ulong,1),
("Alarm2",c_ulong,1),
("reserved",c_ulong,30)]
class t_input_buff(Structure):
"Input buffer from the board"
_fields_ = [("header",c_ulong),
("sys_flags", t_sys_flags),# t_sys_flags is another structure
("data_block", t_data_block * CHIP_NUM)] # t_data_block is another structure
我需要逐个处理 buff 中的每一个字节,我尝试了以下方法:
from pc_memory import*
def calc_formula(buff,len):
sum = 0
for curChar in buff:
numericByteValue = ord(curChar)
sum += numericByteValue
return sum
def main:
input_buff = t_input_buff()
calc_formula(input_buff,len)
但是在执行 for 命令时,我收到了“错误:TypeError: 't_input_buff' 对象不可迭代”的提示。
我也尝试过使用 str(buff)
,但没有成功。
有什么建议吗?
3 个回答
0
我不知道你这个“buff”变量的具体类型是什么,但你可以试着在循环的开头把它明确转换成字符串。
for curChar in str(buff):
4
看看这个buffer的介绍:
from binascii import hexlify
x = t_input_buff()
x.header = 1
x.sys_flags.Alarm1 = 1
x.sys_flags.Alarm2 = 0
x.sys_flags.reserved = 0x15555555
x.data_block[0].current_readings[0] = 2
x.data_block[0].current_readings[1] = 3
x.data_block[0].power_reading = 4
x.data_block[1].current_readings[0] = 5
x.data_block[1].current_readings[1] = 6
x.data_block[1].power_reading = 7
b = buffer(x)
print hexlify(b[:])
输出结果:
0100000055555555020000000300000004000000050000000600000007000000
你也可以使用ctypes来进行类型转换:
p=cast(byref(x),POINTER(c_byte*sizeof(x)))
print p.contents[:]
输出结果:
[1, 0, 0, 0, 85, 85, 85, 85, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0]
注意:在Python 3中,buffer
这个东西不再存在了。你可以用bytes
,或者用list(bytes(...))
来达到和buffer
以及ctypes转换相同的效果。
0
我找到了这个问题的解决办法:(我把原来的结构简化了一下,方便理解)
from ctypes import*
class t_mem_fields(Structure):
_fields_ = [("header",c_ulong),
("log", c_byte * 10)]
class t_mem(Union):
_fields_ = [("st_field",t_mem_fields),
("byte_index",c_byte * 14)]
def calc(buff,len):
sum = 0
print(type(buff))
for cur_byte in buff.byte_index:
print "cur_byte = %x" %cur_byte
sum += cur_byte
print "sum = %x" %sum
return sum
def main():
mem1 = t_mem()
mem1.st_field.header = 0x12345678
mem1.byte_index[4] = 1
mem1.byte_index[5] = 2
mem1.byte_index[6] = 3
mem1.byte_index[7] = 4
calc(mem1,14)
main()