Reputation: 352
I am trying to read response of the serial port. (I am using a RFID module) Here is my code:
import serial
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 9600
ser.timeout = 3
ser.open()
if ser.isOpen():
ser.write("\xFF\x01\x03\x10\x02\x02\x18")
print("command written")
while ser.isOpen():
response = ser.read(5)
print("trying to read")
print(int(response,16))
At first I used directly print(response) and what I got was:
trying to read
�#��
Therefore I used print(int(response,16)) in order to convert response to integer and now I am getting error:
Traceback (most recent call last):
File "serialread.py", line 13, in <module>
print(int(response,16))
ValueError: invalid literal for int() with base 16: '\x94#\xdb\xff'
What should I do? I am pretty new to python and do not have any idea what the problem can be.
Upvotes: 0
Views: 2476
Reputation: 174614
Your string is already a hex literal:
>>> x = '\x94#\xdb\xff'
>>> x.encode('hex')
'9423dbff'
>>> int(x.encode('hex'),16)
2485378047L
Upvotes: 1