C to python conversion
Michele Petrazzo
michele.petrazzo at TOGLIunipex.it
Sat Apr 12 06:58:47 EDT 2008
Hi all,
I'm trying to translate a simple C code into a python + ctypes (where
need), but I have some problems on char conversion. The code have
to work on Linux and talk with the serial port. I think that the problem
is that I don't translate correctly the strings.
C code:
#define START 0x33
#define RETURN_START 0x22
#define ADDR 0x01
#define WRITE_CMD 0x03
#define ALL_CMD 0xFF
...
char buf[10];
char buf_ret[10];
buf[0]=0;
buf[0]=START;
buf[1]=ADDR;
buf[2]=WRITE_CMD;
write(_fd, buf, 6);
read(_fd,buf_ret,6);
It works
python:
START = 0x33
RETURN_START = 0x22
ADDR = 0x01
WRITE_CMD = 0x03
ALL_CMD = 0xFF
lib = C.CDLL('libc.so.6')
items = [START, ADDR, WRITE_CMD]
buf = C.c_char * 10
buffer_rec = buf()
buffer_send = buf(*items)
(Here I receive: TypeError: one character string expected)
If I do:
chr(int()) of every value, it work, but:
lib.write(fd, buffer_send, 6)
lib.read(fd, buffer_rec, 6)
I stay there and block the program execution, until a CTRL+C
What can I do?
Thanks,
Michele
More information about the Python-list
mailing list