-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewport.py
More file actions
350 lines (307 loc) · 10 KB
/
newport.py
File metadata and controls
350 lines (307 loc) · 10 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 17 17:33:35 2022
@author: user
"""
# https://github.com/marvync/Laser-automation-NewFocus : basic sample
# https://github.com/gregmoille/InstrumentControl/blob/master/pyLaser/newfocus.py -> to JeongHo lee, this has more functionality. - Kang. please implement these functions.
# Newport Tunable Laser를 제어하기 위한 Python 클래스
# .NET 기반의 DLL(UsbDllWrap.dll)을 이용해 하드웨어 제어 수행
# Newport의 USB 인터페이스를 통해 파장, 출력, 스캔 속도 등을 설정하거나 질의
import os
import sys
# import ipdb
import numpy as np
import time
import clr
from datetime import datetime
import pyvisa
# import matplotlib.pyplot as plt
from time import sleep
import logging
# from clr import System
from System.Text import StringBuilder
from System import Int32
from System.Reflection import Assembly
# from wlm import WavelengthMeter
sys.path.append(
"C:\\Program Files\\New Focus\\New Focus Tunable Laser Application\\Bin"
)
clr.AddReference("UsbDllWrap")
# import Newport
ProductID = 4106
DeviceKey = "6700 SN22500008"
# Newport 장치와의 실제 통신 핸들
class newport:
"""def __init__(self ,ProductID, DeviceKey):
self.ProductID = ProductID
self._DeviceKey = DeviceKey
self.tlb = Newport.USBComm.USB()
self.answer = StringBuilder(64)
self.tlb.OpenDevices(self.ProductID, True)
wlmeter = WavelengthMeter()"""
def __init__(self, id = int, key = str, debug = False):
super(newport, self).__init__()
self.debug = debug # 디버그 모드
try:
self._dev = Newport.USBComm.USB() # Neport 장치와의 실제 통신 핸들
except Exception as err:
print(err)
self._dev = None
self.debug = True
if not self.debug:
from System.Text import StringBuilder
self.answer = StringBuilder(64)
self._buff = StringBuilder(64)
else:
self.answer = ""
self._buff = ""
# Laser state
self._open = False
self._DeviceKey = key # 장치 고유 식별자
self._idLaser = id # 장치 고유 식별자
# self.answer = StringBuilder(64)
self.Device_config = "newport"
# Laser properties
self._lbd = "0" # 레이저 제어 관련 내부 변수
self._cc = 0
self._scan_lim = []
self._scan_speed = 0
self._scan = 0
self._beep = 0
self._output = 0 # 레이저 제어 관련 내부 변수
self._is_scaning = False
# self._is_changing_lbd = False
self._no_error = '0,"NO ERROR"'
self._haserr = False
# Miscs
# self._buff = StringBuilder(64)
self._err_msg = ""
self.connected = True
# -- Decorators --
# ---------------------------------------------------------
def Checkopen(fun):
def wrapper(*args, **kwargs):
self = args[0]
# if self._open and self._DeviceKey:
if self._open and self._DeviceKey:
out = fun(*args, **kwargs)
return out
else:
pass
return wrapper
# -- Methods --
# --------------------------------------------------------
def get_device_info(self):
return self.Device_config
def set_piezo_voltage(self, pzt_value):
self.pzt = pzt_value
def get_piezo_voltage(self):
return self.pzt
def set_wavelength(self, lbd_value):
self.lbd_value = lbd_value
def Query(self, word):
if self.debug or self._dev is None:
# 디버그 모드용 가짜 응답
fake_responses = {
"SENSe:WAVElength?": "1550.123",
"OUTPut:STATe?": "1",
"SOUR:VOLT:PIEZ?": "50.0",
"*IDN?": "DEBUG-NEWPORT",
"*STB?": "0",
"ERRSTR?": "0,\"NO ERROR\""
}
response = fake_responses.get(word.strip(), "MOCK_RESPONSE")
print(f"[DEBUG][Query] {word.strip()} -> {response}")
return response
else:
self._buff.Clear()
self._dev.Query(self._DeviceKey, word, self._buff)
# print(self._DeviceKey, word , self._buff)
return self._buff.ToString()
# -- Properties --
# ---------------------------------------------------------
@property
# @InOut.output(bool)
def connected(self):
return self._open
@connected.setter
# @Catch.error
def connected(self, value): # 자동 연결(장치 연결을 시도하고, 버퍼를 비우며 초기 상태 확인 후 연결 완료 여부 출력)
if value:
if self.debug:
print(f"[DEBUG] 디바이스 없이 연결된 것으로 간주합니다.")
self._open = True
return # 💥 실제 연결 로직 실행하지 않음
if self._DeviceKey:
# try:
out = self._dev.OpenDevices(self._idLaser, True)
tab = self._dev.GetDeviceTable()
# empty buffer
out = self._dev.Read(self._DeviceKey, self._buff)
# ipdb.set_trace()
while not (out == -1 or out == -2 or out == int("-2")):
out = self._dev.Read(self._DeviceKey, self._buff)
print("Empyting the buffer: {}".format(out))
time.sleep(0.5)
idn = self.identity
if not idn == "":
print("\nLaser connected: {}".format(idn))
else:
logging.warning("The newport doesn't connected")
self._dev.CloseDevices()
time.sleep(0.2)
# ipdb.set_trace()
self.error
self._open = True
# except Exception as e:
# print(e)
else:
self._dev.CloseDevices()
self._open = False
@property
# @InOut.output(bool)
def output(self):
word = "OUTPut:STATe?"
self._output = self.Query(word)
return self._output
@output.setter
# @Catch.error
# @InOut.accepts(bool)
def output(self, value):
word = "OUTPut:STATe {}".format(int(value))
self.Query(word)
self._output = value
@property
# @InOut.output(float)
def lbd(self):
word = "SENSe:WAVElength?"
self._lbd = self.Query(word)
if self.debug:
print("[DEBUG] read wavelength")
else:
print("read wavelength")
# print(self._lbd)
return self._lbd
@property
# @InOut.output(float)
def pzt(self):
word = "SOUR:VOLT:PIEZ?"
self._pzt = self.Query(word)
return self._pzt
@pzt.setter
# @Catch.error
# @InOut.accepts(float)
def pzt(self, value):
word = "SOUR:VOLT:PIEZ {}".format(value)
self.Query(word)
self._pzt = value
@lbd.setter
# @InOut.accepts(float)
# @Catch.error
def lbd(self, value):
self._targetlbd = value
self.Query("OUTP:TRACK 1")
word = "SOURCE:WAVE {}".format(value)
self.Query(word)
if self.debug:
print(f"[DEBUG] write wavelength {value}")
else:
print(f"write wavelength {value}")
self._lbd = value
@property
# @InOut.output(float,float)
def scan_limit(self):
word1 = "SOUR:WAVE:START?"
word2 = "SOUR:WAVE:STOP?"
self._scan_lim = [self.Query(word1), self.Query(word2)]
return self._scan_lim
@scan_limit.setter
# @Catch.error
# @InOut.accepts(list)
def scan_limit(self, value):
start = value[0]
stop = value[1]
word1 = "SOUR:WAVE:START {}".format(start)
self.Query(word1)
word2 = "SOUR:WAVE:STOP {}".format(stop)
self.Query(word2)
self._scan_lim = value
@property
# @Catch.error
# @InOut.output(float)
def scan_speed(self):
word1 = "SOUR:WAVE:SLEW:FORW?"
self._scan_speed = self.Query(word1)
return self._scan_speed
@scan_speed.setter
# @Catch.error
# @InOut.accepts(float)
def scan_speed(self, value):
word = "SOUR:WAVE:SLEW:FORW {}".format(value)
self.Query(word)
word = "SOUR:WAVE:SLEW:RET {}".format(0.1)
self.Query(word)
self._scan_speed = value
@property
# @InOut.output(float)
def scan(self):
word = "SOUR:WAVE:DESSCANS?"
self._scan = self.Query(word)
return self._scan
@scan.setter
# @Catch.error
# @ChangeState.scan("OUTPut:SCAN:START",'OUTPut:SCAN:STOP')
# @InOut.accepts(bool)
def scan(self, value):
self.Query("SOUR:WAVE:DESSCANS 1")
self._scan = value
if self._scan:
self.Query("OUTPut:SCAN:START")
else:
self.Query("OUTPut:SCAN:STOP")
@property
# @InOut.output(bool)
def beep(self):
word = "BEEP?"
self._beep = self.Query(word)
return self.beep
@beep.setter
# @Catch.error
# @InOut.accepts(bool)
def beep(self, value):
word = "BEEP ".format(int(value))
self.Query(word)
self._beep = value
@property
def identity(self):
word = "*IDN?"
self._id = self.Query(word)
return self._id
@property
def error(self):
word = "ERRSTR?"
self._error = ""
err = self.Query(word)
return err
@property
def has_error(self):
word = "*STB?"
dum = self.Query(word)
if dum == "128":
self._haserr = True
if dum == "0":
self._haserr = False
return self._haserr
@property
# @InOut.output(bool)
def _is_changing_lbd(self):
return self.Query("OUTP:TRACK?")
@property
def clear(self):
pass
@clear.setter
# @InOut.accepts(bool)
def clear(self, val):
if val:
self.Query("*CLS")