-
Notifications
You must be signed in to change notification settings - Fork 24
/
example_LDD_112x.py
116 lines (95 loc) · 3.42 KB
/
example_LDD_112x.py
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
107
108
109
110
111
112
113
114
115
116
"""
"""
import logging
from mecom import MeComSerial, ResponseException, WrongChecksum
from serial import SerialException
# default queries from command table below
DEFAULT_QUERIES = [
"current",
"max current",
]
# syntax
# { display_name: [parameter_id, unit], }
COMMAND_TABLE = {
"Device Status": [104, ""],
"current": [1016, "A"],
"max current": [3020, "A"],
"temperature": [1015, "°C"],
}
class MeerstetterLDD_112x(object):
"""
Controlling LDD-112x devices via serial.
"""
def _tearDown(self):
self.session().stop()
def __init__(self, port="COM6", channel=1, queries=DEFAULT_QUERIES, *args, **kwars):
assert channel is 1
self.channel = channel
self.port = port
self.queries = queries
self._session = None
self._connect()
def _connect(self):
# open session
self._session = MeComSerial(serialport=self.port,metype = 'LDD-112x')
# get device address
self.address = self._session.identify()
logging.info("connected to {}".format(self.address))
def session(self):
if self._session is None:
self._connect()
return self._session
def get_data(self):
data = {}
for description in self.queries:
id, unit = COMMAND_TABLE[description]
try:
value = self.session().get_parameter(parameter_id=id, address=self.address, parameter_instance=self.channel)
data.update({description: (value, unit)})
except (ResponseException, WrongChecksum) as ex:
self.session().stop()
self._session = None
return data
def set_current(self, value):
"""
Set laser diode cw current
:param value: float
:param channel: int
:return:
"""
# assertion to explicitly enter floats
assert type(value) is float
logging.info("set current to {} C".format(self.channel, value))
return self.session().set_parameter(parameter_id=2001, value=value, address=self.address, parameter_instance=self.channel)
def set_current_limit(self, value):
"""
Set laser diode cw current limit
:param value: float
:param channel: int
:return:
"""
# assertion to explicitly enter floats
assert type(value) is float
logging.info("set current limit to {} C".format(self.channel, value))
return self.session().set_parameter(parameter_id=3020, value=value, address=self.address, parameter_instance=self.channel)
def _set_enable(self, enable=True):
"""
Enable or disable control loop
:param enable: bool
:param channel: int
:return:
"""
value, description = (1, "on") if enable else (0, "off")
logging.info("set current output to {} to {}".format(self.channel, description))
return self.session().set_parameter(value=value, parameter_id=2020, address=self.address, parameter_instance=self.channel)
def enable(self):
return self._set_enable(True)
def disable(self):
return self._set_enable(False)
if __name__ == '__main__':
# start logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s:%(module)s:%(levelname)s:%(message)s")
# initialize controller
mc = MeerstetterLDD_112x()
# get the values from DEFAULT_QUERIES
print(mc.get_data())