-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRig.cs
2022 lines (1784 loc) · 55.2 KB
/
Rig.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
//
// Rig.cs
//
// Author:
// Jae Stutzman <jaebird@gmail.com>
//
// Copyright (c) 2016 Jae Stutzman
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Runtime.InteropServices;
using HamLibSharp.Utils;
using HamLibSharp.x86;
using HamLibSharp.x64;
namespace HamLibSharp
{
internal interface INativeRig
{
IntPtr Caps { get; }
IRigStateNative State { get; }
IntPtr Callbacks { get; }
};
public partial class Rig : IDisposable
{
const int CommErrors = 10;
// milliseconds
const int UpdateRate = 250;
const string ConfTokenDevice = "rig_pathname";
const string ConfTokenSerialSpeed = "serial_speed";
const string ConfTokenDatabits = "data_bits";
const string ConfTokenStopbits = "stop_bits";
const string ConfTokenSerialParity = "serial_parity";
const string ConfTokenSerialHandshake = "serial_handshake";
const string ConfTokenDataBits = "data_bits";
IntPtr theRig;
INativeRig nativeRig;
RigCaps rigCaps;
public RigCaps Caps { get { return rigCaps; } }
bool disposed;
int errorCount;
BlockingCollection<Action> taskQueue;
System.Timers.Timer timer;
Thread thread;
int updateRate;
volatile bool commErrorClose = false;
public event EventHandler CommErrorClose;
/// <summary>
/// Gets the unique model identifier
/// </summary>
/// <value>The model id.</value>
public int Model {
get {
return rigCaps.RigModel;
}
}
/// <summary>
/// Gets the name of the manufacturer.
/// </summary>
/// <value>The name of the manufacturer.</value>
public string MfgName {
get {
return rigCaps.MfgName;
}
}
/// <summary>
/// Gets the name of the model.
/// </summary>
/// <value>The name of the model.</value>
public string ModelName {
get {
return rigCaps.ModelName;
}
}
/// <summary>
/// Gets the version of the backend.
/// </summary>
/// <value>The version.</value>
public string Version {
get {
return rigCaps.Version;
}
}
/// <summary>
/// Gets the max serial baud rate.
/// </summary>
/// <value>The serial rate.</value>
public int SerialRateMax {
get {
return rigCaps.SerialRateMax;
}
}
/// <summary>
/// Gets the min serial baud rate.
/// </summary>
/// <value>The serial rate.</value>
public int SerialRateMin {
get {
return rigCaps.SerialRateMin;
}
}
/// <summary>
/// Gets the current serial baud rate.
/// </summary>
/// <value>The serial rate.</value>
public int SerialRate {
get {
if (rigOpen) {
return int.Parse (GetConf (ConfTokenSerialSpeed));
} else {
return 0;
}
}
}
/// <summary>
/// Gets the rig path (i.e. serial port).
/// </summary>
/// <value>The rig path.</value>
public string RigPath {
get {
if (rigOpen) {
return GetConf (ConfTokenDevice);
} else {
return string.Empty;
}
}
}
double freq;
/// <summary>
/// Gets the current frequency of the rig.
/// Last updated by call to UpdateFrequency().
/// </summary>
/// <value>The frequency in Hz.</value>
public double Freq {
get {
if (updateRate > 0) {
lock (this) {
return freq;
}
} else {
return GetFrequency ();
}
}
private set {
lock (this) {
freq = value;
}
}
}
long width;
RigMode mode;
public RigMode Mode {
get {
if (updateRate > 0) {
lock (this) {
return mode;
}
} else {
return GetMode (ref width);
}
}
private set {
lock (this) {
mode = value;
}
}
}
public string ModeText {
get {
return TextNameAttribute.GetTextName (Mode);
}
}
volatile PttMode ptt;
/// <summary>
/// Gets the Push-To-Talk status.
/// Last updated by call to UpdatePtt().
/// </summary>
/// <value>The PTT status.</value>
public PttMode Ptt {
get {
if (updateRate > 0) {
lock (this) {
return ptt;
}
} else {
return GetPtt ();
}
}
private set {
lock (this) {
ptt = value;
}
}
}
volatile string lastStatus;
/// <summary>
/// Gets the last status.
/// </summary>
/// <value>The last status.</value>
public string LastStatus {
get {
lock (this) {
return lastStatus;
}
}
private set {
lock (this) {
lastStatus = value;
}
}
}
volatile bool rigOpen;
/// <summary>
/// Gets a value indicating whether this <see cref="HamLibSharp.Rig"/> rig open.
/// </summary>
/// <value><c>true</c> if rig open; otherwise, <c>false</c>.</value>
public bool RigOpen {
get {
lock (this) {
return rigOpen;
}
}
private set {
lock (this) {
rigOpen = value;
}
}
}
public RigMode ModeList {
get {
return nativeRig.State.Mode_list;
}
}
public int VfoList {
get {
return nativeRig.State.Vfo_list;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="HamLibSharp.Rig"/> class.
/// This constructor attempts to find the correct backend based on input name.
/// </summary>
/// <param name="rigModel">Rig model name.</param>
public Rig (string rigModel, int updateRate = UpdateRate)
{
this.updateRate = updateRate < UpdateRate ? UpdateRate : updateRate;
if (!HamLib.Initialized) {
HamLib.Initialize ();
}
// Look up model from name
RigCaps rig;
if (!HamLib.Rigs.TryGetValue (rigModel, out rig)) {
throw new RigException ("Rig model not found");
}
// init the underlying rig and library
InitRig (rig.RigModel);
}
/// <summary>
/// Initializes a new instance of the <see cref="HamLibSharp.Rig"/> class.
/// This constructor initializing the rig backend based on input model id.
/// </summary>
/// <param name="rigModel">Rig model id.</param>
public Rig (int rigModel, int updateRate = UpdateRate)
{
this.updateRate = updateRate < UpdateRate ? UpdateRate : updateRate;
if (!HamLib.Initialized) {
HamLib.Initialize ();
}
InitRig (rigModel);
}
private void InitRig (int rigModel)
{
theRig = rig_init (rigModel);
if (theRig == IntPtr.Zero)
throw new RigException ("Rig initialization error");
nativeRig = MarshalNativeRig (theRig);
var caps = HamLib.MarshalRigCaps (nativeRig.Caps);
rigCaps = new RigCaps (caps, this);
// test...
//Console.WriteLine("TEST>>>>>>");
//Console.WriteLine (nativeRig.State.Itu_region);
//Console.WriteLine ("HamLibPortNative: {0}", Marshal.SizeOf<HamLibPortNative> ());
// end test....
}
internal static INativeRig MarshalNativeRig (IntPtr rig_ptr)
{
INativeRig rig = null;
switch (HamLib.hamLibVersion) {
case HamLibVersion.Current:
case HamLibVersion.V301:
// if the platform is 64-bit, but not windows
if (!HamLib.isWindows && HamLib.bitsize64) {
rig = Marshal.PtrToStructure<NativeRig64> (rig_ptr);
} else {
rig = Marshal.PtrToStructure<NativeRig32> (rig_ptr);
}
break;
case HamLibVersion.V2:
// if the platform is 64-bit, but not windows
if (!HamLib.isWindows && HamLib.bitsize64) {
rig = Marshal.PtrToStructure<NativeRig64v2> (rig_ptr);
} else {
rig = Marshal.PtrToStructure<NativeRig32v2> (rig_ptr);
}
break;
default:
throw new RigException ("Unknown or Incompatible HamLib library found");
}
return rig;
}
private static int OnFrequency (IntPtr theRig, int vfo, double freq, IntPtr rig_ptr)
{
// Console.WriteLine (freq);
return 1;
}
private void Dispose (bool disposing)
{
if (!this.disposed) {
if (disposing) {
// Dispose here any managed resources
}
if (theRig != IntPtr.Zero) {
Stop ();
Close ();
var ret = rig_cleanup (theRig);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
theRig = IntPtr.Zero;
rigCaps = null;
nativeRig = null;
}
}
disposed = true;
}
/// <summary>
/// Releases all resource used by the <see cref="HamLibSharp.Rig"/> object.
/// </summary>
/// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="HamLibSharp.Rig"/>. The
/// <see cref="Dispose"/> method leaves the <see cref="HamLibSharp.Rig"/> in an unusable state. After calling
/// <see cref="Dispose"/>, you must release all references to the <see cref="HamLibSharp.Rig"/> so the garbage
/// collector can reclaim the memory that the <see cref="HamLibSharp.Rig"/> was occupying.</remarks>
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
~Rig ()
{
Dispose (false);
}
/// <summary>
/// Opens communication to a radio. If it is a serial connected rig,
/// searches for the correct baud rate.
/// </summary>
/// <param name="device">Device path (i.e. serial port).</param>
public void Open (string devicePath)
{
if (rigCaps.PortType == RigPort.Serial) {
// find the baud...
var foundBaud = false;
foreach (int baud in Enum.GetValues(typeof(RigSerialBaudRate))) {
if (baud >= SerialRateMin && baud <= SerialRateMax) {
try {
SetConf (ConfTokenSerialSpeed, baud);
OpenInternal (devicePath);
// throws exception if IO error
GetPtt ();
foundBaud = true;
break;
} catch (RigException) {
Close ();
}
}
}
if (!foundBaud) {
throw new RigException ("Unable to communicate with rig");
}
} else {
OpenInternal (devicePath);
}
}
/// <summary>
/// Opens communication to a radio.
/// </summary>
/// <param name="device">Device path (i.e. serial port).</param>
/// <param name="baud">baud rate of serial port.</param>
public void Open (string device, RigSerialBaudRate baud, RigSerialHandshake handshake, int databits, int stopbits)
{
if (rigCaps.PortType == RigPort.Serial) {
SetConf (ConfTokenSerialSpeed, (int)baud);
SetConf (ConfTokenSerialHandshake, TextNameAttribute.GetTextName(handshake));
SetConf (ConfTokenDatabits, databits);
SetConf (ConfTokenStopbits, stopbits);
OpenInternal (device);
} else {
throw new RigException (string.Format ("Device is not serial, but {0}", rigCaps.PortType));
}
}
/// <summary>
/// Open communication to a radio
/// </summary>
public void Open ()
{
OpenInternal (null);
}
private void OpenInternal (string device)
{
if (device != null && device != string.Empty) {
SetConf (ConfTokenDevice, device);
}
var ret = rig_open (theRig);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
rigOpen = true;
// untested since I don't have a rig that supports this
ret = rig_set_freq_callback (theRig, OnFrequency, IntPtr.Zero);
if (ret != RigError.OK) {
LastStatus = ErrorString (ret);
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Closes communication to a radio that was previously open with Open().
/// </summary>
public void Close ()
{
if (rigOpen) {
var ret = rig_close (theRig);
if (ret != RigError.OK) {
LastStatus = ErrorString (ret);
throw new RigException (ErrorString (ret));
}
Freq = 0;
rigOpen = false;
}
}
/// <summary>
/// Start will create a timer and a thread to manage the invocation
/// of calls to the rig. This is useful when an application cannot
/// block on rig calls. If Start is not called, each call is
/// called syncronously.
/// The timer will begin calling UpdateFrequency() and UpdatePtt()
/// to update the state properties on a periodic basis.
/// </summary>
public void Start ()
{
commErrorClose = false;
errorCount = 0;
taskQueue = new BlockingCollection<Action> ();
lock (this) {
try {
Freq = GetFrequency (RigVfo.Current);
} catch (RigException ex) {
LastStatus = ex.Message;
Freq = 0;
}
}
if (updateRate > 0) {
timer = new System.Timers.Timer (updateRate);
timer.AutoReset = false;
timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
timer.Start ();
}
thread = new Thread (TaskThread);
thread.Start ();
}
void Timer_Elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
//logger.Debug ("Timer elapsed, Id: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
if (commErrorClose) {
Stop ();
Close ();
if (CommErrorClose != null) {
CommErrorClose (this, new EventArgs ());
}
} else {
UpdateFrequency (RigVfo.Current);
UpdateMode (RigVfo.Current);
UpdatePtt (RigVfo.Current);
timer.Enabled = true;
}
}
/// <summary>
/// Stop the thread and timer that was started by the
/// call to Start().
/// </summary>
public void Stop ()
{
if (timer != null) {
timer.Dispose ();
timer = null;
}
if (taskQueue != null) {
// this will cause thread to exit by throwing InvalidOperationException
taskQueue.CompleteAdding ();
// wait for taskQueue thread to exit
thread.Join ();
thread = null;
}
}
private void TaskThread ()
{
var running = true;
while (running) {
try {
taskQueue.Take ().Invoke ();
errorCount = 0;
LastStatus = string.Empty;
} catch (InvalidOperationException) {
running = false;
taskQueue = null;
} catch (RigException e) {
LastStatus = e.Message;
errorCount++;
}
if (errorCount > CommErrors) {
commErrorClose = true;
}
}
}
/// <summary>
/// Sets a radio configuration parameter.
/// </summary>
/// <param name="token">Parameter Token.</param>
/// <param name="val">Value to set.</param>
public void SetConf (int token, string val)
{
var ret = rig_set_conf (theRig, token, val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Sets a radio configuration parameter.
/// </summary>
/// <param name="token">Parameter Token.</param>
/// <param name="val">Value to set.</param>
public void SetConf (int token, int val)
{
var ret = rig_set_conf (theRig, token, val.ToString ());
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Sets a radio configuration parameter.
/// </summary>
/// <param name="name">Name of parameter.</param>
/// <param name="val">Value to set.</param>
public void SetConf (string name, string val)
{
var ret = rig_set_conf (theRig, TokenLookup (name), val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Sets a radio configuration parameter.
/// </summary>
/// <param name="name">Name of parameter.</param>
/// <param name="val">Value to set.</param>
public void SetConf (string name, int val)
{
var ret = rig_set_conf (theRig, TokenLookup (name), val.ToString ());
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Retrieves the value of a configuration parameter associated with token.
/// </summary>
/// <returns>The configuration parameter.</returns>
/// <param name="token">Parameter Token.</param>
public string GetConf (int token)
{
string val;
var ret = rig_get_conf (theRig, token, out val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
return val;
}
/// <summary>
/// Retrieves the value of a configuration parameter associated with token.
/// </summary>
/// <returns>The configuration parameter.</returns>
/// <param name="name">Name of parameter.</param>
public string GetConf (string name)
{
string val;
var ret = rig_get_conf (theRig, TokenLookup (name), out val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
return val;
}
/// <summary>
/// Simple lookup returning token id assicated with name.
/// </summary>
/// <returns>The token ID</returns>
/// <param name="name">The name of the configuration parameter.</param>
public int TokenLookup (string name)
{
return rig_token_lookup (theRig, name);
}
/// <summary>
/// Sets the frequency of the target VFO
/// </summary>
/// <param name="freq">Frequency in Hz.</param>
/// <param name="vfo">The target VFO.</param>
public void SetFrequency (double freq, int vfo = RigVfo.Current)
{
if (thread != null) {
taskQueue.Add (() => setfreq (vfo, freq));
} else {
setfreq (vfo, freq);
}
}
private void setfreq (int vfo, double freq)
{
var ret = rig_set_freq (theRig, vfo, freq);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Retrieves the updated frequency from the targeted VFO.
/// Will use Task Queue if Start() has been called.
/// </summary>
/// <param name="vfo">The target VFO.</param>
public void UpdateFrequency (int vfo = RigVfo.Current)
{
if (thread != null) {
taskQueue.Add (() => Freq = GetFrequency (vfo));
} else {
Freq = GetFrequency (vfo);
}
}
/// <summary>
/// Retrieves the updated frequency from the targeted VFO.
/// Blocks until data received.
/// </summary>
/// <returns>The frequency in Hz.</returns>
/// <param name="vfo">The target VFO.</param>
public double GetFrequency (int vfo = RigVfo.Current)
{
double freq;
var ret = rig_get_freq (theRig, vfo, out freq);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
return freq;
}
/// <summary>
/// Sets the mode and associated passband of the target VFO.
/// The passband width must be supported by the backend of the rig.
/// </summary>
/// <param name="mode">The mode to set to.</param>
/// <param name="width">The passband width to set to.</param>
/// <param name="vfo">The target VFO.</param>
public void SetMode (RigMode mode, long width, int vfo = RigVfo.Current)
{
if (thread != null) {
taskQueue.Add (() => setmode (mode, width, vfo));
} else {
setmode (mode, width, vfo);
}
}
private void setmode (RigMode mode, long width, int vfo = RigVfo.Current)
{
var ret = rig_set_mode (theRig, vfo, mode, width);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Retrieves the updated mode and passband of the target VFO.
/// Will use Task Queue if Start() has been called.
/// </summary>
/// <param name="vfo">The target VFO.</param>
public void UpdateMode (int vfo = RigVfo.Current)
{
if (thread != null) {
taskQueue.Add (() => Mode = GetMode(ref width, vfo));
} else {
Mode = GetMode(ref width, vfo);
}
}
/// <summary>
/// Retrieves the mode and passband of the target VFO. If the backend is
/// unable to determine the width, the width will be set to RIG_PASSBAND_NORMAL
/// as a default. The value stored at mode location equals RIG_MODE_NONE when
/// the current mode of the VFO is not defined (e.g. blank memory).
/// </summary>
/// <returns>The current mode.</returns>
/// <param name="vfo">The target VFO.</param>
/// <param name="width">Retrieves the current passband width.</param>
public RigMode GetMode (ref long width, int vfo = RigVfo.Current)
{
RigMode mode;
var ret = rig_get_mode (theRig, vfo, out mode, out width);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
return mode;
}
/// <summary>
/// Sets the current VFO. The VFO can be RIG_VFO_A, RIG_VFO_B, RIG_VFO_C
/// for VFOA, VFOB, VFOC respectively or RIG_VFO_MEM for Memory mode.
/// Supported VFOs depends on rig capabilities.
/// </summary>
/// <param name="vfo">The target VFO.</param>
public void SetVFO (int vfo = RigVfo.Current)
{
var ret = rig_set_vfo (theRig, vfo);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Retrieves the current VFO. The VFO can be RIG_VFO_A, RIG_VFO_B,
/// RIG_VFO_C for VFOA, VFOB, VFOC respectively or RIG_VFO_MEM for
/// Memory mode. Supported VFOs depends on rig capabilities.
/// </summary>
/// <returns>The current VFO.</returns>
public int GetVFO ()
{
int vfo;
var ret = rig_get_vfo (theRig, out vfo);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
return vfo;
}
/// <summary>
/// Sets "Push-To-Talk" on/off.
/// </summary>
/// <param name="ptt">The PTT status to set to.</param>
/// <param name="vfo">The target VFO.</param>
public void SetPtt (PttMode ptt, int vfo = RigVfo.Current)
{
if (thread != null) {
taskQueue.Add (() => SetPttInternal (ptt, vfo));
} else {
SetPttInternal (ptt, vfo);
}
}
private void SetPttInternal (PttMode ptt, int vfo)
{
var ret = rig_set_ptt (theRig, vfo, ptt);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Updates the Ptt property
/// </summary>
/// <param name="vfo">The target VFO.</param>
public void UpdatePtt (int vfo = RigVfo.Current)
{
if (thread != null) {
taskQueue.Add (() => Ptt = GetPtt (vfo));
} else {
Ptt = GetPtt (vfo);
}
}
/// <summary>
/// Returns PttMode
/// </summary>
/// <returns>The ptt.</returns>
/// <param name="vfo">The target VFO.</param>
public PttMode GetPtt (int vfo = RigVfo.Current)
{
PttMode ptt;
var ret = rig_get_ptt (theRig, vfo, out ptt);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
return ptt;
}
/// <summary>
/// Retrieves the status of DCD (is squelch open?).
/// </summary>
/// <returns>The status of the DCD.</returns>
/// <param name="vfo">The target VFO.</param>
public DcdState GetDCD (int vfo = RigVfo.Current)
{
DcdState dcd;
var ret = rig_get_dcd (theRig, vfo, out dcd);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
return dcd;
}
/// <summary>
/// Sets the level of a setting. The level value val can be a float or an integer.
/// </summary>
/// <param name="level">The level setting.</param>
/// <param name="val">The value to set the level setting to.</param>
/// <param name="vfo">The target VFO.</param>
public void SetLevel (RigLevel level, int val, int vfo = RigVfo.Current)
{
var ret = rig_set_level (theRig, vfo, (uint)level, val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Sets the level of a setting. The level value val can be a float or an integer.
/// </summary>
/// <param name="level">The level setting.</param>
/// <param name="val">The value to set the level setting to.</param>
/// <param name="vfo">The target VFO.</param>
public void SetLevel (RigLevel level, float val, int vfo = RigVfo.Current)
{
var ret = rig_set_level (theRig, vfo, (uint)level, val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
private bool LevelIsFloat (RigLevel level)
{
return level == (level & (RigLevel.Volume | RigLevel.RF | RigLevel.Squelch | RigLevel.AudioPeakFilter |
RigLevel.NoiseReduction | RigLevel.TwinPbtIn | RigLevel.TwinPbtOut | RigLevel.RFPower |
RigLevel.MicGain | RigLevel.Compressor | RigLevel.Balance | RigLevel.Swr |
RigLevel.Alc | RigLevel.VoxGain | RigLevel.AntiVox));
}
/// <summary>
/// Retrieves the value of a level. The level value val can be a float or an integer.
/// </summary>
/// <param name="level">The level setting.</param>
/// <param name="val">Get the value of the level setting.</param>
/// <param name="vfo">The target VFO.</param>
public void GetLevel (RigLevel level, out int val, int vfo = RigVfo.Current)
{
if (LevelIsFloat (level))
throw new RigException (ErrorString (-(int)RigError.InvalidParameter));
var ret = rig_get_level (theRig, vfo, (uint)level, out val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
/// <summary>
/// Retrieves the value of a level. The level value val can be a float or an integer.
/// </summary>
/// <param name="level">The level setting.</param>
/// <param name="val">Get the value of the level setting.</param>
/// <param name="vfo">The target VFO.</param>
public void GetLevel (RigLevel level, out float val, int vfo = RigVfo.Current)
{
if (!LevelIsFloat (level))
throw new RigException (ErrorString (-(int)RigError.InvalidParameter));
var ret = rig_get_level (theRig, vfo, (uint)level, out val);
if (ret != RigError.OK) {
throw new RigException (ErrorString (ret));
}
}
private bool ParmIsFloat (RigParm parm)
{
return parm == (parm & (RigParm.Backlight | RigParm.BatteryLevel));
}
/// <summary>
/// Sets a parameter. The parameter value val can be a float or an integer.
/// </summary>
/// <param name="parm">The parameter.</param>
/// <param name="val">The value to set the parameter.</param>