-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.py
More file actions
2856 lines (2466 loc) · 101 KB
/
client.py
File metadata and controls
2856 lines (2466 loc) · 101 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File containing the Client for connecting to the DE-Server
#
# Last update: 2024-08-07
# cfrancis@directelectron.com
# Python System imports
import socket
import sys
import struct
import time
import os
import logging
import mmap
from datetime import datetime
from time import sleep
import re
from typing import List, Union, Tuple
import numpy as np
# External package imports
from PIL import Image
import numpy
# Internal package imports
from deapi.data_types import (
FrameType,
PixelFormat,
Attributes,
Histogram,
PropertySpec,
MovieBufferStatus,
MovieBufferInfo,
DataType,
PropertyCollection,
VirtualMask,
Result,
)
from deapi.buffer_protocols import pb
from deapi.version import version, commandVersion
from deapi.version import commandVersion as cVersion
from deapi.wrappers import write_only, disable_scan, deprecated_argument
## the commandInfo contains [VERSION_MAJOR.VERSION_MINOR.VERSION_PATCH.VERSION_REVISION]
logLevel = logging.INFO
logging.basicConfig(format="%(asctime)s DE %(levelname)-8s %(message)s", level=logLevel)
log = logging.getLogger("DECameraClientLib")
log.info("Python : " + sys.version.split("(")[0])
log.info("DEClient : " + version)
log.info("CommandVer: " + str(commandVersion))
log.info("logLevel : " + str(logging.getLevelName(logLevel)))
class Client:
"""A class for connecting to the DE-Server
Examples
--------
>>> client = Client()
>>> client.connect()
>>> client["Exposure Time (seconds)"]
"""
def __init__(self):
self.commandVersion = commandVersion
self.read_only = False
def set_log_level(self, level):
log = logging.getLogger("DECameraClientLib")
log.setLevel(level)
log.info("Log level set to %s", level)
return
def __str__(self):
return f"Client(host={self.host}, port={self.port}, camera={self.get_current_camera()})"
def _ipython_key_completions_(self):
return self.list_properties()
def _repr_html_(self):
table = f"""
<table>
<tr>
<th>Host</th>
<th>Port</th>
<th>Current Camera</th>
</tr>
<tr>
<td>{self.host}</td>
<td>{self.port}</td>
<td>{self.currCamera}</td>
</tr>
</table>
<details>
<summary>Current Info</summary>
<pre>
{self.get_current_info()}
</pre>
</details>
"""
return table
def gui(self):
from IPython.display import display
display(self)
def __setitem__(self, key, value):
self.set_property(key, value)
def __getitem__(self, key):
return self.get_property(key)
def get_current_info(self):
prop_list = self.list_properties()
values = self.get_properties(prop_list)
text = ""
for p, v in zip(prop_list, values):
text += f"{p}: {v} \n"
return text
def _initialize_attributes(self):
all_properties = self.list_properties()
collections = [p.split(" - ")[0] for p in all_properties if " - " in p]
unique_collections = np.unique(collections)
for collection in unique_collections:
stripped = collection.lower().strip().replace(" ", "_")
props = [p for p in all_properties if collection + " -" in p]
setattr(
self,
stripped,
PropertyCollection(client=self, name=collection, properties=props),
)
def connect(self, host: str = "127.0.0.1", port: int = 13240, read_only=False):
"""Connect to DE-Server
Parameters
----------
host : str, optional
The host to connect to, by default "127.0.0.1" for local connection
port : int, optional
The port to connect to, by default 13240
read_only : bool, optional
If True, the client will be in read-only mode, by default False
"""
self.read_only = read_only
if not read_only and (host == "localhost" or host == "127.0.0.1"):
tcp_no_delay = 0 # on loopback interface, nodelay causes delay
if self.usingMmf:
self.mmf = mmap.mmap(0, MMF_DATA_BUFFER_SIZE, "ImageFileMappingObject")
self.mmf[0] = True
else:
self.usingMmf = False # Disabled MMF if connected remotely
tcp_no_delay = 1
if logLevel == logging.DEBUG:
log.debug("Connecting to server: %s", host)
self.socket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM
) # Create a socket (SOCK_STREAM means a TCP socket)
self.socket.connect(
(host, port)
) # Connect to server reading port for sending data
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, tcp_no_delay)
self.socket.setblocking(False)
self.socket.settimeout(2)
self.cameras = self.__getStrings(self.LIST_CAMERAS)
if logLevel == logging.DEBUG:
log.debug("Available cameras: %s", self.cameras)
self.currCamera = self.cameras[0]
if logLevel == logging.DEBUG:
log.debug("Current camera: %s", self.currCamera)
self.connected = True
self.host = host
self.port = port
log.info("Connected to server: %s, port: %d", host, port)
if cVersion >= 12:
self.set_client_read_only(read_only)
server_version = self.GetProperty("Server Software Version")
server_version = re.findall(r"\d+", server_version)
version = [int(part) for part in server_version[:4]]
temp = version[2] + version[1] * 1000 + version[0] * 1000000
if (temp >= 2007005 and version[3] < 11274) or temp >= 2008000:
## version after 2.8.0
self.commandVersion = 15
elif temp >= 2007004:
## version after 2.7.4
if version[3] < 10590:
self.commandVersion = 12
else:
self.commandVersion = 13
elif temp >= 2007003:
## version after 2.7.3
self.commandVersion = 11
elif temp >= 2007002:
## version after 2.7.2
self.commandVersion = 10
elif temp >= 2005025:
##version after 2.5.25
self.commandVersion = 4
elif temp >= 2001017:
## version after 2.1.17
self.commandVersion = 3
else:
self.commandVersion = commandVersion
print("Command Version: ", self.commandVersion)
self._initialize_attributes()
self.update_scan_size()
self.update_image_size()
self.virtual_masks = []
for i in range(4):
self.virtual_masks.append(VirtualMask(client=self, index=i))
def set_client_read_only(self, read_only):
self.read_only = read_only
command = self._addSingleCommand(self.SET_CLIENT_READ_ONLY, None, [read_only])
response = self._sendCommand(command)
return response
def update_scan_size(self):
self.scan_sizex = self["Scan - Size X"]
self.scan_sizey = self["Scan - Size Y"]
def update_image_size(self):
self.image_sizex = self["Image Size X (pixels)"]
self.image_sizey = self["Image Size Y (pixels)"]
def disconnect(self):
"""
Disconnects from the server.
Closes the memory-mapped file (mmf) if it is open.
Closes the socket connection if it is open.
Sets the 'connected' flag to False.
"""
if self.mmf != 0:
self.mmf.close()
if self.connected:
self.socket.close()
self.socket.close()
self.connected = False
log.info("Disconnected.")
def list_cameras(self) -> List[str]:
"""
List the available cameras on the server.
"""
return self.cameras
def get_virtual_mask(self, index):
mask_name = f"virtual_mask{index}"
a = Attributes()
a.windowWidth = self["Image Size X (pixels)"]
a.windowHeight = self["Image Size Y (pixels)"]
(
res,
_,
_,
_,
) = self.get_result(mask_name, DataType.DE8u, attributes=a)
return res
def get_current_camera(self) -> str:
"""
Get the current camera on the server.
"""
if self.currCamera is None:
return "No current camera"
else:
return self.currCamera
@write_only
def set_current_camera(self, camera_name: str = None):
"""
Set the current camera on the server.
"""
if camera_name is None:
return False
self.currCamera = camera_name
if logLevel == logging.DEBUG:
log.debug("current camera: %s", camera_name)
self.refreshProperties = True
return True
def list_properties(self, options=None, search=None):
"""
Get a list of property names from the current camera on DE-Server
Parameters
----------
options : list, optional
Options to pass to the server, by default None
"""
available_properties = self.__getStrings(self.LIST_PROPERTIES, options)
if available_properties != False:
self.available_properties = available_properties
if logLevel == logging.DEBUG:
log.debug("Available camera properties: %s", available_properties)
if search is not None:
available_properties = [p for p in available_properties if search in p]
return available_properties
def list_registers(self, options=None, search=None):
"""
Get a list of register names from the current camera on DE-Server
Parameters
----------
options : list, optional
Options to pass to the server, by default None
"""
available_registers = self.__getStrings(self.LIST_REGISTERS, options)
if available_registers != False:
self.available_registers = available_registers
if logLevel == logging.DEBUG:
log.debug("Available camera registers: %s", available_registers)
if search is not None:
available_registers = [p for p in available_registers if search in p]
return available_registers
@deprecated_argument(
name="propertyName", since="5.2.0", alternative="property_name"
)
def get_property_spec(self, property_name: str):
"""
Get a list of allowed values for a property of the current camera on DE-Server
Deprecated since DE-MC 2.7.4
Parameters
----------
property_name : str
The name of the property to get the allowed values for
"""
command = self._addSingleCommand(
self.GET_ALLOWABLE_VALUES_DEPRECATED, property_name
)
response = self._sendCommand(command)
if not response:
return None
values = self.__getParameters(response.acknowledge[0])
prop_spec = PropertySpec()
prop_spec.dataType = values[0]
prop_spec.valueType = values[1]
prop_spec.category = values[-3]
prop_spec.options = list(values[2:-3])
prop_spec.defaultValue = str(values[-2])
prop_spec.currentValue = str(values[-1])
optionsLength = len(prop_spec.options)
if prop_spec.valueType == "Range":
if optionsLength == 2:
rangeString = ""
for i in range(optionsLength):
if prop_spec.dataType == "Integer":
rangeString += str(int(prop_spec.options[i]))
else:
rangeString += str(prop_spec.options[i])
if i == 0:
rangeString += str(" - ")
prop_spec.options.append(rangeString)
empty_string_index = None
if prop_spec.valueType == "Set":
for i in range(optionsLength):
if prop_spec.defaultValue == prop_spec.options[i]:
if prop_spec.defaultValue != "":
prop_spec.options[i] = prop_spec.defaultValue + str("*")
else:
empty_string_index = i
if prop_spec.defaultValue == "" and empty_string_index is not None:
prop_spec.options.pop(empty_string_index)
if "allow_all" in prop_spec.valueType:
prop_spec.options = ""
elif prop_spec.dataType == "String":
prop_spec.options = str(list(map(lambda a: str(a), prop_spec.options)))[
1:-1
]
else:
prop_spec.options = str(prop_spec.options)[1:-1]
return prop_spec
@deprecated_argument(
name="propertyName", since="5.2.0", alternative="property_name"
)
def get_property_specifications(self, property_name):
"""
Get a list of allowed values for a property of the current camera on DE-Server
Only works for DE-MC version greater or equal to 2.7.4
Parameters
----------
property_name : str
The name of the property to get the allowed values for
"""
t0 = self.GetTime()
values = False
command = self.__addSingleCommand(
self.GET_PROPERTY_SPECIFICATIONS, property_name
)
response = self.__sendCommand(command)
if response == False:
return None
values = self.__getParameters(response.acknowledge[0])
propSpec = PropertySpec()
propSpec.dataType = values[0]
propSpec.valueType = values[1]
propSpec.category = values[-4]
propSpec.options = list(values[2:-4])
propSpec.defaultValue = str(values[-3])
propSpec.currentValue = str(values[-2])
propSpec.readOnly = bool(values[-1])
optionsLength = len(propSpec.options)
if propSpec.valueType == "Range":
if optionsLength == 2:
rangeString = ""
for i in range(optionsLength):
if propSpec.dataType == "Integer":
rangeString += str(int(propSpec.options[i]))
else:
rangeString += str(propSpec.options[i])
if i == 0:
rangeString += str(" - ")
propSpec.options.append(rangeString)
if propSpec.valueType == "Set":
for i in range(optionsLength):
if propSpec.defaultValue == propSpec.options[i]:
if propSpec.defaultValue != "":
propSpec.options[i] = propSpec.defaultValue + str("*")
else:
emptyStringIndex = i
if propSpec.defaultValue == "":
propSpec.options.pop(emptyStringIndex)
if "allow_all" in propSpec.valueType:
propSpec.options = ""
elif propSpec.dataType == "String":
propSpec.options = str(list(map(lambda a: str(a), propSpec.options)))[1:-1]
else:
propSpec.options = str(propSpec.options)[1:-1]
return propSpec
@deprecated_argument(
name="propertyName", since="5.2.0", alternative="property_name"
)
def get_property(self, property_name: str):
"""
Get the value of a property of the current camera on DE-Server
Parameters
----------
property_name : str
The name of the property to get the value of
"""
t0 = self.GetTime()
ret = False
if property_name is not None:
command = self._addSingleCommand(self.GET_PROPERTY, property_name)
response = self._sendCommand(command)
if response != False:
values = self.__getParameters(response.acknowledge[0])
if type(values) is list:
if len(values) > 0:
ret = values[0] # always return the first value
else:
ret = values
if logLevel == logging.DEBUG:
log.debug(
"GetProperty: %s = %s, completed in %.1f ms",
property_name,
values,
(self.GetTime() - t0) * 1000,
)
return ret
def get_register(self, register_name: str):
"""
Get the value of a register of the current camera on DE-Server
Parameters
----------
register_name : str
The name of the register to get the value of
"""
t0 = self.GetTime()
ret = False
if register_name is not None:
command = self._addSingleCommand(self.GET_REGISTER, register_name)
response = self._sendCommand(command)
if response != False:
values = self.__getParameters(response.acknowledge[0])
if type(values) is list:
if len(values) > 0:
ret = values[0] # always return the first value
else:
ret = values
if logLevel == logging.DEBUG:
log.debug(
"GetRegister: %s = %s, completed in %.1f ms",
register_name,
values,
(self.GetTime() - t0) * 1000,
)
return ret
def get_server_version(self):
"""
Get the server software version
"""
server_version = self.GetProperty("Server Software Version")
server_version = re.findall(r"\d+", server_version)
ver = [int(part) for part in server_version[:4]]
res = ver[2] + ver[1] * 1000 + ver[0] * 1000000
return res
def get_properties(self, names=None):
if names is None:
names = self.list_properties()
return [self.get_property(p) for p in names]
@property
def acquiring(self):
"""Check if the camera is currently acquiring images. (bool)"""
return self["Acquisition Status"] == "Acquiring"
@write_only
def set_property(self, name: str, value):
"""
Set the value of a property of the current camera on DE-Server
Parameters
----------
name : str
The name of the property to set the value of
value : any
The value to set the property to
"""
t0 = self.GetTime()
ret = False
if name is not None and value is not None:
command = self._addSingleCommand(self.SET_PROPERTY, name, [value])
response = self._sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if logLevel == logging.DEBUG:
log.debug(
"SetProperty: %s = %s, completed in %.1f ms",
name,
value,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
@deprecated_argument(
name="changedProperties", since="5.2.0", alternative="changed_properties"
)
def set_property_and_get_changed_properties(self, name, value, changed_properties):
"""
Set the value of a property of the current camera on DE-Server and get all
the changed properties. This is useful for testing and determining how certain
properties affect others.
Parameters
----------
name : str
The name of the property to set the value of
value : any
The value to set the property to
changed_properties : list
List of properties that have changed
"""
t0 = self.GetTime()
ret = False
if name is not None and value is not None:
command = self._addSingleCommand(
self.SET_PROPERTY_AND_GET_CHANGED_PROPERTIES, name, [value]
)
response = self._sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if ret:
ret = self.ParseChangedProperties(changed_properties, response)
if logLevel == logging.DEBUG:
log.debug(
"SetProperty: %s = %s, completed in %.1f ms",
name,
value,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
def set_register(self, name: str, value):
"""
Set the value of a register of the current camera on DE-Server
Parameters
----------
name : str
The name of the register to set the value of
value : any
The value to set the register to
"""
t0 = self.GetTime()
ret = False
if name is not None and value is not None:
command = self._addSingleCommand(self.SET_REGISTER, name, [value])
response = self._sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if logLevel == logging.DEBUG:
log.debug(
"SetRegister: %s = %s, completed in %.1f ms",
name,
value,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
def set_engineering_mode(self, enable, password):
"""
Set the engineering mode of the current camera on DE-Server. Mostly for internal testing.
Parameters
----------
enable : bool
Enable or disable engineering mode
password : str
The password to enable engineering mode
"""
ret = False
command = self._addSingleCommand(self.SET_ENG_MODE, None, [enable, password])
response = self._sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
return ret
@write_only
def setEngModeAndGetChangedProperties(self, enable, password, changedProperties):
ret = False
command = self.__addSingleCommand(
self.SET_ENG_MODE_GET_CHANGED_PROPERTIES, None, [enable, password]
)
response = self.__sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if ret:
ret = self.ParseChangedProperties(changedProperties, response)
return ret
@write_only
@deprecated_argument(name="offsetX", since="5.2.0", alternative="offset_x")
@deprecated_argument(name="offsetY", since="5.2.0", alternative="offset_y")
@deprecated_argument(name="sizeX", since="5.2.0", alternative="size_x")
@deprecated_argument(name="sizeY", since="5.2.0", alternative="size_y")
def set_hw_roi(self, offset_x: int, offset_y: int, size_x: int, size_y: int):
"""
Set the hardware region of interest (ROI) of the current camera on DE-Server.
Parameters
----------
offset_x : int
The x offset of the ROI
offset_y : int
The y offset of the ROI
size_x : int
The width of the ROI
size_y : int
The height of the ROI
"""
t0 = self.GetTime()
ret = False
command = self._addSingleCommand(
self.SET_HW_ROI, None, [offset_x, offset_y, size_x, size_y]
)
response = self._sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if logLevel == logging.DEBUG:
log.debug(
"SetHwRoi: (%i,%i,%i,%i) , completed in %.1f ms",
offset_x,
offset_y,
size_x,
size_y,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
@deprecated_argument(name="sizeX", since="5.2.0", alternative="size_x")
@deprecated_argument(name="sizeY", since="5.2.0", alternative="size_y")
def SetScanSize(self, size_x, size_y):
"""
Set the scan size of the current camera on DE-Server.
"""
t0 = self.GetTime()
ret = False
command = self.__addSingleCommand(self.SET_SCAN_SIZE, None, [size_x, size_y])
response = self.__sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if logLevel == logging.DEBUG:
log.debug(
"SetScanSize: (%i,%i) , completed in %.1f ms",
size_x,
size_y,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
@deprecated_argument(name="sizeX", since="5.2.0", alternative="size_x")
@deprecated_argument(name="sizeY", since="5.2.0", alternative="size_y")
@deprecated_argument(
name="changedProperties", since="5.2.0", alternative="changed_properties"
)
def SetScanSizeAndGetChangedProperties(self, size_x, size_y, changed_properties):
t0 = self.GetTime()
ret = False
command = self.__addSingleCommand(
self.SET_SCAN_SIZE_AND_GET_CHANGED_PROPERTIES, None, [size_x, size_y]
)
response = self.__sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if ret:
ret = self.ParseChangedProperties(changed_properties, response)
if logLevel == logging.DEBUG:
log.debug(
"SetScanSize: (%i,%i) , completed in %.1f ms",
size_x,
size_y,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
def SetScanROI(self, enable, offsetX, offsetY, sizeX, sizeY):
t0 = self.GetTime()
ret = False
command = self.__addSingleCommand(
self.SET_SCAN_ROI, None, [enable, offsetX, offsetY, sizeX, sizeY]
)
response = self.__sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if logLevel == logging.DEBUG:
log.debug(
"SetScanROI: (%i,%i,%i,%i) , completed in %.1f ms",
offsetX,
offsetY,
sizeX,
sizeY,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
def SetScanROIAndGetChangedProperties(
self, enable, offsetX, offsetY, sizeX, sizeY, changedProperties
):
t0 = self.GetTime()
ret = False
command = self.__addSingleCommand(
self.SET_SCAN_ROI__AND_GET_CHANGED_PROPERTIES,
None,
[enable, offsetX, offsetY, sizeX, sizeY],
)
response = self.__sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if ret:
ret = self.ParseChangedProperties(changedProperties, response)
if logLevel == logging.DEBUG:
log.debug(
"SetScanROI: (%i,%i,%i,%i) , completed in %.1f ms",
offsetX,
offsetY,
sizeX,
sizeY,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
def set_hw_roi_and_get_changed_properties(
self, offsetX: int, offsetY: int, sizeX: int, sizeY: int, changedProperties
):
"""
Set the hardware region of interest (ROI) of the current camera on DE-Server and get all
the changed properties. This is useful for testing and determining how certain
properties affect others.
Parameters
----------
offsetX : int
The x offset of the ROI
offsetY : int
The y offset of the ROI
sizeX : int
The width of the ROI
sizeY : int
The height of the ROI
changedProperties : list
List of properties that have changed
"""
t0 = self.GetTime()
ret = False
command = self._addSingleCommand(
self.SET_HW_ROI_AND_GET_CHANGED_PROPERTIES,
None,
[offsetX, offsetY, sizeX, sizeY],
)
response = self._sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if ret:
ret = self.ParseChangedProperties(changedProperties, response)
if logLevel == logging.DEBUG:
log.debug(
"SetHwRoi: (%i,%i,%i,%i) , completed in %.1f ms",
offsetX,
offsetY,
sizeX,
sizeY,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
def set_sw_roi(self, offsetX: int, offsetY: int, sizeX: int, sizeY: int):
"""
Set the software region of interest (ROI) of the current camera on DE-Server.
Parameters
----------
offsetX : int
The x offset of the ROI
offsetY : int
The y offset of the ROI
sizeX : int
The width of the ROI
sizeY : int
The height of the ROI
"""
t0 = self.GetTime()
ret = False
command = self._addSingleCommand(
self.SET_SW_ROI, None, [offsetX, offsetY, sizeX, sizeY]
)
response = self._sendCommand(command)
if response != False:
ret = response.acknowledge[0].error != True
self.refreshProperties = True
if logLevel == logging.DEBUG:
log.debug(
"SetSwRoi: (%i,%i,%i,%i) , completed in %.1f ms",
offsetX,
offsetY,
sizeX,
sizeY,
(self.GetTime() - t0) * 1000,
)
return ret
@write_only
@deprecated_argument(name="binX", since="5.2.0", alternative="bin_x")
@deprecated_argument(name="binY", since="5.2.0", alternative="bin_y")
@deprecated_argument(name="useHW", since="5.2.0", alternative="use_hw")
def set_binning(self, bin_x, bin_y, use_hw=True):
"""
Set the binning of the current camera on DE-Server. If useHW is True, the binning will
use hardware binning. If useHW is False, the binning will use software binning only.
Note
----
In almost all cases useHW should be True. HW binning usually speeds up the camera and
reduces the amount of data sent to the server. It also spreads the dose over a larger number
of pixels which reduces the local dose/damage for each pixel.
Parameters
----------
bin_x : int
The binning in the x direction
bin_y : int
The binning in the y direction
use_hw : bool
If True, use hardware binning and software binning. If False, use software binning only.
"""
retval = True
if commandVersion < 10:
retval = self.SetProperty(
"Binning Mode", "Hardware and Software" if useHW else "Software Only"
)
retval &= self.SetProperty("Binning X", bin_x)
retval &= self.SetProperty("Binning Y", bin_y)
else:
if commandVersion >= 13:
retval = self.SetProperty("Server Normalize Properties", "Off")
if use_hw:
if bin_x > 1: