Python - find out if HAL pin exist

  • zz912
  • zz912's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
17 May 2025 15:33 #328636 by zz912
Python - find out if HAL pin exist was created by zz912
Hello,

I need read HAL pin in Python. It is not problem for me:
        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:
[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
 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 or Create an account to join the conversation.

  • Aciera
  • Aciera's Avatar
  • Away
  • Administrator
  • Administrator
More
17 May 2025 16:13 - 17 May 2025 16:14 #328638 by Aciera
Replied by Aciera on topic Python - find out if HAL pin exist
Try this:
try:
    value = hal.get_value("halui.halui-mdi-is-running")
    exists = True
except Exception:
    exists = False
print(exists)

 
Last edit: 17 May 2025 16:14 by Aciera.
The following user(s) said Thank You: zz912

Please Log in or Create an account to join the conversation.

  • zz912
  • zz912's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
17 May 2025 17:22 #328641 by zz912
Replied by zz912 on topic Python - find out if HAL pin exist
It works. Thank you.

Please Log in or Create an account to join the conversation.

Time to create page: 0.063 seconds
Powered by Kunena Forum