Getting a data structure
Graham Fawcett
fawcett at teksavvy.com
Wed Aug 13 21:07:12 EDT 2003
Mike solem wrote:
>I need to pass a data structures from a C program, to a python program over RS232.
>I'm not quite sure how to recreate a data structure from a string of serial data.
>In C I would do it like this:
>
>---------------------- This compiles under gcc ----------
>#include <stdio.h>
>
>typedef struct{
> int a;
> char b;
>}myStruct;
>
>int main()
>{
> char data[]= {1,2,3,4,5,6,7,8,9};
> myStruct* s1;
>
> s1 = (myStruct*)&data;
> printf("%x %d\n", s1->a, s1->b);
> return 0;
>}
>--------------------------------------------
>
>The 'data' array above would be a buffer which receives data from the serial link.
>So the questions are, what type of python data stucture to use, and how to
>initialize it from the buffer.
>The struct I'm using is a lot more complex than the one I used here. It's built up of a couple
>of other structs.
>
>Thanks
>Mike
>
>
>
Does the standard "struct" module not meet your requirements?
"This module performs conversions between Python values and C structs
represented as Python strings. It uses /format strings/ (explained
below) as compact descriptions of the lay-out of the C structs and the
intended conversion to/from Python values. This can be used in handling
binary data stored in files or from network connections, among other
sources."
http://www.python.org/doc/current/lib/module-struct.html
-- Graham
More information about the Python-list
mailing list