C to python conversion

Paul McGuire ptmcg at austin.rr.com
Sat Apr 12 13:16:06 CEST 2008


On Apr 12, 5:58 am, Michele Petrazzo <michele.petra... at TOGLIunipex.it>
wrote:
> 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

Here are some differences between the C and Python programs:
- the C program uses a separate buffer for reading and writing - the
Python program uses buf for both
- the C program *may* have null-filled the output buffer, I don't know
what the Python buffer contains after the first 3 characters
- in the Python program, items has only 3 characters to write; the C
program has a buffer of 10 characters

Here are some things to try in the Python program:

items = [START, ADDR, WRITE_CMD] + [0]*7

inbuf = C.c_char * 10
outbuf = C.c_char * 10
buffer_rec = inbuf()
buffer_send = outbuf(*items)

-- Paul



More information about the Python-list mailing list