0

Hello all. I'm having a bit of trouble trying to figure out how to access data I input into a ctypes array object. This is used with a hardware SDK, so I can't really change the data format at all. Here's the code:

from ctypes import *
MsgDataType = c_uint8 * 8

msg = MsgDataType()
msg[0] = 1
msg[1] = 2
msg[2] = 3
msg[3] = 4
msg[4] = 5
msg[5] = 6
msg[6] = 7
msg[7] = 8

So basically what I would like is to type for example "msg[0]" and get out 1 as the value, but what I'm getting only 0's instead for "msg[0]", "msg[1]", etc.

Typing "msg" outputs:
<__main__.c_ubyte_Array_8 object at 0x00F58A80>

I also tried creating a pointer via "pt = pointer(msg)" and then tried to access the value using "pt[0]" but I obtain:
<__main__.c_ubyte_Array_8 object at 0x00F58AD0>

Typing "pt" outputs:
<ctypes.LP_c_ubyte_Array_8 object at 0x00F58A80>

I've also tried typing "for i in msg: print i", for which I get the error:
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
for i in msg: print i
TypeError: 'function' object is not iterable

Any ideas how I can resolve this problem? I'm probably missing something very simple, but it's beyond me as a Python/C newbie. Thanks!

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by convoluted
0
MsgDataType = c_uint8 * 8
 
msg = MsgDataType()

Not that you are using the same name twice.

print type(msg)
it may be a class in which case you would access it with
msg.field_name

0

Thanks woooee, your suggestion got me thinking and lead me to my solution. The problem I had was testing the functionality purely in the Python command line, and being a complete novice I didn't realize that if I copy and past the entire block of code

msg = MsgDataType()
msg[0] = 1
msg[1] = 2
msg[2] = 3
msg[3] = 4
msg[4] = 5
msg[5] = 6
msg[6] = 7
msg[7] = 8

into the command line, that it wouldn't sequentially execute each line. So all of the msg values mistakenly took on the value of 0. I can now read the stored values using "msg[0]", etc without problem. Thanks again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.