Connecting RFID reader to TouchDesigner

Hi all,

I bought a USB RFID reader, and I want to connect it to TouchDesigner so that it can read the tag and output 1 as a result.

Currently, I’ve been able to get the KeyboardIn DAT to read and output the tag into the textport, but I haven’t been able to get the tags to actually impact a CHOP. When I run the code, I get the debug “Stored tag is empty.”

stored_tag = ""  # Initialize an empty string to store the tag input

def onKey(inDAT, key, scancode, alt, ctrl, shift, state, repeat, command, option, win, caps, num, scroll, kana, lmeta, rmeta):
    global stored_tag  # Declare stored_tag as a global variable to access it

    # Only act when the key is pressed down
    if state:
        # Debugging: print the key that is pressed
        print(f"Key pressed: {key}")

        # Ignore the Enter key (newline) which might be added automatically
        if key == '\n' or key == 'enter':
            print("Enter key pressed, ignoring it.")
            return  # Do nothing if Enter key is pressed

        # Only append the key if it's a number
        if key.isdigit():  # Ensure it's a numeric key
            stored_tag += key  # Append each key press to stored_tag
            print(f"Updated stored_tag: {stored_tag}")  # Debugging to see the current value of stored_tag
        else:
            print(f"Non-numeric key pressed: {key}")  # If it's not a number, we'll ignore it

    # Check if stored_tag is empty after processing
    if not stored_tag:
        print("Stored tag is empty.")  # Debugging to confirm if stored_tag is getting updated

    # Try converting stored_tag to an integer to compare with the RFID tag value
    try:
        if stored_tag:  # Only attempt to convert if stored_tag is not empty
            tag_value = int(stored_tag)  # Convert stored_tag to an integer
            print(f"Converted stored_tag to: {tag_value}")  # Debugging to show the conversion

            # Get the Table DAT containing RFID tags (replace 'rfid_tags' with the actual Table DAT name)
            tag_table = op('rfid_tags')  # Get the table with RFID tag values
            if tag_table is None:
                print("Error: Table DAT 'rfid_tags' not found!")
                return

            # Iterate through each row in the Table DAT
            for row in tag_table.rows():
                table_tag_value = row[0].val  # Get the value in the first column (tag name or value)
                try:
                    # Convert the tag from the table to an integer and compare it
                    if tag_value == int(table_tag_value):
                        print(f"RFID Tag '{table_tag_value}' detected!")
                        op('constant1').par.value0 = 1  # Trigger the Constant CHOP to 1
                        stored_tag = ""  # Clear stored_tag for the next scan
                        return  # Exit the function as we found a match
                except ValueError:
                    # Handle cases where the value in the table is not a valid number
                    print(f"Invalid RFID tag value in table: {table_tag_value}")

        # Optional: Reset stored_tag if it's too long (error checking)
        if len(stored_tag) > 20:  # If stored_tag is too long (in case of invalid input)
            stored_tag = ""  # Clear stored_tag

    except ValueError:
        # If stored_tag can't be converted to an integer, reset it
        print(f"Stored tag is not a valid number: {stored_tag}")
        stored_tag = ""  # Clear stored_tag if conversion fails

I’m wondering if anyone else has experience connecting the RFID readers to TouchDesigner and if you have any advice on this code or the overall workflow for this connection.

Hi @sarahch2,

While I don’t have a RFID reader at hand, maybe I can try help debugging this.
First: How do you get the tag to be printed into the textport?

cheers
Markus

Hey @snaut,

I actually figured the code out in the meantime. The issue was mainly using keyInfo in the IF statements, and only defining (inDAT, keyInfo) at the function.

Here is my pasted code (this started out as ChatGPT code, so there are a lot of removable debugs)

stored_tag = ""  # Initialize an empty string to store the tag input

def onKey(inDAT, keyInfo):
    global stored_tag  # Declare stored_tag as a global variable to access it
	# Only act when the key is pressed down
    if keyInfo.state:
        # Debugging: print the key that is pressed
        print(f"Key pressed: {keyInfo.key}")

        # Ignore the Enter key (newline) which might be added automatically
        if keyInfo.key == '\n' or keyInfo.key == 'enter':
            print("Enter key pressed, resetting.")
            stored_tag = "" 
            return

        # Only append the key if it's a number
        if keyInfo.key.isdigit():  # Ensure it's a numeric key
            stored_tag += keyInfo.key  # Append each key press to stored_tag
            print(f"Updated stored_tag: {stored_tag}")  # Debugging to see the current value of stored_tag
        else:
            print(f"Non-numeric key pressed: {keyInfo.key}")  # If it's not a number, we'll ignore it

        # Check if stored_tag is empty after processing
        if not stored_tag:
            print("Stored tag is empty.")  # Debugging to confirm if stored_tag is getting updated

        # Try converting stored_tag to an integer to compare with the RFID tag value
        try:
            if stored_tag:  # Only attempt to convert if stored_tag is not empty
                tag_value = int(stored_tag)  # Convert stored_tag to an integer
                print(f"Converted stored_tag to: {tag_value}")  # Debugging to show the conversion

                # Get the Table DAT containing RFID tags (replace 'rfid_tags' with the actual Table DAT name)
                tag_table = op('rfid_tags')  # Get the table with RFID tag values
                if tag_table is None:
                    print("Error: Table DAT 'rfid_tags' not found!")
                    return

                # Iterate through each row in the Table DAT
                for row in tag_table.rows():
                    table_tag_value = row[0].val  # Get the value in the first column (tag name or value)
                    try:
                        # Convert the tag from the table to an integer and compare it
                        if tag_value == int(table_tag_value):
                            print(f"RFID Tag '{table_tag_value}' detected!")
                            op('constant1').par.value0 = 1  # Trigger the Constant CHOP to 1
                            stored_tag = ""  # Clear stored_tag for the next scan
                            return  # Exit the function as we found a match
                    except ValueError:
                        # Handle cases where the value in the table is not a valid number
                        print(f"Invalid RFID tag value in table: {table_tag_value}")

            # Optional: Reset stored_tag if it's too long (error checking)
            if len(stored_tag) > 20:  # If stored_tag is too long (in case of invalid input)
                stored_tag = ""  # Clear stored_tag

        except ValueError:
            # If stored_tag can't be converted to an integer, reset it
            print(f"Stored tag is not a valid number: {stored_tag}")
            stored_tag = ""  # Clear stored_tag if conversion fails
1 Like