Hi everybody!

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 :
--example--
command1 : "\x02\xa1"
command2 : "\x03\xa2\xb5"
etc...

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!

Try something like string formatting ...

myvalue = 16

# %02x creates a 2 digit hex byte padded with zero if needed
command2 = r"\x03\xa2\x%02x" % myvalue

print command2  # \x03\xa2\x10

Edited 6 Years Ago by vegaseat: pad

Use raw string

string = r"Everything here is intepreted as is \crap \ jakflj\ \adf\"

Does not work. Tried it to...

>>> r"\\"
'\\\\'
>>> r"\"
  File "<stdin>", line 1
    r"\"
       ^
SyntaxError: EOL while scanning single-quoted string

Try something like string formatting ...

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

print command2  # \x03\xa2\x10

Does not work either
Print function "escapes" '\\' correctly
But if you type :

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

# JUST OUTPUT THE STRING INSTEAD OF PRINTING IT
command2  #\\x03\\xa2\\x10

Does not work either
Print function "escapes" '\\' correctly
But if you type :

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

# JUST OUTPUT THE STRING INSTEAD OF PRINTING IT
command2  #\\x03\\xa2\\x10

For crying out loud! Don't use the Python Shell and pass it on as a Python program!

The Python Shell (the >>> give it away) is for little, often meaningless tests.

Edited 6 Years Ago by vegaseat: n/a

How about this:

>>> import sys
>>> for i in r"\x03\xa2": sys.stdout.write(i[:1])

\x03\xa2
>>>

Ok, i probably didn't make it clear enough.
Sorry about this.

I have :

...
ser = serial.Serial()
ser.port = 0
ser.baudrate=38400
ser.open()
...

x=16
part1 = '\x03\xa2'
part2 = hex(x)[1:]

command2 = part1 + '\\' + part2

...
#and then 
ser.write(command2) #FAILS because as i said command2 = '\x03\xa2\\x10'
# same thing if i use raw string.
...

And it makes no difference if i use the python shell or pass it on as a Python program.

As far as I can tell, both of these options work.

x=16
backslash = chr(92)
part1 = '\x03\xa2'
part2 = hex(x)[1:]
 
command2 = "%s%s%s" % (part1, backslash, part2)
command3 = part1 + backslash + part2

As far as I can tell, both of these options work.

x=16
backslash = chr(92)
part1 = '\x03\xa2'
part2 = hex(x)[1:]
 
command2 = "%s%s%s" % (part1, backslash, part2)
command3 = part1 + backslash + part2

None of them worked...
still the serial transmission :
ser.write(command2) or ser.write(command3) sends the wrong number (92)

Edited 6 Years Ago by ktsangop: n/a

For me you look like mixing the chr() with hexadesimal numbers and string escapes. Is this not correct?

x=0xb5
part1 = '\x03\xa2' ## two chars chr(0x3) and chr(0xa2)
part2 = chr(x) ## ALSO CHAR
command2 = part1 + part2
print repr(command2)

"""Output:
'\x03\xa2\xb5'
>>> """

Edited 6 Years Ago by pyTony: COMMENTS

Directed at ktsangop,

Are you writing your program with an editor, save it as a .py file and then run it, or are you simply running it directly from the Python interpretive shell?

If you run it from the shell, we can argue till the cows lay eggs.

Directed at ktsangop,

Are you writing your program with an editor, save it as a .py file and then run it, or are you simply running it directly from the Python interpretive shell?

If you run it from the shell, we can argue till the cows lay eggs.

Of course i write python scripts... i just use the shell to test fast some functions AFTER i find bugs in my script...

For me you look like mixing the chr() with hexadesimal numbers and string escapes. Is this not correct?

x=0xb5
part1 = '\x03\xa2' ## two chars chr(0x3) and chr(0xa2)
part2 = chr(x) ## ALSO CHAR
command2 = part1 + part2
print repr(command2)

"""Output:
'\x03\xa2\xb5'
>>> """

Thanks a million tonyjv!!!
I messed up the hex, char etc representations... i always do...
:)
My script runs smoothly now.

Thanks everybody for helping me out.
Cheers

Of course i write python scripts... i just use the shell to test fast some functions AFTER i find bugs in my script...
...
...

Then this code you gave:

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

# JUST OUTPUT THE STRING INSTEAD OF PRINTING IT
command2  #\\x03\\xa2\\x10

should not give any display. See what I mean?

Then this code you gave:

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

# JUST OUTPUT THE STRING INSTEAD OF PRINTING IT
command2  #\\x03\\xa2\\x10

should not give any display. See what I mean?

I just used the shell to show what is really stored in command2 variable.
The above code inside a script was also sending the wrong bytes to the serial interface.
Anyway, as i said i messed up with the hex representation...

This question has already been answered. Start a new discussion instead.