1

I'm trying to set up a program that reads an RFID chip with a RFID reader through a USB serial connection. With the following code, I can read the tag, but the result is returned as a single character on each line.

import serial

ser = serial.Serial(
    port='COM4',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

while True:
    for line in ser.read():
        print(chr(line))


ser.close()

Output:

connected to: COM4

4
0
0
0
0
4
1
2
8
6
C
E
F
F

How do I read the full line at once? I have tried using ´ser.readline()`, but this does not make any difference. Also, I do not know what the square symbol in the end of the read means. As far as I have tested, the square symbol always comes at postion 15, but there are sometimes additional characters after it.

In the end, I need to be able to scan and RFID and then search for the tag in a predefined list to see if it is located in the list.

Hjalte
  • 376
  • 5
  • 17
  • The "square" is the ETX (end of text) character (0x03). You can use `readline` by telling python to use ETX as the EOL (end of line) character. See [pySerial 2.6: specify end-of-line in readline()](//stackoverflow.com/q/16470903) – 001 Jan 18 '18 at 14:37
  • Thank you for the clarification @Johnny Mopp! I'm having a lot of trouble setting it up, however, How do I specify the character (0x03) to be the EOL? Could you provide an example or spell it out a bit more for me? I have experimented with the examples given in the link you provide, but I have had no luck reading the tag at all. – Hjalte Jan 25 '18 at 14:59

1 Answers1

0

Why don't you just concatenate all the digits?

RFID_string = ""
while True:
    for line in ser.read():
        if ((chr(line)) == ETX):
            break;
        else:
            ss += (chr(line))

You are getting the ETX (end of text) always in the 15th digit because it's the standard from your RFID Reader/Tag. There are other standards such as 10 digits.

Matheus Torquato
  • 1,293
  • 18
  • 25
  • It is a posibility, but I think it is a suboptimal one. Using the original solution would ensure that the RFID line and only the RFID line is returned (and only when it is complete). I believe your solution would add any characters there happens to be after the ETX (I know they probably shouldn't be there, but they are)? – Hjalte Jan 30 '18 at 14:06
  • Not really, at least for me. I use exactly this very same code and it works fine for me. – Matheus Torquato Jan 31 '18 at 15:17
  • Okay, that sounds good. I'm having the issue that the output is either a triangle (first digit, line break, first+second digit, line break, etc.) or else the full line is printed continuously (depending on indentation). – Hjalte Feb 06 '18 at 08:24
  • I think you should check your RFID Reader configuration. You should be able to set some parameters such as automatic ETX after each reading. – Matheus Torquato Feb 08 '18 at 01:16