Python - find out if HAL pin exist

More
1 year 5 months ago #328636 by zz912
Hello,

I need read HAL pin in Python. It is not problem for me:
Code:
        value = hal.get_value("halui.halui-mdi-is-running")         print(value)

If the "HAL pin" exists, there is no problem:
The problem occurs when the "HAL pin" does not exist:
Code:
[Gmoccapy][ERROR]  Found an error! The following information may be useful in troubleshooting: Traceback (most recent call last):   File "/home/zdenek/linuxcnc/linuxcnc-zz912/bin/gmoccapy", line 4220, in on_tbtn_flood_toggled     value = hal.get_value("halui.halui-mdi-is-running")             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Can't set value: pin / param halui.halui-mdi-is-running not found  (gmoccapy:62)

I would first need to find out if this "HAL pin" exists and then find out its value.
But I dont know How.

1)
Python HAL interface can check only component, not only "HAL pin".
www.linuxcnc.org/docs/html/config/python-hal-interface.html
Code:
 component_exists     Does the specified component exist at this time. Example hal.component_exists("testpanel")

2)
Maybe I could use Python's "Try:"
But I don't know how. I would need to check in initialization if the pin exists and store it in a variable:
halui_cmd_exist = True
or
halui_cmd_exist = False

Please Log in to join the conversation.

More
1 year 5 months ago #328638 by Aciera
Try this:
Code:
try: value = hal.get_value("halui.halui-mdi-is-running") exists = True except Exception: exists = False print(exists)

 

Please Log in to join the conversation.

More
1 year 5 months ago #328641 by zz912
It works. Thank you.

Please Log in to join the conversation.

Time to create page: 0.077 seconds
Powered by Kunena Forum