Page 1 of 1

Can't make Python scripts work

Posted: Thu Aug 17, 2017 4:57 pm
by kiffer
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?

Re: Can't make Python scripts work

Posted: Fri Aug 18, 2017 4:42 am
by headcrash
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.

Re: Can't make Python scripts work

Posted: Fri Aug 18, 2017 12:55 pm
by kiffer
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.

Re: Can't make Python scripts work

Posted: Fri Aug 18, 2017 1:04 pm
by kiffer
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...

Re: Can't make Python scripts work

Posted: Fri Aug 18, 2017 2:38 pm
by headcrash
I'm not familiar with the 1051 Temperature sensor, but you might have to set the ThermocoupleType before calling the getTemperature method?

Re: Can't make Python scripts work

Posted: Fri Aug 18, 2017 4:23 pm
by kiffer
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.

Re: Can't make Python scripts work

Posted: Thu Oct 05, 2017 7:59 pm
by kiffer
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.

Re: Can't make Python scripts work

Posted: Wed Oct 07, 2020 12:17 pm
by jesser_redfish
I had same issue recently . I could run myu python scrip via terminal but it refused to run in auto start or via start. Refresh would always show it not running. I was using the code generated by Phidget sample code. This sample code I altered to open another python script inside the Event Handler. It worked fine in terminal.

It wasnt till after I added a print call in the Main() after "#Do stuff with your Phidgets here or in your event handlers." that it started to work. I'm not sure why that is needed if it runs fine in terminal.