2

This is a kind of follow-up from my last question if this can help you.

I'm defining a few ctype structures

class EthercatDatagram(Structure):
    _fields_ = [("header", EthercatDatagramHeader),
                ("packet_data_length", c_int),
                ("packet_data", POINTER(c_ubyte)),
                ("work_count", c_ushort)]

class EthercatPacket(Structure):
    _fields_ = [("ether_header", ETH_HEADER),
                ("Ethercat_header", EthercatHeader),
                ("data", POINTER(EthercatDatagram))]

note that this is parsed correctly by python, the missing classes are defined elsewhere. My problem is when I call the following code

packet = EthercatPacket()
ethercap.RecvPacket(byref(packet))
print packet.data.header

This is incorrect. As I understand the problem, data is some kind of pointer so it isn't (really) mapped to EthercatDatagram, hence, the parser doesn't know the underlying header field.

is there some way to read that field as well as any other field represented by POINTER()?

2 Answers 2

1

The square-bracket notation is indeed correct. For reference, here's a snippet from some ctypes code I recently made:

class Message(Structure):
    _fields_ = [ ("id", BYTE), ("data", POINTER(BYTE)), ("data_length", DWORD) ]
    def __repr__(self):
        d = ' '.join(["0x%02X" % self.data[i] for i in range(self.data_length)])
        return "<Message: id = 0x%02X, " % (self.id) + "data_length = " + str(self.data_length) + (self.data_length > 0 and (", data: " + d) or "") + ">"
Sign up to request clarification or add additional context in comments.

Comments

0

Ok I got it working

correct code was

print packet.data.header[0]

thanks to the 7 person who dared to look at the question

the google string for the answer was : python ctype dereference pointer 3rd hit

1 Comment

I've answered this question before on Stackoverflow.

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.