Page 1 of 1

Issues with exiting

Posted: Fri Apr 05, 2019 10:44 pm
by jcrobots
I am playing with two 8/8/8 Phidgets, using the Python API on Ubuntu. On MacOS I have no problem, but on Linux this comes up. My script works right the first time, but after I close it or kill it a few times I lose the ability to open channels.

I believe I am closing all of my ports properly, but it seems something may not be exiting properly.

Is it possible to run a "clean" command when my script starts to avoid this issue if it everything wasn't closed properly on the last exit? The only thing that I'm able to do to fix the problem is to reboot Linux.

My code is below, let me know if I am not exiting properly. I'm a beginner with both Python and Phidgets.

Code: Select all



def run():
    try:
    	# io is a list which will hold all the channels
    	# Only purpose of io is to close them all at the end
        io = []
        LEDs_1 = init_LEDs(DEVICE_2_SERIAL_NUMBER)
        io += LEDs_1
        LEDs_2 = init_LEDs(DEVICE_1_SERIAL_NUMBER)
        io += LEDs_2
        vin_1 = init_vin(DEVICE_1_SERIAL_NUMBER, front=True)
        io += vin_1
        vin_2 = init_vin(DEVICE_2_SERIAL_NUMBER, front=False)
        io += vin_2
        toggle_1 = init_toggle(DEVICE_1_SERIAL_NUMBER)
        io += [toggle_1]
        toggle_2 = init_toggle(DEVICE_2_SERIAL_NUMBER)
        io += [toggle_2]
      
        # Flash LED's based on sensors until keyboard interrupt
        while True:
            for i in LEDS_A:
                LEDs_1[i].setState(bool_a_1)
                LEDs_2[i].setState(bool_a_2)
            for i in LEDS_B:
                LEDs_1[i].setState(bool_b_1)
                LEDs_2[i].setState(bool_b_2)
            time.sleep(1)
        close_ports(io)
        return 0

# A bunch of except blocks are the only way to exit
# All of them are supposed to close all the ports on the way out
# The KeyboardInterrupt functionality doesn't really work
    except PhidgetException as e:
        print("\nExiting with error(s)...")
        DisplayError(e)
        traceback.print_exc(file=open("sensor.log","w"))
        print("Closing sensor channel")
        close_ports(io)
        return 1
    except EndProgramSignal as e:
        print(e)
        print("Closing sensor channel")
        close_ports(io)
        return 1
    except KeyboardInterrupt:
        close_ports(io)
        return 1
    except RuntimeError as e:
        close_ports(io)
        print("Runtime Error: \n\t" + e)
        traceback.print_exc(file=open("sensor.log","w"))
        return 1

# Iterate through a list of channels and close them all
def close_ports(ch_list):  
    for ch in ch_list:
        if ch:  # if the channel isn't "None" 
            ch.close()  # close the channel

run()


Re: Issues with exiting

Posted: Mon Apr 08, 2019 10:07 am
by jcrobots
Edit:

Running a script to reset the USB port fixes the issue. Wondering if there is a more natural solution, for example a way to reset the Phidget through the API, or to prevent the ports from failing to close properly.

Re: Issues with exiting

Posted: Tue Apr 09, 2019 12:03 pm
by jdecoux
You can call:

Code: Select all

Phidget.resetLibrary()
to free up everything in the Phidgets library. This closes all channels, stops all Phidgets-related threads, etc.

Just be sure there are no other Phidgets-related programs you intend to have continue running on your machine, as it will reset the Phidgets library in those as well.

Re: Issues with exiting

Posted: Tue Apr 09, 2019 3:52 pm
by Patrick
The phidgets library should be getting unloaded between python script runs. How are you running the Python script? Just from the command line?

resetLibrary will reset the library, if it is somehow being left loaded by the Python environment between script runs.

Can you enable library logging and post the result during failure case?

-Patrick

Re: Issues with exiting

Posted: Thu Apr 11, 2019 12:53 pm
by Nadav
jdecoux wrote:You can call:

Code: Select all

Phidget.resetLibrary()
Is there a Phidget 22 equivalent?

Re: Issues with exiting

Posted: Thu Apr 11, 2019 2:42 pm
by jdecoux
Yes, that function is for Phidget22.