Page 1 of 1

Data logging with library 22

Posted: Tue Jan 23, 2018 8:01 am
by mirvine
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

Re: Data logging with library 22

Posted: Tue Jan 23, 2018 2:11 pm
by jdecoux
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

Re: Data logging with library 22

Posted: Wed Jan 24, 2018 5:27 am
by mirvine
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