0

I have a c program that reads a rfid tag. I am trying to get output from that c program (it uses sudo and args) and compare it to a string

Here is my code as well as some debugging information.

import subprocess
#args - the c program and its args
args = ["sudo", "./rc522", "-r", "-b", "1"]
process = subprocess.Popen(args,stdout=subprocess.PIPE,stderr=None)
rfidRead=process.communicate()[0]
rfidRead=rfidRead.decode('utf-8')
print (len(rfidRead))
rfidRead = rfidRead[14:440]
print (len(rfidRead))
print (rfidRead)

if "49.4f.09.0a.0c.0f.00.00.00.00.00.00.00.00.00.00" in rfidRead:
        print("unlocked")
else:
        print("Not unlocked")

here is the output...

446
426
49.4f.09.0a.0c.0f.00.00.00.00.00.00.00.00.00.00
Not unlocked

I just cant find the hidden characters and things from the pipe output Can you help please?

This is the rfidRead output using repr:

'\x1b[1;93m4\x1b[00m\x1b[1;93m9\x1b[00m.\x1b[1;93m4\x1b[00m\x1b[1;93mf\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m9\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93ma\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93mc\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93mf\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0'
ColinP
  • 1
  • 2
  • Are you sure you are reading the right pipe? Might the output be on stderr instead of stdout? – Dekker1 Sep 19 '17 at 01:33
  • Try `print(repr(rfidRead))` to see what's actually in the string - any non-printable characters will be shown as escape sequences. – jasonharper Sep 19 '17 at 01:45

1 Answers1

0

I worked out the answer with the help of jasonharper. After doing the repr(...) I decided to google \x1b and found a stackoverflow article about vt100 and that this is vt100 code, so I did another google search to decode vt100, and I got a regular expression to remove the escape codes... here is the article I found. How can I remove the ANSI escape sequences from a string in python

ColinP
  • 1
  • 2