-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRTProtocol.cs
1489 lines (1348 loc) · 56.5 KB
/
RTProtocol.cs
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
// Realtime SDK for Qualisys Track Manager. Copyright 2015-2018 Qualisys AB
//
using QTMRealTimeSDK.Data;
using QTMRealTimeSDK.Network;
using QTMRealTimeSDK.Settings;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
namespace QTMRealTimeSDK
{
/// <summary>Streaming rate</summary>
public enum StreamRate
{
RateAllFrames = 1,
RateFrequency,
RateFrequencyDivisor
}
/// <summary>Data with response from Discovery broadcast</summary>
public struct DiscoveryResponse
{
/// <summary>Hostname of server</summary>
public string HostName;
/// <summary>IP to server</summary>
public string IpAddress;
/// <summary>Base port</summary>
public short Port;
/// <summary>Info text about host</summary>
public string InfoText;
/// <summary>Number of cameras connected to server</summary>
public int CameraCount;
}
public class RTProtocol : IDisposable
{
/// <summary>Constants relating to Protocol</summary>
public static class Constants
{
/// <summary>Latest major version of protocol</summary>
public const int MAJOR_VERSION = 1;
/// <summary>Latest minor version of protocol</summary>
public const int MINOR_VERSION = 26;
/// <summary>Maximum camera count</summary>
public const int MAX_CAMERA_COUNT = 256;
/// <summary>Maximum Analog device count</summary>
public const int MAX_ANALOG_DEVICE_COUNT = 64;
/// <summary>Maximum force plate count</summary>
public const int MAX_FORCE_PLATE_COUNT = 64;
/// <summary>Maximum Gaze vector count</summary>
public const int MAX_GAZE_VECTOR_COUNT = 64;
/// <summary>Size of all packet headers, packet size and packet type</summary>
public const int PACKET_HEADER_SIZE = 8;
/// <summary>Size of data packet header, timestamp, frame number and component count</summary>
public const int DATA_PACKET_HEADER_SIZE = 16;
/// <summary>Size of component header, component size and component type</summary>
public const int COMPONENT_HEADER = 8;
/// <summary>Default base port used by QTM</summary>
public const int STANDARD_BASE_PORT = 22222;
/// <summary>Port QTM listens to for discovery requests</summary>
public const int STANDARD_BROADCAST_PORT = 22226;
/// <summary>QTM packet data part read timeout in microseconds</summary>
public const int SOCKET_READ_TIMEOUT = 5000000; // 5 sec
}
/// <summary>Packet received from QTM</summary>
protected RTPacket Packet
{
get { return mPacket; }
}
private SettingsGeneral mGeneralSettings;
/// <summary>General settings from QTM</summary>
public SettingsGeneral GeneralSettings
{
get { return mGeneralSettings; }
set
{
if (mGeneralSettings != value)
{
mGeneralSettings = value;
}
}
}
private Settings3D m3DSettings;
/// <summary>3D settings from QTM</summary>
public Settings3D Settings3D
{
get { return m3DSettings; }
set
{
if (m3DSettings != value)
{
m3DSettings = value;
}
}
}
private Settings6D m6DOFSettings;
/// <summary>6DOF settings from QTM</summary>
public Settings6D Settings6DOF
{
get { return m6DOFSettings; }
set
{
if (m6DOFSettings != value)
{
m6DOFSettings = value;
}
}
}
private SettingsAnalog mAnalogSettings;
/// <summary>Analog settings from QTM</summary>
public SettingsAnalog AnalogSettings
{
get { return mAnalogSettings; }
set
{
if (mAnalogSettings != value)
{
mAnalogSettings = value;
}
}
}
private SettingsForce mForceSettings;
/// <summary>Force settings from QTM</summary>
public SettingsForce ForceSettings
{
get { return mForceSettings; }
set
{
if (mForceSettings != value)
{
mForceSettings = value;
}
}
}
private SettingsImage mImageSettings;
/// <summary>Image settings from QTM</summary>
public SettingsImage ImageSettings
{
get { return mImageSettings; }
set
{
if (mImageSettings != value)
{
mImageSettings = value;
}
}
}
private SettingsGazeVectors mGazeVectorSettings;
/// <summary>Gaze vector settings from QTM</summary>
public SettingsGazeVectors GazeVectorSettings { get { return mGazeVectorSettings; } }
private SettingsEyeTrackers mEyeTrackerSettings;
/// <summary>Eye tracker settings from QTM</summary>
public SettingsEyeTrackers EyeTrackerSettings { get { return mEyeTrackerSettings; } }
private SettingsSkeletons mSkeletonSettings;
/// <summary>Skeleton settings from QTM</summary>
public SettingsSkeletons SkeletonSettings { get { return mSkeletonSettings; } }
private SettingsSkeletonsHierarchical mSkeletonSettingsHierarchical;
/// <summary>Skeleton settings from QTM</summary>
public SettingsSkeletonsHierarchical SkeletonSettingsHierarchical { get { return mSkeletonSettingsHierarchical; } }
private RTNetwork mNetwork;
private ushort mUDPport;
private RTPacket mPacket;
private int mMajorVersion;
private int mMinorVersion;
private string mErrorString;
private HashSet<DiscoveryResponse> mDiscoveryResponses;
/// <summary>list of discovered QTM server possible to connect to</summary>
public HashSet<DiscoveryResponse> DiscoveryResponses
{
get
{
return mDiscoveryResponses;
}
}
/// <summary>
/// Default constructor
///</summary>
public RTProtocol(int majorVersion = Constants.MAJOR_VERSION, int minorVersion = Constants.MINOR_VERSION)
{
mMajorVersion = majorVersion;
mMinorVersion = minorVersion;
mPacket = new RTPacket(mMinorVersion, mMajorVersion);
mErrorString = "";
mNetwork = new RTNetwork();
mDiscoveryResponses = new HashSet<DiscoveryResponse>();
}
/// <summary>Create connection to server</summary>
/// <param name="serverAddr">address to server</param>
/// <param name="serverPortUDP">port to use if UDP socket is desired, set to 0 for automatic port selection</param>
/// <param name="majorVersion">Major protocol version to use, default is latest</param>
/// <param name="minorVersion">Minor protocol version to use, default is latest</param>
/// <param name="port">base port for QTM server, default is 22222</param>
/// <returns>true if connection was successful, otherwise false</returns>
public bool Connect(string serverAddr, int serverPortUDP = -1,
int majorVersion = Constants.MAJOR_VERSION, int minorVersion = Constants.MINOR_VERSION,
int port = Constants.STANDARD_BASE_PORT)
{
Disconnect();
mMajorVersion = majorVersion;
mMinorVersion = minorVersion;
if (mMajorVersion > 1 || mMinorVersion > 7)
{
// Allow for 1.8 and above versions
}
else
{
mErrorString = "Protocol version of 1.8 or lower can not be used by the c# software development kit";
return false;
}
PacketType packetType;
port += 1;
mPacket = new RTPacket(majorVersion, minorVersion);
if (mNetwork.Connect(serverAddr, port))
{
if (serverPortUDP >= 0)
{
mUDPport = (ushort)serverPortUDP;
if (mNetwork.CreateUDPSocket(ref mUDPport, false) == false)
{
mErrorString = String.Format("Error creating UDP socket: {0}", mNetwork.GetErrorString());
Disconnect();
return false;
}
}
//Get connection response from server
if (Receive(out packetType) == ResponseType.success)
{
if (packetType == PacketType.PacketError)
{
//Error from QTM
mErrorString = mPacket.GetErrorString();
Disconnect();
return false;
}
if (packetType == PacketType.PacketCommand)
{
string response = mPacket.GetCommandString();
if (response == "QTM RT Interface connected")
{
if (SetVersion(mMajorVersion, mMinorVersion, out response))
{
string expectedResponse = String.Format("Version set to {0}.{1}", mMajorVersion, mMinorVersion);
if (response == expectedResponse)
{
return true;
}
else
{
mErrorString = "Unexpected response from server";
Disconnect();
return false;
}
}
else
{
//Error setting version
mErrorString = "Error setting version of protocol";
Disconnect();
return false;
}
}
else
{
//missing QTM response
mErrorString = "Missing response from QTM Server";
Disconnect();
return false;
}
}
}
else
{
//Error receiving packet.
mErrorString = String.Format("Error Recieveing packet: {0}", mNetwork.GetErrorString());
Disconnect();
return false;
}
}
else
{
if (mNetwork.GetError() == SocketError.ConnectionRefused)
{
mErrorString = "Connection refused, Check if QTM is running on target machine";
Disconnect();
}
else
{
mErrorString = String.Format("Error connecting TCP socket: {0}", mNetwork.GetErrorString());
Disconnect();
}
return false;
}
return false;
}
/// <summary>Create connection to server</summary>
/// <param name="host">host detected via broadcast discovery</param>
/// <param name="serverPortUDP">port to use if UDP socket is desired, set to 0 for automatic port selection</param>
/// <param name="majorVersion">Major protocol version to use, default is latest</param>
/// <param name="minorVersion">Minor protocol version to use, default is latest</param>
/// <returns>true if connection was successful, otherwise false</returns>
public bool Connect(DiscoveryResponse host, int serverPortUDP = -1, int majorVersion = Constants.MAJOR_VERSION, int minorVersion = Constants.MINOR_VERSION)
{
return Connect(host.IpAddress, serverPortUDP, majorVersion, minorVersion, host.Port);
}
public void ClearSettings()
{
m3DSettings = null;
m6DOFSettings = null;
mAnalogSettings = null;
mForceSettings = null;
mGazeVectorSettings = null;
mSkeletonSettings = null;
mGeneralSettings = null;
mImageSettings = null;
}
/// <summary>Disconnect from server</summary>
public void Disconnect()
{
mNetwork.Disconnect();
mDiscoveryResponses.Clear();
ClearSettings();
}
/// <summary>Check if there is a tcp connection to the server available</summary>
/// <returns>connection status of TCP socket </returns>
public bool IsConnected()
{
return mNetwork.IsConnected();
}
public RTPacket GetRTPacket()
{
return mPacket;
}
private byte[] data = new byte[65535];
private Object receiveLock = new Object();
[Obsolete("ReceiveRTPacket is deprecated and replaced by Receive.", false)]
public int ReceiveRTPacket(out PacketType packetType, bool skipEvents = true, int timeout = Constants.SOCKET_READ_TIMEOUT)
{
int returnVal = -1;
var response = Receive(out packetType, skipEvents, timeout);
switch (response)
{
case ResponseType.success:
returnVal = RTPacket.GetPacketSize(data);
break;
case ResponseType.timeout:
returnVal = 0;
break;
case ResponseType.error:
case ResponseType.disconnect:
returnVal = -1;
break;
}
return returnVal;
}
public ResponseType Receive(out PacketType packetType, bool skipEvents = true, int timeout = Constants.SOCKET_READ_TIMEOUT)
{
lock (receiveLock)
{
int receivedTotal = 0;
int frameSize;
packetType = PacketType.PacketNone;
do
{
receivedTotal = 0;
var response = mNetwork.Receive(ref data, 0, data.Length, true, timeout);
if (response == ResponseType.timeout)
{
return ResponseType.timeout; // Receive timeout
}
if (response == ResponseType.error)
{
mErrorString = "Socket Error.";
return ResponseType.error;
}
if (response == ResponseType.disconnect)
{
mErrorString = "Disconnected from server.";
return ResponseType.disconnect;
}
if (response.received < Constants.PACKET_HEADER_SIZE)
{
mErrorString = "QTM header not received.";
return ResponseType.error;
}
receivedTotal += response.received;
frameSize = RTPacket.GetPacketSize(data);
packetType = RTPacket.GetPacketType(data);
if (data == null || frameSize > data.Length)
{
// Do some preventive additional allocation to reduce number of times allocation is needed
var newSize = (int)(frameSize * 1.47);
Array.Resize(ref data, newSize);
}
// Receive more data until we have read the whole packet
while (receivedTotal < frameSize)
{
// As long as we haven't received enough data, wait for more
response = mNetwork.Receive(ref data, receivedTotal, frameSize - receivedTotal, false, Constants.SOCKET_READ_TIMEOUT);
if (response == ResponseType.timeout)
{
mErrorString = "Packet truncated.";
return ResponseType.error;
}
if (response == ResponseType.error)
{
mErrorString = "Socket Error.";
return ResponseType.error;
}
if (response == ResponseType.disconnect)
{
mErrorString = "Disconnected from server.";
return ResponseType.disconnect;
}
receivedTotal += response.received;
}
mPacket.SetData(data);
}
while (skipEvents && packetType == PacketType.PacketEvent);
if (receivedTotal == frameSize)
{
return ResponseType.success;
}
mErrorString = "Packet truncated.";
return ResponseType.error;
}
}
/// <summary>Send discovery packet to network to find available QTM Servers.</summary>
/// <param name="replyPort">port for servers to reply.</param>
/// <param name="discoverPort">port to send discovery packet.</param>
/// <returns>true if discovery packet was sent successfully</returns>
public bool DiscoverRTServers(ushort replyPort, int discoverPort = Constants.STANDARD_BROADCAST_PORT)
{
byte[] port = BitConverter.GetBytes(replyPort);
byte[] size = BitConverter.GetBytes(10);
byte[] cmd = BitConverter.GetBytes((int)PacketType.PacketDiscover);
Array.Reverse(port);
List<byte> b = new List<byte>();
b.AddRange(size);
b.AddRange(cmd);
b.AddRange(port);
byte[] msg = b.ToArray();
// Create udp broadcast socket.
if (mNetwork.CreateUDPSocket(ref replyPort, true))
{
var sendStatus = mNetwork.SendUDPBroadcast(msg, 10);
if (!sendStatus)
{
// Something major failed when trying to broadcast
mNetwork.Disconnect();
return false;
}
mDiscoveryResponses.Clear();
const int discoverBufferSize = 65535;
byte[] discoverBuffer = new byte[discoverBufferSize];
Response response;
do
{
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
response = mNetwork.ReceiveBroadcast(ref discoverBuffer, discoverBufferSize, ref remoteEP, 100000);
if (response && response.received > 8)
{
var packetType = RTPacket.GetPacketType(discoverBuffer);
if (packetType == PacketType.PacketCommand)
{
DiscoveryResponse discoverResponse;
if (GetDiscoverData(discoverBuffer, out discoverResponse))
{
discoverResponse.IpAddress = (remoteEP as IPEndPoint).Address.ToString();
mDiscoveryResponses.Add(discoverResponse);
}
}
}
}
while (response && response.received > 8);
}
mNetwork.Disconnect(tcp: false, udp: false, udpBroadcast: true);
return true;
}
#region get set functions
/// <summary>Get protocol version used from QTM server</summary>
/// <param name="majorVersion">Major version of protocol used</param>
/// <param name="minorVersion">Minor version of protocol used</param>
/// <returns>true if command and response was successful</returns>
public bool GetVersion(out int majorVersion, out int minorVersion)
{
string response;
if (SendCommandExpectCommandResponse("Version", out response))
{
var versionString = response;
versionString = versionString.Substring(11);
Version ver = new Version(versionString);
majorVersion = ver.Major;
minorVersion = ver.Minor;
return true;
}
majorVersion = 0;
minorVersion = 0;
return false;
}
/// <summary>Set what realtime server version QTM should provide</summary>
/// <param name="majorVersion">Major version of protocol used</param>
/// <param name="minorVersion">Minor version of protocol used</param>
/// <returns>true if command was successful</returns>
public bool SetVersion(int majorVersion, int minorVersion, out string response)
{
response = "";
if (majorVersion < 0 || majorVersion > Constants.MAJOR_VERSION || minorVersion < 0)
{
mErrorString = "Incorrect version of protocol";
return false;
}
if (!SendCommandExpectCommandResponse("Version " + majorVersion + "." + minorVersion, out response))
{
return false;
}
this.mMajorVersion = majorVersion;
this.mMinorVersion = minorVersion;
return true;
}
/// <summary>Ask QTM server what version is used.</summary>
/// <param name="version">what version server uses</param>
/// <returns>true if command was sent successfully</returns>
public bool GetQTMVersion(out string version)
{
string response;
if (SendCommandExpectCommandResponse("QTMVersion", out response))
{
version = response;
return true;
}
version = "";
return false;
}
/// <summary>Check license towards QTM Server</summary>
/// <param name="licenseCode">license code to check</param>
/// <returns>true if command was successfully sent AND License passed, otherwise false</returns>
public bool CheckLicense(string licenseCode)
{
string response;
if (SendCommandExpectCommandResponse("CheckLicense " + licenseCode, out response))
{
if (response == "License pass")
{
return true;
}
}
return false;
}
/// <summary>Stream all frames from QTM server</summary>
/// <returns>true if streaming started ok</returns>
public bool StreamAllFrames(ComponentType component, int port = -1, string ipAddress = "")
{
return StreamFrames(StreamRate.RateAllFrames, 1, component, port, ipAddress);
}
public bool StreamAllFrames(List<ComponentType> component, int port = -1, string ipAddress = "")
{
return StreamFrames(StreamRate.RateAllFrames, 1, component, port, ipAddress);
}
/// <summary>Stream frames from QTM server</summary>
/// <param name="streamRate">what rate server should stream at</param>
/// <param name="streamValue">related to streamrate, not used if all frames are streamed</param>
/// <param name="components">List of all component types deisred to stream</param>
/// <param name="udpPort">if set, streaming will be done by UDP on this port. Has to be set if ip address is specified</param>
/// <param name="ipAddress">if UDP streaming should occur to other ip address, if not set streaming occurs on same ip as command came from</param>
/// <returns>true if streaming started</returns>
public bool StreamFrames(StreamRate streamRate, int streamValue, List<ComponentType> components = null, int udpPort = -1, string ipAddress = "")
{
string command = "streamframes";
switch (streamRate)
{
case StreamRate.RateAllFrames:
command += " allframes";
break;
case StreamRate.RateFrequency:
command += " frequency:" + streamValue;
break;
case StreamRate.RateFrequencyDivisor:
command += " frequencydivisor:" + streamValue;
break;
}
if (ipAddress != "")
{
if (udpPort > 0)
{
command += " udp:" + ipAddress + ":" + udpPort;
}
else
{
mErrorString = "If an IP-address was specified for UDP streaming, a port must be specified as well";
return false;
}
}
else if (udpPort > 0)
{
command += " udp:" + udpPort;
}
command += BuildStreamString(components);
return SendString(command, PacketType.PacketCommand);
}
public bool StreamFrames(StreamRate streamRate, int streamValue, ComponentType component, int port = -1, string ipaddress = "")
{
List<ComponentType> list = new List<ComponentType>();
list.Add(component);
return StreamFrames(streamRate, streamValue, list, port, ipaddress);
}
/// <summary>Tell QTM Server to stop streaming frames</summary>
/// <returns>true if command was sent successfully</returns>
public bool StreamFramesStop()
{
return SendString("StreamFrames Stop", PacketType.PacketCommand);
}
/// <summary>Get latest event from QTM server</summary>
/// <param name="respondedEvent">even from qtm</param>
/// <returns>true if command was sent successfully</returns>
public bool GetState(out QTMEvent respondedEvent)
{
if (SendString("GetState", PacketType.PacketCommand))
{
ResponseType response;
PacketType packetType;
do
{
response = Receive(out packetType, false, 2000000);
if (response == ResponseType.success)
{
if (packetType == PacketType.PacketEvent)
{
respondedEvent = mPacket.GetEvent();
return true;
}
}
}
while (response == ResponseType.success);
}
respondedEvent = QTMEvent.None;
return false;
}
/// <summary>Send trigger to QTM server</summary>
/// <returns>True if command and trigger was received successfully</returns>
public bool SendTrigger()
{
string response;
if (SendCommandExpectCommandResponse("Trig", out response))
{
if (response == "Trig ok")
{
return true;
}
}
return false;
}
/// <summary>Set an event in QTM</summary>
/// <param name="label">label of event</param>
/// <returns>true if event was set successfully</returns>
public bool SetQTMEvent(string label)
{
string response;
if (SendCommandExpectCommandResponse("setQTMEvent " + label, out response))
{
if (response == "Event set")
{
return true;
}
}
return false;
}
/// <summary>Take control over QTM</summary>
/// <param name="password">Password set in client</param>
/// <returns>True if you become the master</returns>
public bool TakeControl(string password = "")
{
string response;
if (SendCommandExpectCommandResponse("TakeControl " + password, out response))
{
if (response == "You are now master")
{
return true;
}
else
{
mErrorString = response;
return false;
}
}
return false;
}
/// <summary>Release master control over QTM</summary>
/// <returns>true if control was released or if client already is a regular client</returns>
public bool ReleaseControl()
{
string response;
if (SendCommandExpectCommandResponse("releaseControl", out response))
{
if (response == "You are now a regular client" || response == "You are already a regular client")
{
return true;
}
}
return false;
}
/// <summary>Create a new measurement in QTM, connect to the cameras and enter RT (preview) mode. Needs to have control over QTM for this command to work</summary>
/// <returns>true if succeeded</returns>
public bool NewMeasurement()
{
string response;
if (SendCommandExpectCommandResponse("New", out response))
{
if (response == "Creating new connection" || response == "Already connected")
{
return true;
}
else
{
mErrorString = response;
return false;
}
}
return false;
}
/// <summary>Close the current measurement in QTM. Needs to have control over QTM for this command to work</summary>
/// <returns>Returns true if measurement was closed or if there was nothing to close</returns>
public bool CloseMeasurement()
{
string response;
if (SendCommandExpectCommandResponse("Close", out response))
{
if (response == "Closing connection" ||
response == "No connection to close" ||
response == "Closing file" ||
response == "File closed")
{
return true;
}
}
return false;
}
/// <summary>Start capture in QTM. Needs to have control over QTM for this command to work</summary>
/// <returns>true if measurement was started</returns>
public bool StartCapture(bool RTFromFile = false)
{
string response;
string command = (RTFromFile) ? "Start rtfromfile" : "Start";
if (SendCommandExpectCommandResponse(command, out response))
{
if (response == "Starting measurement")
{
return true;
}
}
return false;
}
/// <summary>Stop current measurement in QTM. Needs to have control over QTM for this command to work</summary>
/// <returns>true if measurement was stopped</returns>
public bool StopCapture()
{
string response;
if (SendCommandExpectCommandResponse("Stop", out response))
{
if (response == "Stopping measurement")
{
return true;
}
}
return false;
}
/// <summary>Load file, both relative and absolute path works. Needs to have control over QTM for this command to work</summary>
/// <param name="filename">filename to load</param>
/// <returns>true if measurement was loaded</returns>
public bool LoadFile(string filename)
{
string response;
if (SendCommandExpectCommandResponse("Load " + filename, out response))
{
if (response == "Measurement loaded")
{
return true;
}
}
return false;
}
/// <summary>Save current capture. Needs to have control over QTM for this command to work</summary>
/// <param name="filename">filename to save at.</param>
/// <param name="overwrite">if QTM is allowed to override existing files.</param>
/// <param name="newFilename">if QTM is not allowed to overwrite, the new filename will be sent back </param>
/// <returns>true if measurement was saved.</returns>
public bool SaveFile(string filename, bool overwrite, ref string newFilename)
{
string command = "Save " + filename;
if (overwrite)
{
command += " overwrite";
}
string response;
if (SendCommandExpectCommandResponse(command, out response))
{
if (response.Contains("Measurement saved"))
{
if (response.Contains("Measurement saved"))
{
Regex pattern = new Regex("'.*'$");
Match match = pattern.Match(response);
newFilename = match.Value.Replace("'", "");
}
return true;
}
}
return false;
}
/// <summary>Load project. Needs to have control over QTM for this command to work</summary>
/// <param name="projectPath">path to project to load</param>
/// <returns>true if project was loaded</returns>
public bool LoadProject(string projectPath)
{
string response;
if (SendCommandExpectCommandResponse("LoadProject " + projectPath, out response))
{
if (response.Contains("Project loaded"))
{
return true;
}
}
return false;
}
/// <summary>Get general settings from QTM Server and saves data in protocol</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool GetGeneralSettings()
{
return GetSettings("General", "General", out mGeneralSettings);
}
/// <summary>Get 3D settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool Get3dSettings()
{
return GetSettings("3D", "The_3D", out m3DSettings);
}
/// <summary>Get 6DOF settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool Get6dSettings()
{
return GetSettings("6D", "The_6D", out m6DOFSettings);
}
/// <summary>Get Analog settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool GetAnalogSettings()
{
return GetSettings("Analog", "Analog", out mAnalogSettings);
}
/// <summary>Get Force settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool GetForceSettings()
{
return GetSettings("Force", "Force", out mForceSettings);
}
/// <summary>Get Image settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool GetImageSettings()
{
return GetSettings("Image", "Image", out mImageSettings);
}
/// <summary>Get Gaze vector settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool GetGazeVectorSettings()
{
return GetSettings("GazeVector", "Gaze_Vector", out mGazeVectorSettings);
}
/// <summary>Get eye tracker settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool GetEyeTrackerSettings()
{
return GetSettings("EyeTracker", "Eye_Tracker", out mEyeTrackerSettings);
}
private void SetDegreeOfFreedomFromConstraint(DegreeOfFreedom dof)
{
if (dof != null && dof.Constraint != null)
{
dof.LowerBound = dof.Constraint.LowerBound;
dof.UpperBound = dof.Constraint.UpperBound;
}
}
private void SetConstraintFromDegreeOfFreedom(DegreeOfFreedom dof)
{
if (dof != null)
{
dof.Constraint = new Constraint { LowerBound = dof.LowerBound, UpperBound = dof.UpperBound };
}
}
/// <summary>Get Skeleton settings from QTM Server</summary>
/// <returns>Returns true if settings was retrieved</returns>
public bool GetSkeletonSettings()
{
if (mMajorVersion == 1 && mMinorVersion < 21)
{
return GetSettings("Skeleton", "Skeletons", out mSkeletonSettings);
}
else
{
if (GetSettings("Skeleton", "Skeletons", out mSkeletonSettingsHierarchical))
{
mSkeletonSettings = new SettingsSkeletons();
mSkeletonSettings.Xml = mSkeletonSettingsHierarchical.Xml;