Skip to content

Instantly share code, notes, and snippets.

@ilovetogetspamed
Created February 25, 2021 15:29
Show Gist options
  • Save ilovetogetspamed/7fb06934ac0247dd29a33a59be846d63 to your computer and use it in GitHub Desktop.
Save ilovetogetspamed/7fb06934ac0247dd29a33a59be846d63 to your computer and use it in GitHub Desktop.
Phidget_1023_0_Python_Example
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Devices.Log import *
from Phidget22.LogLevel import *
from Phidget22.Devices.RFID import *
from Phidget22.Devices.DigitalOutput import *
import traceback
import time
#Declare any event handlers here. These will be called every time the associated event occurs.
def onRFID0_Tag(self, tag, protocol):
print("Tag: " + str(tag))
print("Protocol: " + RFIDProtocol.getName(protocol))
print("----------")
def onRFID0_TagLost(self, tag, protocol):
print("Tag: " + str(tag))
print("Protocol: " + RFIDProtocol.getName(protocol))
print("----------")
def onRFID0_Attach(self):
print("Attach!")
def onRFID0_Detach(self):
print("Detach!")
def onRFID0_Error(self, code, description):
print("Code: " + ErrorEventCode.getName(code))
print("Description: " + str(description))
print("----------")
def onDigitalOutput0_Attach(self):
print("Attach [0]!")
def onDigitalOutput0_Detach(self):
print("Detach [0]!")
def onDigitalOutput0_Error(self, code, description):
print("Code [0]: " + ErrorEventCode.getName(code))
print("Description [0]: " + str(description))
print("----------")
def onDigitalOutput1_Attach(self):
print("Attach [1]!")
def onDigitalOutput1_Detach(self):
print("Detach [1]!")
def onDigitalOutput1_Error(self, code, description):
print("Code [1]: " + ErrorEventCode.getName(code))
print("Description [1]: " + str(description))
print("----------")
def onDigitalOutput2_Attach(self):
print("Attach [2]!")
def onDigitalOutput2_Detach(self):
print("Detach [2]!")
def onDigitalOutput2_Error(self, code, description):
print("Code [2]: " + ErrorEventCode.getName(code))
print("Description [2]: " + str(description))
print("----------")
def main():
try:
Log.enable(LogLevel.PHIDGET_LOG_INFO, "phidgetlog.log")
#Create your Phidget channels
rfid0 = RFID()
digitalOutput0 = DigitalOutput()
digitalOutput1 = DigitalOutput()
digitalOutput2 = DigitalOutput()
#Set addressing parameters to specify which channel to open (if any)
digitalOutput0.setChannel(0)
digitalOutput1.setChannel(1)
digitalOutput2.setChannel(2)
#Assign any event handlers you need before calling open so that no events are missed.
rfid0.setOnTagHandler(onRFID0_Tag)
rfid0.setOnTagLostHandler(onRFID0_TagLost)
rfid0.setOnAttachHandler(onRFID0_Attach)
rfid0.setOnDetachHandler(onRFID0_Detach)
rfid0.setOnErrorHandler(onRFID0_Error)
digitalOutput0.setOnAttachHandler(onDigitalOutput0_Attach)
digitalOutput0.setOnDetachHandler(onDigitalOutput0_Detach)
digitalOutput0.setOnErrorHandler(onDigitalOutput0_Error)
digitalOutput1.setOnAttachHandler(onDigitalOutput1_Attach)
digitalOutput1.setOnDetachHandler(onDigitalOutput1_Detach)
digitalOutput1.setOnErrorHandler(onDigitalOutput1_Error)
digitalOutput2.setOnAttachHandler(onDigitalOutput2_Attach)
digitalOutput2.setOnDetachHandler(onDigitalOutput2_Detach)
digitalOutput2.setOnErrorHandler(onDigitalOutput2_Error)
#Open your Phidgets and wait for attachment
rfid0.openWaitForAttachment(5000)
digitalOutput0.openWaitForAttachment(5000)
digitalOutput1.openWaitForAttachment(5000)
digitalOutput2.openWaitForAttachment(5000)
#Do stuff with your Phidgets here or in your event handlers.
digitalOutput0.setDutyCycle(1)
digitalOutput1.setDutyCycle(1)
digitalOutput2.setDutyCycle(1)
try:
input("Press Enter to Stop\n")
except (Exception, KeyboardInterrupt):
pass
#Close your Phidgets once the program is done.
rfid0.close()
digitalOutput0.close()
digitalOutput1.close()
digitalOutput2.close()
except PhidgetException as ex:
#We will catch Phidget Exceptions here, and print the error informaiton.
traceback.print_exc()
print("")
print("PhidgetException " + str(ex.code) + " (" + ex.description + "): " + ex.details)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment