Need help coding in python for a magnetic limit switch

Supporting 2.6 and up
Post Reply
Jake
Fresh meat
Posts: 1
Joined: Fri Sep 29, 2023 8:25 pm
Contact:

Need help coding in python for a magnetic limit switch

Post by Jake »

I'm trying to set up a Phidgets magnetic limit switch (this one - https://www.phidgets.com/?tier=3&catid= ... prodid=416) so that once the sensor detects the magnet, it can change a variable in my Python code.

I have connected the switch directly to my VINT hub (this one - https://www.phidgets.com/?prodid=1202) in the manner as shown on the Phidgets website (photo attached).

But whenever I run my code, I get the following error"

"Phidget Exception 3: No matching devices were found to open. Make sure your device is attached, and that your addressing parameters are specified correctly. If your Phidget has a plug or terminal block for external power, ensure it is plugged in and powered."


The connections are all fine, as my set up works in the phidget control panel on windows.

So I assume my code isn't entirely correct (I'm used to coding to control motors, which informed much of the outlay of my code).

What am I doing wrong?

Here is my code:

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
import os
import datetime
from time import sleep 

new_position = None

def check_position():
    try:
      digital_input = DigitalInput()
        digital_input.setDeviceSerialNumber(703224) # Define your VINT hub serial number (replace with your actual serial number)
        digital_input.setChannel(3) # Define your VINT hub port
        # Create a DigitalInput object for the Magnetic Contact Switch

        # Open the Phidget device
        digital_input.openWaitForAttachment(1000)

        # Check the state of the limit switch
        limit_switch = digital_input.getState()

        if limit_switch:
            new_position = "Found Limit Switch"
        else:
            new_position = "Not Found Limit Switch"

    except KeyboardInterrupt:
        print("Exiting...")

    finally:
        if digital_input:
            digital_input.close()

while True:
    check_position()

    # Check the position and print it
    if new_position == "Found Limit Switch":
        print("Switch Found")
    else:
        print("Switch Not Found")

    # Sleep for 1 second
    sleep(1)
    

Image
Attachments
3560_0_Alt3.jpg
(97.85 KiB) Not downloaded yet
User avatar
Patrick
Lead Developer
Posts: 634
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Need help coding in python for a magnetic limit switch

Post by Patrick »

You'll need to set IsHubPortDevice to true.

-Patrick
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests