0

This is a followup question to my question , that was left unanswered
EDIT and organized

from my module:

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

I need to go over each byte in buff and i tried the following:

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)

and i get "error:TypeError: 't_input_buff' object is not iterable" upon executing the for command

i also tried use str(buff) with no luck
Any suggestions?

2
  • please, give also description of "data_block" structure so I can help Commented Dec 4, 2011 at 9:23
  • I update my question with data_block and sys_flags strucutres definitions Commented Dec 4, 2011 at 9:34

3 Answers 3

4

Check out 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[:])

Output:

0100000055555555020000000300000004000000050000000600000007000000

You can also use ctypes casting:

p=cast(byref(x),POINTER(c_byte*sizeof(x)))
print p.contents[:]

Output:

[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]

Note: In Python 3, buffer no longer exists. bytes, and list(bytes(...)) give the equivalent of buffer and the ctypes casting example.

Sign up to request clarification or add additional context in comments.

2 Comments

I checked out the 'buffer' and it seems to work. however i prefer the 'union' solution (maybe because i'm used to write in C) . maybe i will use the 'buffer' option to take slice of the source by using offset parameter
V good exposition. It might be useful to explicitly say that the 3 fields Alarm1, Alarm2 and reserved form a 32 bit word, with Alarm1 as bit 0, Alarm2 as bit 1 and reserved as bits 2 to 31. Hence the data in this word will appear as 5 repeated 8 times.
0

I don't know what is the actual type of your "buff" variable, but you can try to explicitly convert it to string in the loop head

for curChar in str(buff):

Comments

0

I found the solution:(i shortened the original structures for easier understanding)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.