473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C to python conversion

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
Jun 27 '08 #1
4 2682
On Apr 12, 5:58*am, Michele Petrazzo <michele.petra...@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
Jun 27 '08 #2
En Sat, 12 Apr 2008 07:58:47 -0300, Michele Petrazzo
<mi**************@TOGLIunipex.itescribió:
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

Jun 27 '08 #3
En Sat, 12 Apr 2008 07:58:47 -0300, Michele Petrazzo
<mi**************@TOGLIunipex.itescribió:
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

Jun 27 '08 #4
On Apr 13, 7:58 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Sat, 12 Apr 2008 07:58:47 -0300, Michele Petrazzo
<michele.petra...@TOGLIunipex.itescribió:
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
The easiest way is to use struct:

START = 0x33
RETURN_START = 0x22
ADDR = 0x01
WRITE_CMD = 0x03
ALL_CMD = 0xFF

buf = struct.pack('3b3x', START, ADDR, WRITE_CMD)
os.write(_fd, buf)
buf_ret = os.read(_fd, 6)

And, definitely, no need for ctypes here.

--
Ivan Illarionov
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
10
by: Willem | last post by:
When I run the follwing code using Python 2.3: from time import clock t1 = clock () for i in range (10000): a = int ('bbbbaaaa', 16) t2 = clock () for i in range (10000): a = long ('bbbbaaaa',...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
27
by: Josh | last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI screens) that we want to get out of VB and into a better language. The program is over 10 years old and has already been...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.