The problem is that I need to read all digital inputs as one, i.e.
When inputs 3 and 7 are at 'HIGH' state, I expect the output to be:
output = ['0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0']
Currently, I repeat the attach for each channel (attach, wait for event, append the readout to an array, detach).
Code: Select all
import time
import traceback
from Phidget22.Devices.DigitalInput import *
from Phidget22.Phidget import *
from Phidget22.Net import *
DIO = []
crash = ''
def DisplayError(e, self):
print e.message + ' ' + e.details + ' (' + str(e.code) + ')'
def onAttachHandler(self):
ph = self
try:
"""
* Get device information and display it.
"""
serialNumber = ph.getDeviceSerialNumber()
channelClass = ph.getChannelClassName()
channel = ph.getChannel()
deviceClass = ph.getDeviceClass()
if (deviceClass != DeviceClass.PHIDCLASS_VINT):
pass
#print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
# "\n\t-> Channel " + str(channel) + "\n")
else:
hubPort = ph.getHubPort()
print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
"\n\t-> Hub Port: " + str(hubPort) + "\n\t-> Channel " + str(channel) + "\n")
except PhidgetException as e:
print("\nError in Attach Event:")
DisplayError(e)
traceback.print_exc()
return
def onDetachHandler(self):
ph = self
try:
# If you are unsure how to use more than one Phidget channel with this event, we recommend going to
# www.phidgets.com/docs/Using_Multiple_Phidgets for information
print("\nDetach Event:")
"""
* Get device information and display it.
"""
serialNumber = ph.getDeviceSerialNumber()
channelClass = ph.getChannelClassName()
channel = ph.getChannel()
deviceClass = ph.getDeviceClass()
if (deviceClass != DeviceClass.PHIDCLASS_VINT):
print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
"\n\t-> Channel " + str(channel) + "\n")
else:
hubPort = ph.getHubPort()
print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
"\n\t-> Hub Port: " + str(hubPort) + "\n\t-> Channel " + str(channel) + "\n")
except PhidgetException as e:
print("\nError in Detach Event:")
DisplayError(e)
traceback.print_exc()
return
def onErrorHandler(self, errorCode, errorString):
sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" + str(errorCode) + ")\n")
def onStateChangeHandler(self, state):
# If you are unsure how to use more than one Phidget channel with this event, we recommend going to
# www.phidgets.com/docs/Using_Multiple_Phidgets for information
#print("[State Event] -> State: " + str(state))
global DIO
DIO.append(str(state))
def PrintEventDescriptions():
print("\n--------------------\n"
"\n | State change events will call their associated function every time the input reports its state has changed.\n"
" | Press ENTER once you have read this message.\n")
readin = sys.stdin.readline(1)
print("\nScan ch- {}".format(channel) )
def main():
try:
"""
* Allocate a new Phidget Channel object
"""
try:
ch = DigitalInput()
except PhidgetException as e:
sys.stderr.write("Runtime Error -> Creating DigitalInput: \n\t")
DisplayError(e)
raise
except RuntimeError as e:
sys.stderr.write("Runtime Error -> Creating DigitalInput: \n\t" + e)
raise
ch.setDeviceSerialNumber(-1)
ch.setIsHubPortDevice(False)
ch.setChannel(channel)
"""
* Add event handlers before calling open so that no events are missed.
"""
print("\nScan ch- {}".format(channel))
"""
* Open the channel with a timeout
"""
try:
ch.openWaitForAttachment(5000)
except PhidgetException as e:
pass
time.sleep(0.2)
except PhidgetException as e:
sys.stderr.write("\nExiting with error(s)...")
DisplayError(e)
traceback.print_exc()
print("Cleaning up...")
ch.setOnStateChangeHandler(None)
ch.close()
return 1
for channel in range(0,16):
main()
if DIO == []:
print ('\r\nPhidget not found')
exit(-5)
print DIO