Convert variable directly into a string (no ASCII)
timr at probo.com
Sat May 2 15:10:27 EDT 2009
Justin Rajewski <Justin at EmbeddedMicro.com> wrote:
>I need to print variables out over serial, however I need them to not be
>in ASCII, ie if the variable is 5 then print 5 not "5".
>The function that writes to the serial port requires a string and I can
>send non-variables out with the string "/x05" for 5.
>Is this even possible?
Of course. The struct and array modules can help you with this. Note that
you have to know how big each variable should be.
For example, if you want to sent 1, 2, and 3 out as bytes, you can do:
a = 1
b = 2
c = 3
serial = struct.pack( 'BBB', a, b, c )
If you need them as 16-bit ints, you can use H instead of B.
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list