-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_read_ios.py
More file actions
106 lines (86 loc) 路 3.49 KB
/
Copy pathexample_read_ios.py
File metadata and controls
106 lines (86 loc) 路 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Example: Read Physical IOs - Staubli Robot
This script demonstrates how to read physical I/O signals:
1. Connect to a Staubli controller via SOAP
2. Retrieve and display all available physical IOs
3. Prompt the user to select an IO to read
4. Display the IO value and state
5. Loop to allow reading multiple IOs
Requirements:
- A Staubli controller (or emulator) reachable at the specified IP address
- The UnderAutomation.Staubli Python package installed
Usage:
python example_read_ios.py
"""
from underautomation.staubli.connection_parameters import ConnectionParameters
from underautomation.staubli.staubli_controller import StaubliController
# ---------------------------------------------------------------------------
# 1. Create a controller instance and connect
# ---------------------------------------------------------------------------
controller = StaubliController()
# If you get a InvalidLicenseException while connecting, get a trial license key from https://underautomation.com/license
#StaubliController.register_license("licensee", "license_key")
params = ConnectionParameters("127.0.0.1")
controller.connect(params)
print("Connected to the controller.\n")
# ---------------------------------------------------------------------------
# 2. Retrieve all physical IOs
# ---------------------------------------------------------------------------
print("=" * 70)
print("ALL PHYSICAL IOs")
print("=" * 70)
all_ios = controller.soap.get_all_physical_ios()
print(f"\nFound {len(all_ios)} physical IO(s):\n")
# Display IOs in a table
print(f" {'#':<4} {'Name':<30} {'Type':<10} {'Description'}")
print(f" {'-'*4} {'-'*30} {'-'*10} {'-'*30}")
io_names = []
for i, io in enumerate(all_ios):
io_names.append(io.name)
desc = io.description if io.description else ""
type_str = io.type_str if io.type_str else ""
print(f" {i:<4} {io.name:<30} {type_str:<10} {desc}")
print()
# ---------------------------------------------------------------------------
# 3. Interactive loop: prompt user to read specific IO
# ---------------------------------------------------------------------------
print("=" * 70)
print("READ IO VALUE")
print("=" * 70)
print("\nEnter the IO name (or number) to read its value.")
print("Type 'quit' or 'q' to exit.\n")
while True:
user_input = input("IO to read: ").strip()
# Check for exit
if user_input.lower() in ("quit", "q", "exit"):
print("Exiting...")
break
# Determine the IO name
io_name = None
# Check if user entered a number (index)
if user_input.isdigit():
idx = int(user_input)
if 0 <= idx < len(io_names):
io_name = io_names[idx]
else:
print(f" Invalid index. Please enter a number between 0 and {len(io_names) - 1}.")
continue
else:
# User entered a name directly
io_name = user_input
# Read the IO value
try:
io_states = controller.soap.read_ios([io_name])
if io_states and len(io_states) > 0:
state = io_states[0]
print(f"\n IO Name : {io_name}")
print(f" Value : {state.value}")
print(f" State : {state.state.name}")
print(f" Locked : {state.locked}")
print(f" Simulated : {state.simulated}")
print()
else:
print(f" No state returned for IO '{io_name}'.\n")
except Exception as e:
print(f" Error reading IO '{io_name}': {e}\n")
print("\nDone.")