Hi, I have this code to communicate to an RFID device that outputs hex values.

import serial
COMPort='COM13'
mySer=serial.Serial(COMPort, 9600, timeout=2)
try:
    mySer.flushInput()
    print "".join(["0x%2x " %(ord(c)) for c in list(mySer.readall)])
    print msg
finally:
    print 'Close'
    mySer.close()

but cause error

Traceback (most recent call last):
  File "C:\Python26\rfidtest.py", line 6, in <module>
    print "".join(["0x%2x " %(ord(c)) for c in list(mySer.readall)])
AttributeError: 'Serial' object has no attribute 'readall'

can anybody help me what I'm missing? I'm sure I got the same code worked before.

thanks in advance,
mbox+96

PySerial doesn't have a method of readall(). You will have to use readline(), readlines(), or xreadlines(). You can find a list of methods and how to use them here.

Hi tech b, thanks for the quick responce..works fine now. But I have another question..the output of one tag is "0x41 0x7d 0xff 0xf4 0xbd 0xf3 0xbb 0x47 0xfb 0x42" which is I think in hex value. How can I convert this to string or decimal value for easier reading?

mbox_96

Hi, I got the solution I need...thanks

try:
    mySer.flushInput()
    s =  "".join(["%2x" %(ord(c)) for c in list(mySer.readline())])
    print int(s, 16)

finally:
    print 'Close'
    mySer.close()

regards,
mbox_96

Hi, I got the solution I need...thanks

try:
    mySer.flushInput()
    s =  "".join(["%2x" %(ord(c)) for c in list(mySer.readline())])
    print int(s, 16)

finally:
    print 'Close'
    mySer.close()

regards,
mbox_96

Looks like you are doing something like:

try:
    s =  [ord(c) for c in mySer.readline()]
    print s
finally:
    print 'Close'

in obfuscated way.

This article has been dead for over six months. Start a new discussion instead.