Can't make Python scripts work

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

Can't make Python scripts work

Post 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?
headcrash
Phidgetsian
Posts: 8
Joined: Fri Aug 11, 2017 10:46 am
Contact:

Re: Can't make Python scripts work

Post 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.
kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Re: Can't make Python scripts work

Post 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.
kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Re: Can't make Python scripts work

Post 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...
headcrash
Phidgetsian
Posts: 8
Joined: Fri Aug 11, 2017 10:46 am
Contact:

Re: Can't make Python scripts work

Post by headcrash »

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

Post 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.
kiffer
Phidgetsian
Posts: 5
Joined: Thu Aug 17, 2017 10:16 am
Contact:

Re: Can't make Python scripts work

Post 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.
jesser_redfish
Fresh meat
Posts: 1
Joined: Fri Oct 02, 2020 12:24 pm
Contact:

Re: Can't make Python scripts work

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest