Pyserial question

David Goodger goodger at python.org
Mon Jan 5 11:10:37 EST 2004


Kelia Nichols wrote:
 > I am using Pyserial to work with a RS232 device.  My question is,
 > how do I write hex to the device or cannot write hex to it pyserial?

What do you mean exactly?  Do you want to write a value in hex to the
RS232 port (e.g. write 26 [decimal] as "1A")?  If so, just use string
formatting:

 >>> "%02X" % 26
'1A'

Or do you want to send a byte for which you know the hex
representation (e.g. send "1A" as ASCII 26)?  If so, use integer
conversion & character value conversion:

 >>> chr(int('1a', 16))
'\x1a'

If it's more complex than that (values larger than 255, etc.), the
"struct" module may help.  Post more details for better help.

-- David Goodger






More information about the Python-list mailing list