-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibWrapper.cs
More file actions
790 lines (701 loc) · 33.1 KB
/
LibWrapper.cs
File metadata and controls
790 lines (701 loc) · 33.1 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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace PCO.SDK.NET;
/// <summary>
/// Wrapper class for the PCO.SDK (C++) library.
/// </summary>
public static class LibWrapper
{
/// <summary>
/// Stores errors coded as integers. Text descriptions of errors can be retrieved using <see cref="GetLastError"/> class.
/// </summary>
private static int _error;
private static PCO_Description _cameraDescription = new() { wSize = (ushort)Marshal.SizeOf(_cameraDescription) };
#region CameraAccess
/// <summary>
/// Open connection to camera.
/// </summary>
/// <param name="cameraHandle">Handle to opened camera.</param>
/// <returns>True if camera was found.</returns>
public static bool OpenCamera(ref nint cameraHandle)
{
return SDK.PCO_OpenCamera(pHandle: ref cameraHandle, wCamNum: 0) == 0;
}
/// <summary>
/// Close connection to a previously opened camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <exception cref="PcoException"></exception>
public static void CloseCamera(nint cameraHandle)
{
_error = SDK.PCO_CloseCamera(pHandle: cameraHandle);
if (_error != 0)
throw new PcoException(_error);
}
#endregion
#region CameraDescription
// ToDo: Retrieve fields instead (eg. dynamic range, limits and increments of binning, roi, size and exposure?
internal static PCO_Description GetCameraDescription(nint cameraHandle)
{
_error = SDK.PCO_GetCameraDescription(pHandle: cameraHandle, strDescription: ref _cameraDescription);
if (_error != 0)
throw new PcoException(_error);
else return _cameraDescription;
}
/// <summary>
/// Get bit depth of camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Bit depth (in bits per pixel).</returns>
/// <exception cref="PcoException"></exception>
public static ushort GetCameraBitDepth(nint cameraHandle)
{
_error = SDK.PCO_GetCameraDescription(pHandle: cameraHandle, strDescription: ref _cameraDescription);
if(_error != 0)
throw new PcoException(_error);
else return _cameraDescription.wDynResDESC;
}
#endregion
#region CameraStatus
/// <summary>
/// Get name of camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Camera name.</returns>
public static string GetCameraName(nint cameraHandle)
{
byte[] szCameraName = new byte[40];
_error = SDK.PCO_GetCameraName(pHandle: cameraHandle, szCameraName: szCameraName, wSZCameraNameLen: (ushort)szCameraName.Length);
if (_error != 0)
throw new PcoException(_error);
// Return string with trailing zeros trimmed away.
return Encoding.Default.GetString(szCameraName).TrimEnd('\0');
}
/// <summary>
/// Get serial number of camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Camera serial number.</returns>
public static string GetCameraSerialNumber(nint cameraHandle)
{
var cameraType = new PCO_CameraType();
cameraType.wSize = (ushort)Marshal.SizeOf(cameraType);
_error = SDK.PCO_GetCameraType(pHandle: cameraHandle, strCameraType: ref cameraType);
if (_error != 0)
throw new PcoException(_error);
else return cameraType.dwSerialNumber.ToString("");
}
/// <summary>
/// Get temperature in camera, based on specified probe location.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="selector">Probe location.</param>
/// <returns>Temperature in degrees Celsius.</returns>
/// <exception cref="PcoException"></exception>
public static float GetCameraTemperature(nint cameraHandle, TemperatureSelector selector)
{
short sensorTemp = 0, internalTemp = 0, additionalTemp = 0;
_error = SDK.PCO_GetTemperature(pHandle: cameraHandle, sCCDTemp: ref sensorTemp, sCamTemp: ref internalTemp, sPowTemp: ref additionalTemp);
if (_error != 0)
throw new PcoException(_error);
var deviceTemperature = selector switch
{
TemperatureSelector.Sensor => sensorTemp / 10,
TemperatureSelector.Mainboard => internalTemp,
TemperatureSelector.DeviceSpecific => additionalTemp,
_ => sensorTemp / 10
};
return deviceTemperature;
}
/// <summary>
/// Get current health status of camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="warningMessage">Warning message received.</param>
/// <param name="errorMessage">Error message received.</param>
/// <returns>True if camera is healthy.</returns>
public static bool GetCameraHealth(nint cameraHandle, out string warningMessage, out string errorMessage)
{
uint cameraWarning = 0, cameraError = 0, cameraStatus = 0;
_error = SDK.PCO_GetCameraHealthStatus(pHandle: cameraHandle, dwWarn: ref cameraWarning, dwError: ref cameraError, ref cameraStatus); // what is returned in case of warning, error, status fail?
warningMessage = GetLastError.ToString((int)cameraWarning);
errorMessage = GetLastError.ToString((int)cameraError);
return _error == 0; // does it only return false when error is received?
}
#endregion
#region CameraControl
/// <summary>
/// Reset all camera settings to their default values.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <exception cref="PcoException"></exception>
public static void ResetSettingsToDefault(nint cameraHandle)
{
// Make sure camera is stopped.
if (GetRecordingState(cameraHandle))
SetRecordingState(cameraHandle, false);
// Reset settings.
_error = SDK.PCO_ResetSettingsToDefault(pHandle: cameraHandle);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Arms the camera, preparing it for recording.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <exception cref="PcoException"></exception>
public static void ArmCamera(nint cameraHandle)
{
_error = SDK.PCO_ArmCamera(pHandle: cameraHandle);
if (_error != 0)
throw new PcoException(_error);
}
#endregion
#region ImageSensor
/// <summary>
/// Get collective information about settings of imaging sensor.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Sensor settings.</returns>
/// <exception cref="PcoException"></exception>
internal static PCO_Sensor GetSensorStruct(nint cameraHandle)
{
// not working?
var cameraSensor = new PCO_Sensor { wSize = 8000 }; // (ushort)Marshal.SizeOf(_cameraSensor); // 5880
cameraSensor.strDescription.wSize = (ushort)Marshal.SizeOf(cameraSensor.strDescription); // 436
cameraSensor.strDescription2.wSize = (ushort)Marshal.SizeOf(cameraSensor.strDescription2); // 296
cameraSensor.strDescriptionIntensified.wSize = (ushort)Marshal.SizeOf(cameraSensor.strDescriptionIntensified); // 160
cameraSensor.strDescription3.wSize = (ushort)Marshal.SizeOf(cameraSensor.strDescription3); // 192
cameraSensor.strSignalDesc.wSize = (ushort)Marshal.SizeOf(cameraSensor.strSignalDesc);
//for (int i = 0; i < 20; i++)
//{
// _cameraSensor.strSignalDesc.strSingeSignalDesc[i].wSize = (ushort)Marshal.SizeOf(_cameraSensor.strSignalDesc.strSingeSignalDesc[i]);
//}
_error = SDK.PCO_GetSensorStruct(pHandle: cameraHandle, strSensor: ref cameraSensor);
if (_error != 0)
throw new PcoException(_error);
else return cameraSensor;
}
/// <summary>
/// Get current (armed) image size in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="width">Current horizontal resolution.</param>
/// <param name="height">Current vertical resolution.</param>
/// <param name="widthMax">Maximum horizontal resolution.</param>
/// <param name="heightMax">Maximum vertical resolution.</param>
/// <exception cref="PcoException"></exception>
public static void GetSizes(nint cameraHandle, ref ushort width, ref ushort height, ref ushort widthMax, ref ushort heightMax)
{
_error = SDK.PCO_GetSizes(pHandle: cameraHandle, wXResAct: ref width, wYResAct: ref height, wXResMax: ref widthMax, wYResMax: ref heightMax);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Get current sensor format (standard or extended) in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="sensorWidth">Maximum width.</param>
/// <param name="sensorHeight">Maximum height.</param>
/// <exception cref="PcoException"></exception>
public static void GetSensorFormat(nint cameraHandle, out ushort sensorWidth, out ushort sensorHeight, out ushort minimumWidth, out ushort minimumHeight)
{
// Retrieve information about sensor format in use (standard or extended).
ushort wSensor = 0;
_error = SDK.PCO_GetSensorFormat(pHandle: cameraHandle, wSensor: ref wSensor);
if (_error != 0)
throw new PcoException(_error);
// Retrieve description of camera device.
_error = SDK.PCO_GetCameraDescription(pHandle: cameraHandle, strDescription: ref _cameraDescription);
if (_error != 0)
throw new PcoException(_error);
// Return maximum width and height based on sensor format.
if (wSensor == 0) // standard
{
sensorHeight = _cameraDescription.wMaxVertResStdDESC;
sensorWidth = _cameraDescription.wMaxHorzResStdDESC;
}
else // extended
{
sensorHeight = _cameraDescription.wMaxVertResExtDESC;
sensorWidth = _cameraDescription.wMaxHorzResExtDESC;
}
minimumWidth = _cameraDescription.wMinSizeHorzDESC;
minimumHeight = _cameraDescription.wMinSizeVertDESC;
}
/// <summary>
/// Get currently used region-of-interest (ROI) in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="width">Width.</param>
/// <param name="height">Height.</param>
/// <param name="offsetX">OffsetX.</param>
/// <param name="offsetY">OffsetY.</param>
/// <exception cref="PcoException"></exception>
public static void GetROI(nint cameraHandle, out ushort width, out ushort height, out ushort offsetX, out ushort offsetY)
{
ushort wRoiX0 = 0, wRoiY0 = 0, wRoiX1 = 0, wRoiY1 = 0;
_error = SDK.PCO_GetROI(pHandle: cameraHandle, wRoiX0: ref wRoiX0, wRoiY0: ref wRoiY0, wRoiX1: ref wRoiX1, wRoiY1: ref wRoiY1);
if (_error != 0)
throw new PcoException(_error);
width = (ushort)(wRoiX1 - wRoiX0 + 1);
height = (ushort)(wRoiY1 - wRoiY0 + 1);
offsetX = (ushort)(wRoiX0 - 1);
offsetY = (ushort)(wRoiY0 - 1);
}
/// <summary>
/// Set region-of-interest (ROI) in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="width">Width.</param>
/// <param name="height">Height.</param>
/// <param name="offsetX">OffsetX.</param>
/// <param name="offsetY">OffsetY.</param>
/// <exception cref="PcoException"></exception>
public static void SetROI(nint cameraHandle, ushort width, ushort height, ushort offsetX, ushort offsetY)
{
_error = SDK.PCO_SetROI(pHandle: cameraHandle, wRoiX0: (ushort)(offsetX + 1), wRoiY0: (ushort)(offsetY + 1), wRoiX1: (ushort)(offsetX + width), wRoiY1: (ushort)(offsetY + height));
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Get smallest incremental steps available for region-of-interests (ROI) in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="horizontalStep">Horizontal ROI increment.</param>
/// <param name="verticalStep">Vertical ROI increment.</param>
/// <exception cref="PcoException"></exception>
public static void GetROISteps(nint cameraHandle, out ushort horizontalStep, out ushort verticalStep)
{
_cameraDescription = GetCameraDescription(cameraHandle);
horizontalStep = _cameraDescription.wRoiHorStepsDESC;
verticalStep = _cameraDescription.wRoiVertStepsDESC;
}
/// <summary>
/// Get binning setting currently used in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="orientation">Horizontal or vertical.</param>
/// <returns>Binning setting.</returns>
/// <exception cref="PcoException"></exception>
public static ushort GetBinning(nint cameraHandle, BinningOrientation orientation)
{
ushort wBinHorz = 0, wBinVert = 0;
_error = SDK.PCO_GetBinning(pHandle: cameraHandle, wBinHorz: ref wBinHorz, wBinVert: ref wBinVert);
if (_error != 0)
throw new PcoException(_error);
return orientation switch
{
BinningOrientation.Horizontal => wBinHorz,
_ => wBinVert
};
}
/// <summary>
/// Set binning settings in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="binningSetting">New binning setting.</param>
/// <param name="orientation">Horizontal or vertical.</param>
/// <exception cref="PcoException"></exception>
public static void SetBinning(nint cameraHandle, ushort binningSetting, BinningOrientation orientation)
{
var horizontalBinning = GetBinning(cameraHandle, BinningOrientation.Horizontal);
var verticalBinning = GetBinning(cameraHandle, BinningOrientation.Vertical);
_error = orientation == BinningOrientation.Horizontal
? SDK.PCO_SetBinning(pHandle: cameraHandle, wBinHorz: binningSetting, wBinVert: verticalBinning)
: SDK.PCO_SetBinning(pHandle: cameraHandle, wBinHorz: horizontalBinning, wBinVert: binningSetting);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Get limits available for binning settings in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="maximumHorizontalBinning">Maximum horizontal binning (number of pixels).</param>
/// <param name="maximumVerticalBinning">Maximum vertical binning (number of pixels).</param>
/// <param name="horizontalBinningStep">Horizontal binning increment.</param>
/// <param name="verticalBinningStep">Vertical binning increment.</param>
/// <exception cref="PcoException"></exception>
public static void GetBinningLimits(nint cameraHandle, out ushort maximumHorizontalBinning, out ushort maximumVerticalBinning, out ushort horizontalBinningStep, out ushort verticalBinningStep)
{
_error = SDK.PCO_GetCameraDescription(pHandle: cameraHandle, strDescription: ref _cameraDescription);
if (_error != 0)
throw new PcoException(_error);
else
{
maximumHorizontalBinning = _cameraDescription.wMaxBinHorzDESC;
maximumVerticalBinning = _cameraDescription.wMaxBinVertDESC;
horizontalBinningStep = _cameraDescription.wBinHorzSteppingDESC;
verticalBinningStep = _cameraDescription.wBinVertSteppingDESC;
}
}
/// <summary>
/// Get list of valid binning settings of camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="orientation">Horizontal or vertical.</param>
/// <returns></returns>
/// <exception cref="PcoException"></exception>
public static List<long> GetValidBinningModes(nint cameraHandle, BinningOrientation orientation)
{
_error = SDK.PCO_GetCameraDescription(pHandle: cameraHandle, strDescription: ref _cameraDescription);
if (_error != 0)
throw new PcoException(_error);
var binningModes = new List<long>();
if (orientation == BinningOrientation.Horizontal)
{
if (_cameraDescription.wBinHorzSteppingDESC == 0) // binary stepping
{
int i = 1;
while (i <= _cameraDescription.wMaxBinHorzDESC)
{
binningModes.Add(i);
i *= 2;
}
}
else // linear stepping
{
int i = 1;
while (i <= _cameraDescription.wMaxBinHorzDESC)
{
binningModes.Add(i);
i += 1;
}
}
}
else
{
if (_cameraDescription.wBinVertSteppingDESC == 0) // binary stepping
{
int i = 1;
while (i <= _cameraDescription.wMaxBinVertDESC)
{
binningModes.Add(i);
i *= 2;
}
}
else // linear stepping
{
int i = 1;
while (i <= _cameraDescription.wMaxBinVertDESC)
{
binningModes.Add(i);
i += 1;
}
}
}
return binningModes;
}
/// <summary>
/// Get noise filter correction mode currently used in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Noise filter mode in use.</returns>
/// <exception cref="PcoException"></exception>
public static NoiseFilterMode GetNoiseFilterMode(nint cameraHandle)
{
ushort wNoiseFilterMode = 0;
_error = SDK.PCO_GetNoiseFilterMode(pHandle: cameraHandle, wNoiseFilterMode: ref wNoiseFilterMode);
if (_error != 0)
throw new PcoException(_error);
else return (NoiseFilterMode)wNoiseFilterMode;
}
/// <summary>
/// Set noise filter correction mode in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="mode">Noise filter correction mode.</param>
/// <exception cref="PcoException"></exception>
public static void SetNoiseFilterMode(nint cameraHandle, NoiseFilterMode mode)
{
_error = SDK.PCO_SetNoiseFilterMode(pHandle: cameraHandle, wNoiseFilterMode: (ushort)mode);
if (_error != 0)
throw new PcoException(_error);
}
#endregion
#region TimingControl
/// <summary>
/// Get current frame rate from camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Frame rate in [Hz].</returns>
/// <exception cref="PcoException"></exception>
public static float GetFrameRate(nint cameraHandle)
{
uint dwTime_s = 0, dwTime_ns = 0;
_error = SDK.PCO_GetCOCRuntime(pHandle: cameraHandle, dwTime_s: ref dwTime_s, dwTime_ns: ref dwTime_ns); // get currently set COC runtime
if (_error != 0)
throw new PcoException(_error);
float frameTime = dwTime_s * 1000000000 + dwTime_ns; // time required to take a single image [ns]
return 1 / frameTime * 1E9f; // current framerate [Hz]
}
/// <summary>
/// Set frame rate [Hz] in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="framerate">variable representing the wanted framerate in [Hz].</param>
/// <exception cref="PcoException"></exception>
public static void SetFrameRate(nint cameraHandle, float framerate)
{
// get current delay and exposure times
ushort wTimeBaseDelay = 0, wTimeBaseExposure = 0; // time bases (ns)
uint currentExposure = 0, currentDelay = 0; // exposure and delay times
_error = SDK.PCO_GetDelayExposureTime(pHandle: cameraHandle, dwDelay: ref currentDelay, dwExposure: ref currentExposure, wTimeBaseDelay: ref wTimeBaseDelay, wTimeBaseExposure: ref wTimeBaseExposure); // get currently set delay and exposure times
if (_error != 0)
throw new PcoException(_error);
currentDelay *= (uint)Math.Pow(1000, wTimeBaseDelay); // ns
currentExposure *= (uint)Math.Pow(1000, wTimeBaseExposure); // ns
// get current frame time
uint dwTime_s = 0, dwTime_ns = 0;
_error = SDK.PCO_GetCOCRuntime(pHandle: cameraHandle, dwTime_s: ref dwTime_s, dwTime_ns: ref dwTime_ns); // get currently set COC runtime
if (_error != 0)
throw new PcoException(_error);
// calculate new delay time
uint currentFrameTime = dwTime_s * 1000000000 + dwTime_ns; // time required to take a single image [ns]
int newDelay = System.Convert.ToInt32(currentDelay + (int)Math.Round(1 / framerate * 1E9 - currentFrameTime));
if (newDelay >= 0)
_error = SDK.PCO_SetDelayExposureTime(pHandle: cameraHandle, dwDelay: (uint)newDelay / 1000, dwExposure: currentExposure / 1000, wTimeBaseDelay: 1, wTimeBaseExposure: 1); // set new delay time [ns]
else
_error = SDK.PCO_SetDelayExposureTime(pHandle: cameraHandle, dwDelay: 0, dwExposure: (uint)(currentExposure / 1000 + newDelay / 1000), wTimeBaseDelay: 1, wTimeBaseExposure: 1); // reduce exposure time
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Get exposure time in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Exposure time (in microseconds).</returns>
/// <exception cref="PcoException"></exception>
public static double GetExposureTime(nint cameraHandle)
{
uint currentExposure = 0, currentDelay = 0; // exposure and delay times
ushort wTimeBaseDelay = 0, wTimeBaseExposure = 0; // timebases
_error = SDK.PCO_GetDelayExposureTime(pHandle: cameraHandle, dwDelay: ref currentDelay, dwExposure: ref currentExposure, wTimeBaseDelay: ref wTimeBaseDelay, wTimeBaseExposure: ref wTimeBaseExposure); // get currently set delay and exposure times
if (_error != 0)
throw new PcoException(_error);
currentExposure *= (uint)Math.Pow(1000, wTimeBaseExposure); // ns
return currentExposure / 1000;
}
/// <summary>
/// Get limits available for exposure time in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="minExposure">Minimum exposure time (in microseconds).</param>
/// <param name="maxExposure">Maximum exposure time (in microseconds).</param>
/// <param name="stepExposure">Exposure time increment (in microseconds).</param>
/// <exception cref="PcoException"></exception>
public static void GetExposureTimeLimits(nint cameraHandle, out uint minExposure, out uint maxExposure, out double stepExposure)
{
_error = SDK.PCO_GetCameraDescription(pHandle: cameraHandle, strDescription: ref _cameraDescription);
if (_error != 0)
throw new PcoException(_error);
else
{
minExposure = _cameraDescription.dwMinExposureDESC / 1000; // ns => µs
maxExposure = _cameraDescription.dwMaxExposureDESC * 1000; // ms => µs
stepExposure = System.Convert.ToDouble(System.Convert.ToDecimal(_cameraDescription.dwMinExposStepDESC) / 1000); // ns => µs
}
}
/// <summary>
/// Set exposure time in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="exposureTime">Exposure time (in microseconds).</param>
/// <exception cref="PcoException"></exception>
public static void SetExposureTime(nint cameraHandle, double exposureTime)
{
// Retrieve current framerate.
float framerate = GetFrameRate(cameraHandle);
// Set delay and exposure time.
int _error = SDK.PCO_SetDelayExposureTime(pHandle: cameraHandle, dwDelay: 0, dwExposure: (uint)exposureTime, wTimeBaseDelay: 1, wTimeBaseExposure: 1); // try to set requested exposure time (minimize delay)
if (_error != 0)
throw new PcoException(_error);
// Retain frame rate used (will adjust delay time).
SetFrameRate(cameraHandle, framerate);
}
/// <summary>
/// Get trigger mode currently used in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Trigger mode.</returns>
/// <exception cref="PcoException"></exception>
public static TriggerMode GetTriggerMode(nint cameraHandle)
{
ushort wTriggerMode = 0;
_error = SDK.PCO_GetTriggerMode(pHandle: cameraHandle, wTriggerMode: ref wTriggerMode);
if (_error != 0)
throw new PcoException(_error);
else return (TriggerMode)wTriggerMode;
}
/// <summary>
/// Sets trigger mode in camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="triggerMode">Trigger mode.</param>
/// <exception cref="PcoException"></exception>
public static void SetTriggerMode(nint cameraHandle, TriggerMode triggerMode)
{
_error = SDK.PCO_SetTriggerMode(pHandle: cameraHandle, wTriggerMode: (ushort)triggerMode);
if (_error != 0)
throw new PcoException(_error);
}
#endregion
#region RecordingControl
/// <summary>
/// Get current recording state of camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>True if recording is active.</returns>
/// <exception cref="PcoException"></exception>
public static bool GetRecordingState(nint cameraHandle)
{
ushort recordingState = 0;
_error = SDK.PCO_GetRecordingState(pHandle: cameraHandle, wRecState: ref recordingState);
if (_error != 0)
throw new PcoException(_error);
else return recordingState == 1;
}
/// <summary>
/// Set recording state of camera.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="state">True to start recording, false to stop.</param>
/// <exception cref="PcoException"></exception>
public static void SetRecordingState(nint cameraHandle, bool state)
{
_error = state ? SDK.PCO_SetRecordingState(pHandle: cameraHandle, wRecState: 1) : SDK.PCO_SetRecordingState(pHandle: cameraHandle, wRecState: 0);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Set date and time of internal camera clock.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="dateTime">Date and time to be set.</param>
/// <exception cref="PcoException"></exception>
public static void SetDateTime(nint cameraHandle, DateTime dateTime)
{
_error = SDK.PCO_SetDateTime(pHandle: cameraHandle, ucDay: (byte)dateTime.Day, ucMonth: (byte)dateTime.Month, wYear: (ushort)dateTime.Year, wHour: (ushort)dateTime.Hour, ucMin: (byte)dateTime.Minute, ucSec: (byte)dateTime.Second);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Set timestamp mode of camera, determining how timestamps are written to raw image data.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="mode">Timestamp mode.</param>
/// <exception cref="PcoException"></exception>
public static void SetTimestampMode(nint cameraHandle, TimestampMode mode)
{
_error = SDK.PCO_SetTimestampMode(pHandle: cameraHandle, wTimeStampMode: (ushort)mode);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Get current timestamp mode of camera, determining how timestamps are written to raw image data.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <returns>Current timestamp mode implemented.</returns>
/// <exception cref="PcoException"></exception>
public static TimestampMode GetTimestampMode(nint cameraHandle)
{
ushort wTimeStampMode = 0;
_error = SDK.PCO_GetTimestampMode(pHandle: cameraHandle, wTimeStampMode: ref wTimeStampMode);
if (_error != 0)
throw new PcoException(_error);
else return (TimestampMode)wTimeStampMode;
}
#endregion
#region ImageInformation
#endregion
#region BufferManagement
// ToDo: Simplify buffer management methods?
/// <summary>
/// Allocate memory for buffer.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="sBufNr">Buffer index.</param>
/// <param name="size">Buffer size (in bytes).</param>
/// <param name="wBuf">Buffer handle.</param>
/// <param name="hEvent">Buffer event handle.</param>
/// <exception cref="PcoException"></exception>
public static void AllocateBuffer(nint cameraHandle, ref short sBufNr, int size, ref nuint wBuf, ref nint hEvent)
{
_error = SDK.PCO_AllocateBuffer(pHandle: cameraHandle, sBufNr: ref sBufNr, size: size, wBuf: ref wBuf, hEvent: ref hEvent);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Free memory allocation of buffer.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="sBufNr">Buffer index.</param>
/// <exception cref="PcoException"></exception>
public static void FreeBuffer(nint cameraHandle, short sBufNr)
{
_error = SDK.PCO_FreeBuffer(pHandle: cameraHandle, sBufNr: sBufNr);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Retrieve status of buffer.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <param name="sBufNr">Buffer index.</param>
/// <param name="dwStatusDll">SDK status.</param>
/// <param name="dwStatusDrv">Image transfer status.</param>
/// <exception cref="PcoException"></exception>
public static void GetBufferStatus(nint cameraHandle, short sBufNr, ref uint dwStatusDll, ref uint dwStatusDrv)
{
_error = SDK.PCO_GetBufferStatus(pHandle: cameraHandle, sBufNr: sBufNr, dwStatusDll: ref dwStatusDll, dwStatusDrv: ref dwStatusDrv);
if (_error != 0)
throw new PcoException(_error);
}
#endregion
#region ImageAcquisition
/// <summary>
///
/// </summary>
/// <param name="cameraHandle"></param>
/// <param name="dwFirstImage"></param>
/// <param name="dwLastImage"></param>
/// <param name="sBufNr"></param>
/// <param name="wXRes"></param>
/// <param name="wYRes"></param>
/// <param name="wBitPerPixel"></param>
/// <exception cref="PcoException"></exception>
public static void AddBufferEx(nint cameraHandle, uint dwFirstImage, uint dwLastImage, short sBufNr, ushort wXRes, ushort wYRes, ushort wBitPerPixel)
{
_error = SDK.PCO_AddBufferEx(pHandle: cameraHandle, dwFirstImage: dwFirstImage, dwLastImage: dwLastImage, sBufNr: sBufNr, wXRes: wXRes, wYRes: wYRes, wBitPerPixel: wBitPerPixel);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
/// Removes all remaining buffers from internal queue and resets the transfer state.
/// </summary>
/// <param name="cameraHandle">Handle to a previously opened camera device.</param>
/// <exception cref="PcoException"></exception>
public static void CancelImages(nint cameraHandle)
{
_error = SDK.PCO_CancelImages(pHandle: cameraHandle);
if (_error != 0)
throw new PcoException(_error);
}
/// <summary>
///
/// </summary>
/// <param name="cameraHandle"></param>
/// <param name="nr_of_buffer"></param>
/// <param name="bufferList"></param>
/// <param name="timeout"></param>
/// <exception cref="PcoException"></exception>
public static void WaitForBuffer(nint cameraHandle, int nr_of_buffer, ref PCO_Buflist bufferList, int timeout)
{
_error = SDK.PCO_WaitforBuffer(pHandle: cameraHandle, nr_of_buffer: nr_of_buffer, bufferList: ref bufferList, timeout: timeout);
if (_error != 0)
throw new PcoException(_error);
}
#endregion
}