question about binary and serial info

Peter Hansen peter at engcorp.com
Thu Aug 18 14:08:37 EDT 2005


nephish at xit.net wrote:
>>>>import serial
>>>>ser = serial.Serial('/dev/ttyS0', 2400, timeout= 10, bytesize=8, stopbits=1)
>>>>a = ser.read(1)
>>>>print a

It sounds like you want to convert characters into their corresponding 
integer values.  To do this, use the ord() builtin function.

 >>> ord('^')
94

Then you can do the bitwise operations you want.

(Of course, you might be looking for the struct module (see the docs on 
that) instead of just ord().  That would be the case if you want to 
convert more than one character at a time, to some numeric value.)

If this is what you want, part of the confusion was your calling the 
incoming data a "string" when it's really a series of characters, which 
you will be dealing with individually.

Another part of the confusion was referring to ASCII.  From the looks of 
things, your data is just bytes, not ASCII.  ASCII refers not to the 
bytes themselves, but to the meaning assigned to those bytes for certain 
purposes.  For example, the bytes 70, 111, and 111 are just three bytes, 
with no particular meaning to anyone.  If you want to treat them as 
ASCII, however, they represent these three characters: "Foo".  Your data 
looks like chunk when treated as ASCII, so it's probably just bytes.

-Peter



More information about the Python-list mailing list