在python ctypes类中逐字节访问(结构)

时间:2011-12-04 08:10:52

标签: python ctypes

这是my question的后续问题,未得到答复 编辑并组织

来自我的模块:

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 :(得分: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 cast:

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不再存在。 byteslist(bytes(...))相当于buffer和ctypes投射示例。

答案 1 :(得分:0)

我不知道你的“buff”变量的实际类型是什么,但你可以尝试在循环头中将它显式转换为字符串

for curChar in str(buff):

答案 2 :(得分: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()