Page 1 of 1

read all digital inputs on 1012 DIO

Posted: Wed Oct 10, 2018 7:43 am
by DarkLight
The example code states :"If you are unsure how to use more than one Phidget channel with this event, we recommend going to http://www.phidgets.com/docs/Using_Multiple_Phidgets for information"

alas - the site is empty...
[Update … The link now works]

Does anyone has experience with reading all inputs once?

Re: read all digital inputs on 1012 DIO

Posted: Wed Oct 10, 2018 10:38 am
by fraser
The link you gave works for me.

Re: read all digital inputs on 1012 DIO

Posted: Tue Oct 16, 2018 3:16 am
by DarkLight
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


Re: read all digital inputs on 1012 DIO

Posted: Tue Oct 16, 2018 8:47 am
by jdecoux
Yes, that way of using Phidgets would be cumbersome. We recommend opening all your Phidgets at the start of your program, and leaving them open until the program is done. That way you only need to open them all once.

By leaving all the inputs open at the same time, you will be able to collect data from each of them much faster and more easily.