1

We are trying to read RFID tags using raspberry pi b+. We used following script in the python .

import serial
import time
serial=serial.Serial("/dev/ttyUSB0", baudrate=2400)
while True:
    if serial.inWaiting()>0:
        read_result=serial.read(12)
        print("Read card {0}",format(read_result.decode("utf-8","replace")))
        print("Sleeping 2 sec")
        time.sleep(2)
        serial.flushInput() 

On reading the tag it gives the error as:

File "/home/pi/rfidtry/try.py",line 7, in <module>
print("Read card {0}",format(read_result.decode("utf-8","replace")))
UnicodeEncodeError: 'ascii' codac can't encode character u'\uffd' in position 2
:ordinal not in range(128)
ashwinbhy
  • 590
  • 1
  • 7
  • 27
  • Can you omit the `replace` and show the output? (It should give you a slightly different error message) – hek2mgl Mar 28 '15 at 10:52

3 Answers3

0

As suggested in this post, it might work if you encoded your unicode string first to ascii and then decode it.

read_result.encode('ascii','replace').decode('utf-8')

Of course, you will lose the non-working characters.

Community
  • 1
  • 1
Julien Spronck
  • 14,079
  • 4
  • 41
  • 51
0

Did you try this

print read_result

and see if it works (instead of )

print("Read card {0}",format(read_result.decode("utf-8","replace")))
Illusionist
  • 4,754
  • 8
  • 41
  • 69
0

The problem was with the baudrate. The earlier reader i used was 2400 but the one i am using now is 9600.

ashwinbhy
  • 590
  • 1
  • 7
  • 27