0

I have a structure defined like so in C++ :

typedef struct __attribute__((packed)){
    uint16_t nb_elements; //2 bytes
    struct __attribute__((packed)){ // 6 bytes
        uint8_t identifier1; // 1 byte
        uint8_t identifier2; // 1 byte
        float my_data; // 4 bytes
    } element[];
} sElements;

(notice that element structures are nested in sElements structure).

I want to do the same thing in Python (using ctypes), and I am struggling at this point :

from ctypes import *    

class element(Structure):
  _fields_ = [("identifier1", c_uint, 8),
          ("identifier2", c_uint, 8),
          ("my_data", c_float, 64)]

class sElements(Structure):
  _fields_ = [("nb_elements", c_uint, 16)
          #I don't know what to put here !
         ]

How could I reproduce this nested structure array in Python ?

7
  • 1
    What do you want to do with that structure? If it's just for using it in a Python program, a simple class would be way easier... Commented Apr 3, 2017 at 19:10
  • I usually don't do bit fields for fun haha ! The goal is to send this structure via Serial, and receive it with a C program on the other side. Commented Apr 3, 2017 at 19:20
  • See stackoverflow.com/questions/8392203/… Commented Apr 3, 2017 at 19:30
  • By doing so I am afraid that only a pointer is stored in the structure, thus, I can't transfer it through Serial. Am I right ? Or all the nested structures would be copied in the structure ? Commented Apr 3, 2017 at 19:38
  • 1
    I think you are mis-understanding the use of ctypes. ctypes is used to share data with C code, not to serialize data to a specific format. If you are going to send this data via Serial what you need to know is how the bytes will be arranged on the wire, not how they will appear in memory. They are not the necessarily the same. Commented Apr 4, 2017 at 1:43

1 Answer 1

1

Here is what I did, it does not totally answer the question, but it solved my problem : (using bitstring)

import bitstring

class element():
    def __init__(self):
        self.identifier1 = None #uint:8
        self.identifier2 = None #uint:8
        self.my_data = None #float:64

    def serialize(self):
        return bitstring.pack('uint:8, uint:8, float:64', self.identifier1, self.identifier2, self.my_data)


class sElements():
    def __init__(self):
        self.nb_elements = None #uint:16
        self.element = [] #element list

    def serialize(self):
        serialized = bitstring.pack('uint:16', self.nb_elements)
        for elt in self.element:
            serialized += elt.serialize()
        return serialized

Modulo a possible endianness problem it allows to store those data (and send them) the way I want it.

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

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.