I'm facing a really tricky problem in python.
I am using the pyserial package to communicate with an external serial interface board.
The board accepts certain hexadecimal strings as commands in the following form :
command1 : "\x02\xa1"
command2 : "\x03\xa2\xb5"
Some of the commands are static (as command1) and some other are dynamic (as command2).
Dynamic means that for command2 the part of the string "\x03\xa2" is always the same BUT "\xb5" is a variable passed to script (say as an iteger parsed from an txt file).
What i need to do is to concatenate :
"\x03\xa2" with "\xb5" (or anything that the second part of the script might be).
I tried :
x=16 ===> INPUT TO THE SCRIPT
part1 = '\x03\xa2' ===> CONSTANT
part2 = hex(x)[1:] ===> RESULTS IN 'x10'
command2 = part1 + '\\' + part2 ====> RESULTS IN '\x03\xa2\\x10'
Which is incorrect because the boards understands the extra backslash as : hexadecimal (5C) = decimal (92)
So the wrong number (92 instead of 16) is sent.
Is there ANY way to add a single backslash to this string concatenation??
Thanks in advance!