En Sat, 12 Apr 2008 07:58:47 -0300, Michele Petrazzo <[EMAIL PROTECTED]> escribió:
> 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); You don't even need ctypes. In C, `char` is a small integer: 'A' and the number 65 are interchangeable. In Python, there are no chars but strings of length 1, which are not the same thing as their ordinal integer. The easiest way is to define those constants as strings instead: START = chr(0x33) RETURN_START = chr(0x22) ADDR = chr(0x01) WRITE_CMD = chr(0x03) ALL_CMD = chr(0xFF) NUL = chr(0) buf = START + ADDR + WRITE_CMD + NUL + NUL + NUL # I assume the buffer was initialized to NULs, because only 3 bytes # are filled but 6 bytes are written. os.write(_fd, buf) buf_ret = os.read(_fd, 6) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list