> serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r') [cut] > My problem is that the "write()" function only takes a string, and I > want to substitute variables for the hex literals.
Well, you can try something like >>> import types >>> def makeString(a): ... return ''.join([type(x) != types.IntType and ... str(x) or chr(x) for x in a]) ... >>> data = ['!SC', 1, 5, 0xFA, 0, '\r'] >>> makeString(data) '!SC\x01\x05\xfa\x00\r' Thus, you can mix and match your desired data in a list, and then let Python intelligently smash together the string you want, so you can later pass that to your write() call. It does hiccup (read "throw an exception") if you have an empty string in your list. It also falls down if you try and put in an integer constant that isn't in range(256). My advice regarding these would be "don't do that". :) Alternatively, you can concoct some cousin-function to chr() that takes any old garbage along with a default, and returns either the chr() of it, unless that throws an expception, in which case you just return something like '\x00' (whatever you specified as the default). This allows you to use your favorite notation. If you like hex notation, you can use it (as in the "0xFA" in the above data). If you prefer integers, you can toss them in the mix. Alternatively, you can create a suite of API wrapper functions, such as def move(rate, low, high, channel=1): serialport.write(''.join([type(x) != types.IntType and ... str(x) or chr(x) for x in ... ['!SC', channel, rate, low, high, '\r'] ... ])) (that could be uncompacted a bit for readibility's sake...) You could then just call move(5, 0xFA, 0) and the function does the heavy work for you. Might also be more readable later for other folks coming to the project (if there are others). Just a couple ideas you might want to try. -tkc -- http://mail.python.org/mailman/listinfo/python-list