Can't make Python scripts work

Supporting 2.7 and 3.2+
kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Can't make Python scripts work

Postby kiffer » Thu Aug 17, 2017 4:57 pm

Extreme noob here. Trying to write a very simple program (called ProblemCode.py) to read temperature data with my 1051 and K-type thermocouple. Code is VERY basic as follows:

Code: Select all

from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Devices.TemperatureSensor import *

ch = TemperatureSensor()
ch.open()

tmp = ch.getTemperature()

print(tmp)



I'm using Geany to write this script and execute it. But all I get is:

    Traceback (most recent call last):
    File "ProblemCode.py", line 8, in <module>
    tmp = ch.getTemperature()
    File "/usr/local/lib/python2.7/dist-packages/Phidget22/Devices/Tem
    peratureSensor.py", line 186, in getTemperature
    raise PhidgetException(result)
    Phidget22.PhidgetException.PhidgetException

If I open a terminal and type "python3.6 ProblemCode.py" I get the same response. However, if I open a terminal and type "python3.6" and then at the prompts type the exact same code directly into the terminal it works fine. Why would this be?

headcrash
Phidgetsian
Posts: 8
Joined: Fri Aug 11, 2017 10:46 am
Contact:

Re: Can't make Python scripts work

Postby headcrash » Fri Aug 18, 2017 4:42 am

You need to give it time to attach before trying to call it. You could just put a sleep(1) after your open, but that would be inefficient, especially if opening a lot of devices. Better to replace your open() with openWaitForAttachment(5000) if going for basic or even better use events if going for most efficient.

Entering your statements manually at the Python prompt works because that gives it enough time to attach.

See "Step Two: Wait for Attachment..." under "Write Code" in Phidgets Python Language doc.

kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Re: Can't make Python scripts work

Postby kiffer » Fri Aug 18, 2017 12:55 pm

Thanks for the info! Makes sense, but I tried it and it didn't work. Same error as above...

Any chance my libraries could be located in the wrong place? But then why would it work from the command line? I'm a noob to Linux too.

kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Re: Can't make Python scripts work

Postby kiffer » Fri Aug 18, 2017 1:04 pm

Added an attach handler and it is printing that the 1051 is successfully attaching. But the code still errors on the ch.getTemperature() method. Hmmmm...

headcrash
Phidgetsian
Posts: 8
Joined: Fri Aug 11, 2017 10:46 am
Contact:

Re: Can't make Python scripts work

Postby headcrash » Fri Aug 18, 2017 2:38 pm

I'm not familiar with the 1051 Temperature sensor, but you might have to set the ThermocoupleType before calling the getTemperature method?

kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Re: Can't make Python scripts work

Postby kiffer » Fri Aug 18, 2017 4:23 pm

Hmm... I tried that but it now errors out on that line: ch.setThermocoupleType(2)

The documentation for the setThermocoupleType method states that K-type (which is what I have) is number 2.

The error I get is identical to the initial error I posted except of course it occurs in a different function (setThermocoupleType as opposed to getTemperature) within the TemperatureSensor.py code. I don't need to set this value when typing directly into the command prompt either.

kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Re: Can't make Python scripts work

Postby kiffer » Thu Oct 05, 2017 7:59 pm

UPDATE: I used the example code from Phidgets and modified it to record the data I was interested in. The example code worked fine, but I still can't figure out why my very basic little program wouldn't work. Strange.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest