Re-evaluating a string?

bugnthecode bugnthecode at gmail.com
Sun Jul 23 18:58:12 EDT 2006


I'm writing a program to send data over the serial port. I'm using
pyserial, and I'm on WindowsXP. When I use literals I can get the data
accross how I want it for example:

                   1     2      3      4     5      6
serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r')

1=Get devices attention
2=Select channel on device
3=Rate for movement
4=Low byte of 16 bits
5=High bytes of 16 bits
6=Carriage return signaling command is over

This command works as desired. Sends the first 3 ASCII characters, then
some numbers in hex followed by a carriage return.

My problem is that the "write()" function only takes a string, and I
want to substitute variables for the hex literals.

I know that I can use the "hex()" function and it will return a string
with the appropriate hex value, and I could combine this with some
other literals like "\\" to create my desired hex literal, but then I
would need something to re-parse my string to change my ASCII text into
the appropriate hex values.

Any ideas on how I may do this? Any help is greatly appreciated.

Thanks,
Will




More information about the Python-list mailing list