-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCSIRORemoteControlServer.cs
1813 lines (1610 loc) · 58.5 KB
/
CSIRORemoteControlServer.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
// RegisterSystemAssembly: System.dll
// RegisterSystemAssembly: System.Core.dll
// RegisterSystemAssembly: System.Drawing.dll
// RegisterSystemAssembly: System.XML.dll
// RegisterSystemAssembly: System.XML.Linq.dll
// RegisterAssembly: DefinitionsCore.dll
// RegisterAssembly: BasicHardware.dll
// RegisterAssembly: PluginManager.dll
// RegisterAssembly: HardwareClient.dll
// RegisterAssembly: SpectrumLibrary.dll
// RegisterAssembly: Util.dll
/*
Copyright 2011 Jake Ross
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
//TODO: Check that all strings which use {0} also have the format command
__version__=2.1.1
Modified by Axel Suckow, Alec Deslandes, Christoph.Gerber (2019)
*/
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using Thermo.Imhotep.BasicHardware;
using Thermo.Imhotep.Definitions.Core;
using Thermo.Imhotep.SpectrumLibrary;
using Thermo.Imhotep.Util;
using System.Drawing;
using System.Linq;
using Thermo.Imhotep.Util.SpreadSheetML;
class CSIRORemoteControl
{
// peak center setting variables
private static double PCSetMassRangePercent=1.5;
private static double PCSetValidIntensity=50;
private static double PCSetUserPeakOffset;
private static double PCPeakCenterResultMassOffset;
InsertScript: Scripts\SampleFunctionIncludeScript.cs
private static int m_PORT = 1069;
private static Socket UDP_SOCK;
private static Thread SERVER_THREAD;
private static TcpListener TCPLISTENER;
private static bool USE_UDP=false;
private static bool USE_BEAM_BLANK=true;
private static bool peakCenterSuccessFull=false;
private static bool MAGNET_MOVING=false;
private static double MAGNET_MOVE_THRESHOLD=0.25;// threshold to trigger stepped magnet move in DAC units
private static int MAGNET_STEPS=20; // number of steps to divide the total magnet displacement
public static int MAGNET_STEP_TIME=100; //ms to delay between magnet steps
private static double MAGNET_MOVE_THRESHOLD_REL_MASS = 0.4; //relative mass threshold to trigger stepped magnet move
private static double LAST_Y_SYMMETRY=0;
private static bool IsBLANKED=false;
private static Dictionary<string, double> m_defl = new Dictionary<string, double>();
private static LogLevel SCRIPT_ERROR_LOG_LEVEL = LogLevel.UserInfo; //Determines on what level script errors are logged, typically LogLevel.UserError or LogLevel.UserInfo
public const int ON = 1;
public const int OFF = 0;
public const int OPEN = 1;
public const int CLOSE = 0;
public static IRMSBaseCore Instrument;
public static IRMSBaseMeasurementInfo m_restoreMeasurementInfo;
public static bool CDDS_ARE_ON = false; // keeps track of CDD state as long as this is done through "ActivateAllCDDs" and "DeactivateAllCDDs"
public static string SCAN_DATA;
public static object m_lock=new object();
public static int commandCount = 0;
private static List<string> DETECTOR_NAMES = new List<string>(new string[]{"CUP 4,H2",
"CUP 3,H1",
"CUP 2,AX",
"CUP 1,L1",
"CUP 0,L2",
"CDD 0,L2 (CDD)",
"CDD 1,L1 (CDD)",
"CDD 2,AX (CDD)",
"CDD 3,H1 (CDD)",
"CDD 4,H2 (CDD)"
});
public static void Main ()
{
Instrument= HelixMC;
// // Prints all properties and methods of PeakCenterSetting
// IRMSBaseCupConfigurationData activeCupData = Instrument.CupConfigurationDataList.GetActiveCupConfiguration();
// IRMSBasePeakCenterSetting pcSetting = activeCupData.PeakCenterSetting;
// log(typeof(IRMSBasePeakCenterSetting).ToString());
// log(typeof(IRMSBasePeakCenterSetting).GetFields().ToString());
// log("Properties of PeakCenterSetting:");
// PropertyInfo[] piArray = typeof(IRMSBasePeakCenterSetting).GetProperties();
// log(piArray.Length.ToString());
// foreach (PropertyInfo pi in piArray)
// {
// log(pi.Name.ToString());
// }
// log("Methods of PeakCenterSetting:");
// MethodInfo[] miArray = typeof(IRMSBasePeakCenterSetting).GetMethods();
// log(miArray.Length.ToString());
// foreach (MethodInfo mi in miArray)
// {
// log(mi.Name.ToString());
// }
//init parameters
Instrument.GetParameter("Y-Symmetry Set", out LAST_Y_SYMMETRY);
GetTuningSettings();
PrepareEnvironment();
//mReportGains();
//list attributes
//listattributes(Instrument.GetType(), "instrument");
//setup data recording
InitializeDataRecord();
if (USE_UDP)
{
UDPServeForever();
}
else
{
TCPServeForever();
}
}
//====================================================================================================================================
// Last Update: 18 June 2019
//
// Commands are case sensitive and in CamelCase.
// Inputs are given in <> (do not include the "<" or ">" in the commands).
// e.g SetTrapVoltage 120 not SetTrapVoltage <120>
// Output is generally a short string stating whether the command was
// executed successfully (e.g. "OK", "Error: TrapVoltage could not be set")
// More complex output is indicated with a -> sign
//
// Commands that have been tested and found to work at CSIRO are marked
// with an @.
//
// Commands:
// GetParameters
// GetTuningSettingsList -> (a comma separated string)
// SetTuningSettings
// @GetData -> (tagged data e.g. H2,mass,intensity,L1 (CDD),mass,intensity,...)
// @SetIntegrationTime <seconds>
// @GetIntegrationTime -> seconds
// BlankBeam <true or false> if true set y-symmetry to -50 else return to previous value
//===========Cup/SubCup Configurations==============================
// GetCupConfigurationList
// GetSubCupConfigurationList
// GetActiveCupConfiguration
// GetActiveSubCupConfiguration
// GetSubCupParameters -> (list of Deflection voltages and the Ion Counter supply voltage)
// SetSubCupConfiguration <sub cup name>
// @ActivateCupConfiguration <cup name>:<sub cup name>
//
//===========Ion Pump Valve=========================================
// @Open #opens the Ion pump to the mass spec
// @Close #closes the Ion pump to the mass spec
// GetValveState #returns True for open false for closed
//===========Magnet=================================================
// GetMagnetDAC
// SetMagnetDAC <value> #0-10V
// GetMagnetMoving
// @SetMass <value>,<string cupName> #deactivates the CDDs if they are active
// @GetMass -> Mass on the AX cup
//===========Source=================================================
// GetHighVoltage or GetHV
// SetHighVoltage or SetHV <kV>
// GetTrapVoltage
// SetTrapVoltage <value>
// GetElectronEnergy
// SetElectronEnergy <value>
// GetIonRepeller
// SetIonRepeller <value>
// GetYSymmetry
// SetYSymmetry <value>
// GetZSymmetry
// SetZSymmetry <value>
// GetZFocus
// SetZFocus <value>
// GetExtractionFocus
// SetExtractionFocus <value>
// GetExtractionSymmetry
// SetExtractionSymmetry <value>
// GetExtractionLens
// SetExtractionLens <value>
//==========Detectors===============================================
// @ActivateAllCDDs #activates and unprotects all CDDs with a mass in the CupConfiguration
// @DeactivateAllCDDs #deactivates and protects all CDDs
// @ActivateCDD <name>
// ProtectDetector <name>,<On/Off>
// GetDeflections <name>[,<name>,...]
// GetDeflection <name>
// SetDeflection <name>,<value>
// GetCDDVoltage
// SetCDDVoltage <value>
// GetGain <name>
// SetGain <name>,<value>
//
//==========Operations=================================================
// @PeakCenter <cupName>,<peakMass>[,<setMass>] #the last is optional, defines mass at which scan is continued after the peak center
// PeakCenterGetResult
//
//==========Generic Device===============================================
// GetParameters <name>[,<name>,...]
// GetParameter <name> - name is any valid device currently listed in the hardware database
// SetParameter <name>,<value> - name is any valid device currently listed in the hardware database
//
//==================================================================
// Error Responses:
// Error: Invalid Command - the command is poorly formated or does not exist.
// Error: could not set <hardware> to <value>
//
//====================================================================================================================================
private static string ParseAndExecuteCommand (string cmd)
{
commandCount = commandCount + 1;
log(commandCount.ToString());
string result = "Error: Invalid Command";
bool ParResult = false;
Logger.Log(LogLevel.UserInfo, String.Format("{0}: Executing {1}",commandCount.ToString(), cmd));
string[] args = cmd.Trim().Split (' ');
string[] pargs;
string jargs;
double r;
switch (args[0]) {
case "GetTuningSettingsList":
result = GetTuningSettings();
break;
case "SetTuningSettings":
if(SetTuningSettings(args[1]))
{
result="OK";
}
else
{
result=String.Format("Error: could not set tuning settings {0}",args[1]);
};
break;
case "GetData":
result=SCAN_DATA;
break;
case "SetIntegrationTime":
result=SetIntegrationTime(Convert.ToDouble(args[1]));
break;
case "PeakCenterSetMassRangePercent":
PCSetMassRangePercent=Convert.ToDouble(args[1]);
Instrument.PeakCenterSetting = Instrument.CupConfigurationDataList.GetActiveCupConfiguration().PeakCenterSetting;
Instrument.PeakCenterSetting.MassRangePercent = PCSetMassRangePercent;
result="OK";
break;
case "PeakCenterSetValidIntensity":
PCSetValidIntensity=Convert.ToDouble(args[1]);
Instrument.PeakCenterSetting = Instrument.CupConfigurationDataList.GetActiveCupConfiguration().PeakCenterSetting;
Instrument.PeakCenterSetting.ValidIntensity = PCSetValidIntensity;
result="OK";
break;
case "PeakCenterGetUserOffset":
result=Convert.ToString(PCSetUserPeakOffset);;
break;
case "PeakCenterGetResultOffset":
result=Convert.ToString(PCPeakCenterResultMassOffset);;
break;
case "GetIntegrationTime":
result=GetIntegrationTime();
break;
case "BlankBeam":
ParResult = SwitchBlankBeam(args[1].ToLower());
if(ParResult)
{ result="OK"; }
else
{ result="Error: could not blank the beam";}
break;
//============================================================================================
// Cup / SubCup Configurations
//============================================================================================
case "GetCupConfigurationList":
List<string> cup_names = GetCupConfigurations ();
result = string.Join ("\r", cup_names.ToArray ());
break;
case "GetSubCupConfigurationList":
string config_name = args[1];
List<string> sub_names = GetSubCupConfigurations (config_name);
result = string.Join ("\r", sub_names.ToArray ());
break;
case "GetActiveCupConfiguration":
result=Instrument.CupConfigurationDataList.GetActiveCupConfiguration().Name;
break;
case "GetActiveSubCupConfiguration":
result=Instrument.CupConfigurationDataList.GetActiveSubCupConfiguration().Name;
break;
case "GetSubCupParameters":
result=GetSubCupParameters();
break;
case "SetSubCupConfiguration":
// Logger.Log(LogLevel.Debug, String.Format("Set SupCup {0}",cmd));
// if(ActivateCupConfiguration ("Argon", cmd.Remove(0,23)))
// {
// result="OK";
// }
// else
// {
// result=String.Format("Error: could not set sub cup to {0}", args[1]);
// }
// break;
jargs=Instrument.CupConfigurationDataList.GetActiveCupConfiguration().Name;
result=mActivateCup(jargs, args[1]);
break;
case "ActivateCupConfiguration":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(':');
result = mActivateCup(pargs[0], pargs[1]);
break;
//============================================================================================
// Ion Pump Valve
//============================================================================================
case "Open":
// jargs=String.Join(" ", Slice(args,1,0));
// ParResult=SetParameter(jargs,OPEN);
if (SetParameter("Valve Ion Pump Set", 1))
{ result="Valve opened"; }
else
{ result=String.Format("Error: could not open the valve"); }
break;
case "Close":
// jargs=String.Join(" ", Slice(args,1,0));
// ParResult=SetParameter(jargs,CLOSE);
if (SetParameter("Valve Ion Pump Set", 0))
{ result="Valve closed"; }
else
{ result=String.Format("Error: could not close the valve"); }
break;
case "GetValveState":
result=GetValveState("Valve Ion Pump Set");
break;
//============================================================================================
// Magnet
//============================================================================================
case "GetMagnetDAC":
if (Instrument.GetParameter("Field Set", out r))
{
result=r.ToString();
}
break;
case "SetMagnetDAC":
result=SetMagnetDAC(Convert.ToDouble(args[1]));
break;
case "GetMagnetMoving":
result=GetMagnetMoving();
break;
case "SetMass":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
result = SetMass(Convert.ToDouble(pargs[0]),(pargs.Length>1) ? pargs[1] : null);
break;
case "GetMass":
result = HelixMC.MeasurementInfo.ActualScanMass.ToString();
break;
//============================================================================================
// Source Parameters
//============================================================================================
case "GetHighVoltage":
if(Instrument.GetParameter("Acceleration Reference Set",out r))
{
result=(r*1000).ToString();
}
break;
case "SetHighVoltage":
ParResult=SetParameter("Acceleration Reference Set", Convert.ToDouble(args[1])/1000.0);
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set high voltage"); }
break;
case "GetHV":
if(Instrument.GetParameter("Acceleration Reference Set",out r))
{
result=(r).ToString();
}
break;
case "SetHV":
ParResult=SetParameter("Acceleration Reference Set", Convert.ToDouble(args[1])/1000.0);
break;
case "GetTrapVoltage":
if(Instrument.GetParameter("Trap Voltage Readback",out r))
{
result=r.ToString();
}
break;
case "SetTrapVoltage":
ParResult=SetParameter("Trap Voltage Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Trap Voltage"); }
break;
case "GetElectronEnergy":
if(Instrument.GetParameter("Electron Energy Readback",out r))
{
result=r.ToString();
}
break;
case "SetElectronEnergy":
ParResult=SetParameter("Electron Energy Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Electron Energy"); }
break;
case "GetIonRepeller":
if(Instrument.GetParameter("Ion Repeller Set",out r))
{
result=r.ToString();
}
break;
case "SetIonRepeller":
ParResult=SetParameter("Ion Repeller Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Ion Repeller"); }
break;
case "GetYSymmetry":
if(Instrument.GetParameter("Y-Symmetry Set",out r))
{
result=r.ToString();
}
break;
case "SetYSymmetry":
LAST_Y_SYMMETRY=Convert.ToDouble(args[1]);
ParResult=SetParameter("Y-Symmetry Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Y Symmetry"); }
break;
case "GetZSymmetry":
if(Instrument.GetParameter("Z-Symmetry Set",out r))
{
result=r.ToString();
}
break;
case "SetZSymmetry":
ParResult=SetParameter("Z-Symmetry Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Z Symmetry"); }
break;
case "GetZFocus":
if(Instrument.GetParameter("Z-Focus Set",out r))
{
result=r.ToString();
}
break;
case "SetZFocus":
ParResult=SetParameter("Z-Focus Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Z Focus"); }
break;
case "GetExtractionFocus":
if(Instrument.GetParameter("Extraction Focus Set",out r))
{
result=r.ToString();
}
break;
case "SetExtractionFocus":
ParResult=SetParameter("Extraction Focus Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Extraction Focus"); }
break;
case "GetExtractionSymmetry":
if(Instrument.GetParameter("Extraction Symmetry Set",out r))
{
result=r.ToString();
}
break;
case "SetExtractionSymmetry":
ParResult=SetParameter("Extraction Symmetry Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Extraction Symmetry"); }
break;
case "GetExtractionLens":
if(Instrument.GetParameter("Extraction Lens Set",out r))
{
result=r.ToString();
}
break;
case "SetExtractionLens":
ParResult=SetParameter("Extraction Lens Set",Convert.ToDouble(args[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Extraction Lens"); }
break;
//============================================================================================
// Detectors
//============================================================================================
case "ActivateAllCDDs":
result = ActivateAllCDDs();
break;
case "DeactivateAllCDDs":
result = DeactivateAllCDDs();
break;
case "ActivateCDD":
jargs=String.Join(" ", Slice(args,1,0));
result = ActivateCDD(jargs);
break;
case "ProtectDetector":
// jargs=String.Join(" ", Slice(args,1,0));
// pargs=args[1].Split(',');
// pargs=jargs.Split(',');
// jargs=String.Join(" ", Slice(args,1,0));
pargs=args[1].Split(',');
ProtectDetector(pargs[0], pargs[1]);
result="OK";
break;
case "GetDeflections":
result = GetDeflections(args);
break;
case "GetDeflection":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
if(Instrument.GetParameter(String.Format("Deflection {0} Set",pargs[0]), out r))
{
result=r.ToString();
}
break;
case "SetDeflection":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
ParResult=SetParameter(String.Format("Deflection {0} Set",pargs[0]),Convert.ToDouble(pargs[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not Set Deflection"); }
break;
case "GetCDDVoltage":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
if(Instrument.GetParameter(String.Format("{0} Supply Set",pargs[0]), out r))
{
result=r.ToString();
}
break;
case "SetCDDVoltage":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
jargs=pargs[0] + " Supply Set";
ParResult=SetParameter(jargs, Convert.ToDouble(pargs[1]));
if (ParResult)
{ result="OK"; }
else
{ result=String.Format("Error: could not set Ion Counter Voltage"); }
break;
case "GetGain":
jargs=String.Join(" ", Slice(args,1,0));
result=GetGain(jargs);
break;
case "SetGain":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
result="OK";
SetGain(pargs[0], Convert.ToDouble(pargs[1]));
break;
//============================================================================================
// Operations
//============================================================================================
case "PeakCenter":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
result="Wait For Peak Center!";
if (!RunPeakCentering(pargs[0],Convert.ToDouble(pargs[1]),(pargs.Length>2) ? Convert.ToDouble(pargs[2]) : Convert.ToDouble(pargs[1])))
{
result = "Error: Could not do peak center.";
peakCenterSuccessFull = false;
break;
}
peakCenterSuccessFull = true;
result = "OK";
break;
case "PeakCenterGetResult":
result=peakCenterSuccessFull.ToString();
break;
//============================================================================================
// Generic
//============================================================================================
case "GetParameters":
result = GetParameters(args);
break;
case "GetParameter":
jargs=String.Join(" ", Slice(args,1,0));
if(Instrument.GetParameter(jargs, out r))
{
result=r.ToString();
}
break;
case "SetParameter":
jargs=String.Join(" ", Slice(args,1,0));
pargs=jargs.Split(',');
ParResult=SetParameter(pargs[0], Convert.ToDouble(pargs[1]));
break;
}
log(String.Format("{0} => {1}", cmd, result));
return result;
}
//============================================================================================
// EOCommands
//============================================================================================
private static bool PrepareEnvironment()
{
m_restoreMeasurementInfo=Instrument.MeasurementInfo;
return Instrument.ScanTransitionController.InitializeScriptScan(m_restoreMeasurementInfo);
}
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Prepares the environment for a peak center by 1) turning off the CDDs, 2)
/// changing the mass, 3) turning the CDDs back on if peak center is run on
/// a CDD.
/// </summary>
/// <param name="cupName">The cup configuration name.</param>
/// <param name="cupMass">The the mass for which peak centering is done.</param>
/// <returns><c>True</c> if successfull; otherwise <c>false</c>.</returns>
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
private static bool PrepareEnvironmentForPeakCenter(string cupName, double cupMass)
{
Logger.Log(LogLevel.UserInfo,"Preparing environment for a peak center...");
if (SetMass(cupMass,cupName)!= "OK") //Note: SetMass automatically deactivates CDDs
{
Logger.Log(SCRIPT_ERROR_LOG_LEVEL, "Error: could not set mass to desired value. Aborting peak center...");
return false;
}
if (IsCDD(cupName)) // Activate CDDs if needed (i.e. if peak center was requested on a CDD)
{
if (ActivateAllCDDs()!= "OK")
{
Logger.Log(SCRIPT_ERROR_LOG_LEVEL, "Error: could not reactivate CDDs. Aborting peak center...");
return false;
}
}
m_restoreMeasurementInfo=Instrument.MeasurementInfo;
return Instrument.ScanTransitionController.InitializeScriptScan(m_restoreMeasurementInfo);
}
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Determines whether a given cup is a CDD
/// </summary>
/// <param name="cupName">The cup configuration name.</param>
/// <returns><c>True</c> if the cup is a CDD; otherwise <c>false</c>.</returns>
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
private static bool IsCDD(string cupName)
{
bool result = false;
IRMSBaseCupConfigurationData activeCupData = Instrument.CupConfigurationDataList.GetActiveCupConfiguration();
foreach(IRMSBaseCollectorItem col in activeCupData.CollectorItemList)
{
if ((col.CollectorType == IRMSBaseCollectorType.CounterCup) && (col.Appearance.Label == cupName))
{
result = true;
}
}
return result;
}
public static void InitializeDataRecord()
{
// attach a handler to the ScanDataAvailable Event
Instrument.ScanDataAvailable+=ScanDataAvailable;
}
public static void Dispose()
{
Logger.Log (LogLevel.UserInfo, "Stop Server");
// deattach the handler from the ScanDataAvailable Event
Instrument.ScanDataAvailable-=ScanDataAvailable;
//shutdown the server
if (USE_UDP)
{
UDP_SOCK.Close();
}
else
{
TCPLISTENER.Stop();
}
SERVER_THREAD.Abort();
}
//====================================================================================================================================
//Qtegra Methods
//====================================================================================================================================
public static string GetDeflections(string[] args)
{
List<string> data = new List<string>();
double v;
string dets = String.Join(" ", Slice(args,1,0));
foreach(string k in dets.Split(','))
{
if(!Instrument.GetParameter(String.Format("Deflection {0} Set", k), out v))
{
v = 0;
}
data.Add(v.ToString());
}
return string.Join(",",data.ToArray());
}
public static string GetParameters(string[] args)
{
List<string> data = new List<string>();
double v;
string param;
foreach(string k in args[1].Split(','))
{
param="";
switch (k) {
case "YSymmetry":
param="Y-Symmetry Set";
break;
case "ZSymmetry":
param="Extraction-Symmetry Set";
break;
case "HighVoltage":
param="Acceleration Reference Set";
break;
case "HV":
param="Acceleration Reference Set";
break;
case "TrapVoltage":
param="Trap Voltage Readback";
break;
case "ElectronEnergy":
param="Electron Energy Readback";
break;
case "ZFocus":
param="Extraction-Focus Set";
break;
case "IonRepeller":
param="Ion Repeller Set";
break;
case "ExtractionFocus":
param="Extraction Focus Set";
break;
case "ExtractionSymmetry":
param="Extraction Symmetry Set";
break;
case "ExtractionLens":
param="Extraction Lens Set";
break;
}
//log(param);
//log(k);
if (param != "")
{
Instrument.GetParameter(param, out v);
}
else
{
v = 0;
}
data.Add(v.ToString());
}
return string.Join(",",data.ToArray());
}
public static bool SwitchBlankBeam(string switchOn)
{
if (!USE_BEAM_BLANK)
{
return true;
}
double yval=LAST_Y_SYMMETRY;
bool blankbeam=false;
if (switchOn=="true")
{
if(!IsBLANKED)
{
//remember the non blanking Y-Symmetry value
Instrument.GetParameter("Y-Symmetry Set", out LAST_Y_SYMMETRY);
yval=-50;
IsBLANKED=true;
blankbeam=true;
}
}
else
{
if(IsBLANKED)
{
IsBLANKED=false;
blankbeam=true;
if(!SetParameter("Y-Symmetry Set",yval))
{return false;}
}
}
if(blankbeam)
{
if(!SetParameter("Y-Symmetry Set",yval))
{return false;}
};
return true;
}
public static string mActivateCup(string a, string b)
{
string result;
if(ActivateCupConfiguration(a, b))
{
result="OK";
}
else
{
result=String.Format("Error: could not set cup={0}, sub cup to {1} ", a, b);
}
return result;
}
public static string GetMagnetMoving()
{
if (MAGNET_MOVING)
{
return "True";
}
else
{
return "False";
}
}
public static void ProtectDetector(string detname, string state)
{
string param=String.Format("Deflection {0} CDD Set",detname);
if (state.ToLower()=="on")
{
double v;
Instrument.GetParameter(param, out v);
m_defl[detname]=v;
SetParameter(param, 3250);
}
else
{
if (m_defl.ContainsKey(detname))
{
SetParameter(param, m_defl[detname]);
}
else
{
SetParameter(param, 0);
}
}
}
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Activate all CDD cups with a mass in the cupconfiguration. After the
/// activation, a monitor scan is resumed at the same mass as before.
/// </summary>
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
private static string ActivateAllCDDs()
{
// activate the CDDs...
if (!ActivateAllCounterCups(true))
{
return "Could not activate CDDs.";
}
Thread.Sleep(1000); // Wait for a little time for the activation to take effect...
// Restart the monitor scan, to ensure data is reported for the CDD...
if (!RunMonitorScan(HelixMC.MeasurementInfo.ActualScanMass))
{
return "Could not restart monitor scan.";
}
return "OK";
}
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Deactivate all CDD cups with a mass in the cupconfiguration. The
/// monitor scan is then resumed at the actual mass.
/// </summary>
///-------------------------------------------------------------------------------------------------------------------------------------------------------------
private static string DeactivateAllCDDs()
{
if (!ActivateAllCounterCups(false))
{
return "Could not deactivate CDDs.";
}
//This ensures that after the deactivation, data is not reported for the CDD...