-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (73 loc) · 2.98 KB
/
Copy pathmain.py
File metadata and controls
97 lines (73 loc) · 2.98 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
# -- Imports ------------------------------------------------------------------
import fwlib # namespace
from types import TracebackType
from typing import Optional, Type
class FocasException(Exception):
pass
class UnableToReadMacro(FocasException):
"""Raised when a macro could not be read"""
class UnableToReadAxis(FocasException):
"""Raised when an axis could not be read"""
class FocasController:
def __init__(self, ip: str, port: int = 8193, sample_rate: int = 10):
self.ip = ip
self.port = port
self.sample_rate = sample_rate
self.library_handle = None
def __enter__(self): # -> Focas:
"""Initialize the connector class"""
self.connect()
return self
def __exit__(
self,
exception_type: Optional[Type[BaseException]],
exception_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
"""Clean up the resources used by the storage class
Parameters
----------
exception_type:
The type of the exception in case of an exception
exception_value:
The value of the exception in case of an exception
traceback:
The traceback in case of an exception
"""
self.disconnect()
def connect(self):
ret, self.libh = fwlib.cnc_allclibhndl3(self.ip, self.port, self.sample_rate)
if ret != 0:
raise ConnectionError(
f"Unable to connect to Focas control (IP: {self.ip}, Port:"
f" {self.port})"
)
self.library_handle = self.libh
assert self.library_handle is not None, "Library handle not initialized"
def disconnect(self):
fwlib.cnc_freelibhndl(self.library_handle)
def read_macro(self, register: int) -> int:
ret, (_, dataFeature) = fwlib.cnc_rdmacror2(self.library_handle, register)
if ret != 0:
raise UnableToReadMacro(f"Unable to read macro “{register}”")
return dataFeature
def read_axis(self, category: int, index: int) -> int:
ret, axisdata = fwlib.cnc_rdaxisdata(self.library_handle, category, [index])
if ret != 0:
raise UnableToReadAxis(f"Unable to read axis data “{category}”, “{index}”")
return axisdata
if __name__ == "__main__":
nc_data = []
focas_controller = FocasController("192.168.1.10")
with focas_controller:
try:
axis_tuples = [[1, 0], [2, 0], [3, 0], [5, 0], [5, 1]]
for tuple in axis_tuples:
axisdata = focas_controller.read_axis(tuple[0], tuple[1])
for axis in axisdata:
nc_data.append(axis[1])
print(nc_data)
except (ConnectionError, UnableToReadMacro, UnableToReadAxis) as error:
print(error)
focas_controller.disconnect()
print(nc_data)