-
Notifications
You must be signed in to change notification settings - Fork 0
/
GreateyesLabViewCounterTimerController.py
228 lines (189 loc) · 8 KB
/
GreateyesLabViewCounterTimerController.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
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
from sardana import State
from sardana.pool.controller import CounterTimerController, Type, Description, DefaultValue, DataAccess, Access, FGet, FSet
import socket
import struct
import json
import numpy as np
import time
class greatEyes:
def __init__(self, ip, port):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.s.connect((ip, port))
def writeRead(self, cmd):
self.write(cmd)
return(self.read())
def acquireImage(self, intTime):
self.write('SETTIME' + intTime)
self.writeRead('ACQUIRE')
def write(self, cmd):
# write
cmd_bytes = len(cmd)
cmd_bytes_string = struct.pack('>I', cmd_bytes)
self.s.send(cmd_bytes_string)
self.s.sendall(cmd)
def read(self):
# read
ack_bytes_string = self.s.recv(4)
#print(ack_bytes_string)
ack_bytes = struct.unpack('>I', ack_bytes_string[:4])[0]
#print(ack_bytes)
# Look for the response
amount_received = 0
ack = b''
while amount_received < ack_bytes:
data = self.s.recv(4096)
amount_received += len(data)
ack += data
# ack = self.s.recv(ack_bytes)
return(ack)
def __del__(self):
self.s.close()
class GreateyesLabViewCounterTimerController(CounterTimerController):
"""The most basic controller intended from demonstration purposes only.
This is the absolute minimum you have to implement to set a proper counter
controller able to get a counter value, get a counter state and do an
acquisition.
This example is so basic that it is not even directly described in the
documentation"""
ctrl_properties = {'IP': {Type: str,
Description: 'The IP/FQDN of the greateyes labview server',
DefaultValue: 'greateyes.hhg.lab'},
'port': {Type: int,
Description: 'The port of the greateyes labview server',
DefaultValue: 5000},
}
ctrl_attributes = {'PointNb' : { Type : int,
Description : "pointNb of the scan",
Access : DataAccess.ReadWrite,
FGet : 'getPointNb', FSet : 'setPointNb', },
'FileName' : { Type : str,
Description : "file name of image",
Access : DataAccess.ReadWrite,
FGet : 'getFileName', FSet : 'setFileName', },
}
def getPointNb(self):
return self.pointNb
def setPointNb(self, value):
self.pointNb = int(value)
return
def getFileName(self):
return self.fileName
def setFileName(self, value):
self.fileName = value
return
def AddDevice(self, axis):
self._axes[axis] = {}
def DeleteDevice(self, axis):
self._axes.pop(axis)
def __init__(self, inst, props, *args, **kwargs):
"""Constructor"""
super(GreateyesLabViewCounterTimerController,
self).__init__(inst, props, *args, **kwargs)
print ('GreatEyes Initialization ...')
self.ge = greatEyes(self.IP, self.port)
print ('SUCCESS')
self.data = []
self.isAquiring = False
self._axes = {}
self.timeout = 5
self.start_time = 0
self.pointNb = 0
self.fileName = ''
def ReadOne(self, axis):
"""Get the specified counter value"""
if (axis == 0) and (self.isAquiring == False):
self.data = []
peaks = json.loads(self.ge.writeRead(b'GET_PEAKS'))
#print(peaks)
self.data = np.array(peaks, dtype=float).flatten()
#print(self.data)
for spec, ref in zip(self.data[0:10], self.data[10:20]):
if ref == 0: # avoid division by 0
self.data = np.append(self.data, 0)
else:
self.data = np.append(self.data, spec/ref)
#timestamps = json.loads(self.ge.writeRead(b'GET_TIMESTAMPS'))
#URIs = json.loads(self.ge.writeRead(b'GET_URI'))
#print(URIs)
#print(timestamps)
# spec_filename = timestamps[0]
fileID = -999#np.uint64(spec_filename[5:11] + spec_filename[12:18] + spec_filename[19:22])
#print(fileID)
self.data = np.append(self.data, fileID)
return self.data[axis]
def StateOne(self, axis):
"""Get the specified counter state"""
if (time.time() - self.start_time) > self.timeout:
return State.Fault, "Counter in timeout"
if axis == 0:
res = self.ge.writeRead(b'GET_STATUS')
try:
status = int(res.decode("utf-8").split(' ')[1])
if (status == -1) or (status == 1):
self.isAquiring = False
elif status == -2:
#print("ERROR: Camera in running mode!")
self.error("ERROR: Camera in running mode!")
self.warning("ERROR: Camera in running mode!")
return State.Fault, "Camera in running mode!"
else:
self.isAquiring = True
except:
#return State.Fault, "error while parsing status response!"
self.isAquiring = True
if self.isAquiring:
return State.Moving, "Counter is acquiring"
else:
return State.On, "Counter is stopped"
def StartOne(self, axis, value=None):
"""acquire the specified counter"""
return
def StartAll(self):
self.isAquiring = True
# create path
if self.pointNb < 0: #ct
filePath = self.fileName
else:
filePath = '{:s}{:04d}'.format(self.fileName,self.pointNb)
filePath = filePath.replace('/', '\\')
if filePath:
#print(filePath)
res = self.ge.writeRead(b'ACQUIRE ' + filePath)
else:
#print('No filePath given')
res = self.ge.writeRead(b'ACQUIRE')
self.start_time = time.time()
#time.sleep(0.1)
def LoadOne(self, axis, value, repetitions):
pass
def StopOne(self, axis):
"""Stop the specified counter"""
pass
def AbortOne(self, axis):
"""Abort the specified counter"""
pass
def SendToCtrl(self, cmd):
"""
Send custom native commands. The cmd is a space separated string
containing the command information. Parsing this string one gets
the command name and the following are the arguments for the given
command i.e.command_name, [arg1, arg2...]
:param cmd: string
:return: string (MANDATORY to avoid OMNI ORB exception)
"""
mode = cmd.split(' ')[0].lower()
args = cmd.strip().split(' ')[1:]
if mode == "set_basepath":
self.ge.writeRead(b'SET_BASEPATH {:}'.format(args[0]))
elif mode == "set_exposure":
self.ge.writeRead(b'SET_EXPOSURETIME {:} {:}'.format(args[0], int(float(args[1])*1000)))
elif mode == "dark":
self.ge.writeRead(b'DARK {:}'.format(args[0]))
elif mode == "set_save":
self.ge.writeRead(b'SET_SAVE {:} {:}'.format(args[0], int(args[1])))
elif mode == "set_save_dark":
self.ge.writeRead(b'SET_SAVEDARK {:} {:}'.format(args[0], int(args[1])))
elif mode == "get_spectra":
res = self.ge.writeRead(b'GET_SPECTRUM')
print(res)