Data logging with library 22

Supporting 2.7 and 3.2+
mirvine
Phidgetsian
Posts: 6
Joined: Tue Jan 23, 2018 7:44 am
Contact:

Data logging with library 22

Postby mirvine » Tue Jan 23, 2018 8:01 am

Hi
I have searched without success on the site for an answer. I haven't found it, but sorry if it is somewhere.
Before I used phidgets library without a problem but here with the event handling system I am lost I want to log data to a file where the file changes every hour. I need the file variable in my event handler. Without using globals how is this possible ?

Many thanks
Mark

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

Re: Data logging with library 22

Postby jdecoux » Tue Jan 23, 2018 2:11 pm

Hi Mark,

Every event handler in Phidget22 for Python has a self parameter that references the instance of the object that called the event.

To pass data to the event handlers, you can add attributes to the object instance, and it will be available using the self parameter of the event, which is the first parameter in the list for the event handler.

In our examples, the self parameter is labeled 'e', though you can label it 'self' in your application.

For example, with a digital input object, you could use the following to pass a file name to its events.

Code: Select all

ch = DigitalInput()

#add a 'filename' attribute
ch.filename = "fileName.txt"

ch.setOnStateChangeHandler(StateChangeHandler)


Which can then be referenced in the event handler as follows:

Code: Select all

def StateChangeHandler(self, state):
    print("State: %f" % state)
    if(getattr(self, "filename", None) is not None):
        print(self.filename)
        #code for dealing with your file here

mirvine
Phidgetsian
Posts: 6
Joined: Tue Jan 23, 2018 7:44 am
Contact:

Re: Data logging with library 22

Postby mirvine » Wed Jan 24, 2018 5:27 am

Hi

Thanks for the reply. I didn't realize that I could add variables to my object ! I have been looking for complicated solutions yet the solution is simple.

Thanks a lot !

Mark


Return to “Python”

Who is online

Users browsing this forum: No registered users and 2 guests