Files

57 lines
1.8 KiB
Python
Raw Permalink Normal View History

"""Run a whole HDevelop program (.hdev) via HDevEngine and read control vars back.
Use when the logic lives in a program's main rather than a parameterized
procedure. Outputs print as a JSON object on stdout.
Example:
/mnt/c/Users/NAURA/.local/bin/uv.exe run python \
.claude/skills/halcon/scripts/run_program.py \
--program C:/workspace/agent-studio/halcon-001/demo_prog.hdev \
--get Total --get Area --get Message
Notes:
- The program must be self-contained and non-interactive (no draw_*, no stop()).
- Use ABSOLUTE paths inside the program (no cwd notion under HDevEngine).
"""
import argparse
import json
import sys
from _common import jsonable
def main() -> int:
ap = argparse.ArgumentParser(description="Run a HALCON .hdev via HDevEngine")
ap.add_argument("--program", required=True, help="Absolute path to the .hdev")
ap.add_argument("--get", dest="gets", action="append", default=[],
metavar="Name", help="Control variable to read (repeatable)")
args = ap.parse_args()
try:
from halcon.hdevengine import HDevProgram, HDevProgramCall, HDevEngineError
except Exception as e:
print(f"ERROR: cannot import HDevEngine ({e}). Run via the project venv "
f"using uv.", file=sys.stderr)
return 1
program = HDevProgram(args.program)
call = HDevProgramCall(program)
try:
call.execute()
except HDevEngineError as e:
print(json.dumps({"ok": False, "error": str(e)}, ensure_ascii=False))
return 3
out = {}
for name in args.gets:
try:
out[name] = jsonable(call.get_control_var_by_name(name))
except HDevEngineError as e:
out[name] = {"error": str(e)}
print(json.dumps({"ok": True, "vars": out}, ensure_ascii=False))
return 0
if __name__ == "__main__":
sys.exit(main())