I am trying to use the bit fields in a useful way to create packed fields that can be sent out as packets. Let's see if my code can help explain:
from ctypes import * class myPacketHeader(Structure): _pack_ = 1 _fields_ = [("seqnum", c_ubyte, 8), ("is_command", c_uint, 1), ("address", c_uint, 31), ("length", c_ubyte, 8)] header = myPacketHeader() header.seqnum = 255 header.is_command = 1 header.address = 0x1234 header.length = 54 print "Size of header is: %d" % sizeof(header) # Now I would like to send out the packed bytes...
This looks to work as I would like -- the output is:
Size of header is: 6
The next step is to convert the header to an array of bytes (or a list). I'd hope that there is a built-in method to get the packed bytes. Anyone know how to get there?