Page 1 of 1

How to read different thermocouples from 1048

Posted: Sun Sep 10, 2017 7:55 pm
by brianz
I'm trying to update my code from Phidgets21 to Phidget22 in Python and the APIs seem to have changed quite drastically. It's really confusing how to get different temperatures when the 4-input 1048 Temperature Sensor.

Previous, the temperature changed callback had a signature like this:

Code: Select all

def TemperatureChangeHandler(event):
    print(event.index, event.temperature)
...where `index` was the thermocouple number, from 0-3.

The callback now get's two variables, the event and temperature. However, "index" is now gone from the event object. How are we supposed to read all four temperature?

Code: Select all

def TemperatureChangeHandler(event, temperature):
    # no more event.index
I've looked through the docs and they are unhelpful...these details are completely missing from them, sadly. There is zero information on how to handle multiple TC inputs.

Help appreciated.

Re: How to read different thermocouples from 1048

Posted: Mon Sep 11, 2017 7:57 am
by mparadis
In Phidget22, each channel needs to have it's handler function set. You can set them to the same function, or different functions. It might look like this:

Code: Select all

 ch0 = TemperatureSensor()
 ch1 = TemperatureSensor()
 ch2 = TemperatureSensor()
 ch3 = TemperatureSensor()
 
 ch0.setChannel(0)
 ch1.setChannel(1)
 ch2.setChannel(2)
 ch3.setChannel(3)
 
 ch0.setOnTemperatureChangeHandler(TemperatureChangeHandler)
 ch1.setOnTemperatureChangeHandler(TemperatureChangeHandler)
 ch2.setOnTemperatureChangeHandler(TemperatureChangeHandler)
 ch3.setOnTemperatureChangeHandler(TemperatureChangeHandler)
 
 ch0.openWaitForAttachment(5000)
 ch1.openWaitForAttachment(5000)
 ch2.openWaitForAttachment(5000)
 ch3.openWaitForAttachment(5000)
 
If you use the same event handler for all four channels, you can tell which channel triggered it by using the event argument:

Code: Select all


def TemperatureChangeHandler(e, temperature):
    print("Channel: %i  |  Temperature: %f" % (e.getChannel(), temperature))