I am trying to send a string variable contains the command.
Like this:
value=\"[0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]\"
self.s.write(serial.to_bytes(value)
serial.to_bytes takes a sequence as input. You should remove double quotes around value
to pass a sequence of integers instead of a str
representing the sequence you want to pass:
value = [0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]
self.s.write(serial.to_bytes(value)) # works now
In the first case, you sent a sequence of bytes representing "[0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]"
. Now, you will send the sequence [0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]
as expected.
If you want to send a string, just send it as bytes
:
# Python 2
self.s.write('this is my string')
text = 'a string'
self.s.write(text)
# Python 3
self.s.write(b'this is my string')
text = 'a string'
self.s.write(text.encode())
And for a sequence:
for value in values:
# Python 2
self.s.write(value)
# Python 3
self.s.write(value.encode())