read all digital inputs on 1012 DIO

Supporting 2.7 and 3.2+
Post Reply
DarkLight
Phidgetsian
Posts: 12
Joined: Wed Dec 27, 2017 6:59 am
Contact:

read all digital inputs on 1012 DIO

Post 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?
Last edited by DarkLight on Tue Oct 16, 2018 3:09 am, edited 1 time in total.
fraser
Engineering
Posts: 138
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: read all digital inputs on 1012 DIO

Post by fraser »

The link you gave works for me.
DarkLight
Phidgetsian
Posts: 12
Joined: Wed Dec 27, 2017 6:59 am
Contact:

Re: read all digital inputs on 1012 DIO

Post 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

jdecoux
Labview Developer
Posts: 180
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: read all digital inputs on 1012 DIO

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests