Communicating with multiple devices in an event handler

Supporting 2.7 and 3.2+
balcs
Fresh meat
Posts: 3
Joined: Wed Nov 14, 2018 10:54 pm
Contact:

Communicating with multiple devices in an event handler

Postby balcs » Wed Nov 14, 2018 11:17 pm

It is possible that I am just missing something simple here, but it's not clear how to make an event handler in Python aware of multiple Phidgets (that is, not just the caller Phidget). What I am trying to do is have a cgi script running on a Phidgets SBC change two voltage output settings on voltage output Phidgets connected to the SBC. Of course, the cgi script can't just attach the voltage output Phidgets, set voltages, and then close them, because the voltage outputs go back to zero on close (although this approach would be much more sensible in this application, because the voltage doesn't need to be set very often....is there any way to have the voltage output just keep its setting on close?).

But anyway, based on the documentation, it seems like the way to do this is to have the cgi script attach a Phidgets dictionary, change a value in the dictionary, and then exit. Then a separate program that has also attached the dictionary sets an event handler such that an update to a dictionary value triggers changes in the voltage outputs. So then I need to have a program running in the background that has attached three Phidgets (dictionary, voltage output 1, voltage output 2) and also set an event handler that triggers when a key in the dictionary is updated (by a totally different program in this case). The thing that I'm missing is how to make the event handler (which is triggered by the dictionary object) able to set parameters of the other two objects. Is there some example code that does something like this? Or am I totally out to lunch and there is a different way to do this?

thanks,

User avatar
mparadis
Site Admin
Posts: 656
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Communicating with multiple devices in an event handler

Postby mparadis » Thu Nov 15, 2018 9:21 am

The dictionary shouldn't be necessary in this case.

If all you need is a program that will turn the two outputs, it's as simple as opening the two channels, setting their properties, and then keeping them that way until the program is closed. There is no way to keep the voltage outputs on after the program is closed, so no matter what there will need to be a program running as long as you want the outputs on.

In order to open more than one Phidget, you'll need to create a new handle for each one and set their parameters separately before opening each one. Here's a video showing how this works.

Let me know if it's still unclear or if I misunderstood what you're trying to do.

balcs
Fresh meat
Posts: 3
Joined: Wed Nov 14, 2018 10:54 pm
Contact:

Re: Communicating with multiple devices in an event handler

Postby balcs » Thu Nov 15, 2018 1:52 pm

The program that has the voltage output Phidgets attached needs to respond to external commands (in this case from a separate computer on the network that is controlling lots of hardware) to change voltages while it is running. Because of the nature of the equipment, the control voltage can't be allowed to go to zero during changes, so the program has to run continuously and somehow detect a external request that a voltage change is needed. The other thing I thought about was having the program poll a separate file somewhere that can be changed by an external program, but the dictionary approach seemed more sensible because the event handler aspect of the dictionary would eliminate polling. However, this is the first time I've tried messing around with Phidgets programming, so other suggestions are welcome.

As regards multiple Phidgets, I've successfully written programs to open several Phidgets at once and do things with them. So that's fine. The question is how an event handler called by one Phidget can do things with other Phidgets in addition to just the caller

User avatar
Patrick
Lead Developer
Posts: 3099
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Communicating with multiple devices in an event handler

Postby Patrick » Thu Nov 15, 2018 2:44 pm

Hi,

If you just want to respond to requests over then network, you need to write a long-running server program. It sounds like you want to respond to web requests using cgi python scripts, but you could simply use a python web server framework, such as CherryPy, to handle these incoming requests in the context of a long running Python server.

As for your event handler question, I'm not too sure what the problem is.. you just define your Phidgets objects, set up handlers, and open them all, and then in the dictionary change event, you set the output on the voltage output objects.

-Patrick

User avatar
mparadis
Site Admin
Posts: 656
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Communicating with multiple devices in an event handler

Postby mparadis » Thu Nov 15, 2018 3:03 pm

If you're talking about how to give events access to Phidget handles without using global variables, you can do it by adding the handles as a property of the event handler (which adds it to the "self" object) as seen in the code sample on this page.

balcs
Fresh meat
Posts: 3
Joined: Wed Nov 14, 2018 10:54 pm
Contact:

Re: Communicating with multiple devices in an event handler

Postby balcs » Thu Nov 15, 2018 5:38 pm

Aha. That link is what I was looking for. Thanks.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 2 guests