-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlars_networkanalyzer.cpp
2160 lines (1700 loc) · 67.6 KB
/
lars_networkanalyzer.cpp
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: network_analyzer.cpp
* Author: David
*
* Created on March 30, 2016, 1:44 PM
*/
// LARS Network Analyzer
#include "lars_networkanalyzer.h"
// LARS Global Variables
#include "lars_variables.h"
#include "lars_fpga.h"
// LARS Network Analyzer Constructor
lars::network_analyzer::network_analyzer ( tty ttyDevice )
{
// Declare device as uninitialized
initialized = false;
// Store the device's TTY
update_tty( ttyDevice );
// Set the timeout period
serial.setTimeout( boost::posix_time::seconds( 30 ) );
// Reset the Serial Adapter
if ( reset_usb_adapter( ttyDevice ) )
{
// Test the serial port
serial.open( dev_address, dev_baud );
if ( serial.isOpen( ) )
{
// Port opened - close and report it's functional
serial.close( );
std::cout << stamp( ) << " [" << dev_name << "] Serial port is functional." << std::endl;
}
else
{
// Port wasn't open - report it's not responding to our command
std::cout << stamp( )
<< " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< "port did not respond to 'open' command." << __func__ << "." << std::endl;
}
// Initialize Controller
if ( network_analyzer::gpib_initialize( ) == false )
{
std::cout << stamp( )
<< " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< "could initialize the GPIB device to default settings." << __func__ << "." << std::endl;
}
// Finish Initialization
initialized = true;
}
}
// LARS Network Analyzer Destructor
lars::network_analyzer::~network_analyzer ( )
{
// Print Goodbye
std::cout << stamp( ) << " [" << dev_name << "] Closing port." << std::endl;
// Close the serial port
if ( serial.isOpen( ) ) serial.close( );
}
// Set the assigned TTY device
void lars::network_analyzer::update_tty ( tty ttyDevice )
{
// Store the device's name
dev_name.assign( ttyDevice.name );
// Store the device's address
dev_address.assign( ttyDevice.address );
// Store the device's serial number
dev_serialnumber.assign( ttyDevice.serialNumber );
// Store the device's baud rate
dev_baud = ttyDevice.baud;
}
// Check if the device was initialized
bool lars::network_analyzer::is_initialized ( )
{
return initialized;
}
// Initialize the GPIB Controller
bool lars::network_analyzer::gpib_initialize ( )
{
// Setup Prologix USB to GPIB Adapter
try
{
// Reset the controller
gpib_reset_controller( );
// Print the firmware version of the Prologix Controller
gpib_print_version( );
// Setup the controller's configuration
std::cout << stamp( ) << " [" << dev_name << "] Starting configuration of the GPIB controller." << std::endl;
// Tell the controller to save configuration on power-loss
gpib_set_save_configuration( true );
// Set the read timeout to 3 seconds
gpib_set_timeout( 3000 );
// Set to DEVICE mode
gpib_set_mode( 1 );
// Set auto to true (read after write mode)
gpib_set_auto( true );
// Set the address of the network analyzer
gpib_set_address( 16 );
// Set the end character mode
gpib_set_eoi( true );
// Set the end character used
gpib_set_eos( 1 );
// GPIB Controller Finished Initialization
std::cout << stamp( ) << " [" << dev_name << "] Completed configuration of the GPIB controller." << std::endl;
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
};
// Setup a collection
/* frequency_start = Starting Frequency
* frequency_stop = Ending Frequency
* points = Number of collection points
* power = Power Output
*/
bool lars::network_analyzer::setup_collection (
float frequency_start,
float frequency_stop,
int points,
float power
)
{
// Setup Prologix USB to GPIB Adapter
try
{
// Setup the controller's configuration
std::cout << stamp( ) << " [" << dev_name << "] Starting to setup a collection." << std::endl;
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Requesting the NWA's product identification." << std::endl;
// Get the NWA's Identification
nwa_print_version( );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting NWA to preset values." << std::endl;
// Start with the NWA Presets
nwa_rest_to_presets( );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Disabling the NWA debugger." << std::endl;
// Disable the NWA Debugger
nwa_set_debugging( false );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting the network analyzer's date to system date." << std::endl;
// Set the network analyzer's date to the current system date
nwa_date_to_system( );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting the network analyzer's time to system time." << std::endl;
// Set the network analyzer's time to the current system time
nwa_time_to_system( );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Checking date on the network analyzer." << std::endl;
// Print out the date on the network analyzer
std::cout << stamp( ) << " [" << dev_name << "] NWA Date: " << nwa_date( ) << std::endl;
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Checking time on the network analyzer." << std::endl;
// Print out the time on the network analyzer
std::cout << stamp( ) << " [" << dev_name << "] NWA Time: " << nwa_time( ) << std::endl;
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Clearing all NWA status registers." << std::endl;
// Clear all NWA status registers
nwa_clear_status_registers( );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting the NWA frequency range [1.1e9, 1.4e9]." << std::endl;
// Set the NWA frequency range [1.1e9, 1.4e9]
nwa_set_frequency_range( 1.1e9f, 1.4e9f );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting the NWA collection points to 201." << std::endl;
// Set the NWA collection points to 201
nwa_set_collection_points( 201 );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting the NWA output power to 0.00dBm." << std::endl;
// Setting the NWA output power to 0.00dBm
nwa_set_output_power_dbm( 0.0f );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting the NWA averaging mode to off." << std::endl;
// Setting the NWA averaging mode to off
nwa_set_averaging( false );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Toggle the NWA's power trip." << std::endl;
// Toggle the NWA's power trip
nwa_toggle_power_trip( );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting NWA channel coupling." << std::endl;
// Set NWA channel coupling
nwa_set_channel_coupling( true );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting NWA to dual display mode." << std::endl;
// Setting NWA to dual display mode
nwa_set_dual_display( true );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting NWA to blank screen mode." << std::endl;
// Setting NWA to blank screen mode
nwa_blank_screen( false );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting NWA Channel 1 to polar, form 4." << std::endl;
// Setting NWA Channel 1 to polar, form 4
nwa_set_current_channel( 1 );
nwa_set_mode_polar( );
nwa_autoscale_display( );
// Print a status message
std::cout << stamp( ) << " [" << dev_name << "] Setting NWA Channel 2 to polar, form 4." << std::endl;
// Setting NWA Channel 2 to polar, form 4
nwa_set_current_channel( 2 );
nwa_set_mode_polar( );
nwa_autoscale_display( );
// GPIB Controller Finished Initialization
std::cout << stamp( ) << " [" << dev_name << "] Finished setting up a collection." << std::endl;
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Obtain the date on the network analyzer
// RETURNS STRING // Ex: 31 Mar 2016
std::string lars::network_analyzer::nwa_date ( )
{
try
{
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Request the device's version information
serial.writeString( "\nREADDATE;\n" );
// Capture result string
std::string nwa_string = serial.readStringUntil( "\n" );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return nwa_string;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Obtain the time on the network analyzer
// RETURNS STRING // Ex: 11:21:13
std::string lars::network_analyzer::nwa_time ( )
{
try
{
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Request the device's version information
serial.writeString( "\nREADTIME;\n" );
// Capture result string
std::string nwa_string = serial.readStringUntil( "\n" );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return nwa_string;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Set the date on the network analyzer
// SETDATE DD MMM YYYY;
bool lars::network_analyzer::nwa_date ( int year, int month, int day )
{
try
{
// Check the input year
if ( ( year < 0 ) or (year > 9999 ) )
throw timeout_exception( "Input year out of range [0, 9999]." );
// Check the input month
if ( ( month < 1 ) or (month > 12 ) )
throw timeout_exception( "Input month out of range [1, 12]." );
// Check the input day
if ( ( day < 1 ) or (day > 31 ) )
throw timeout_exception( "Input day out of range [1, 31]." );
// Convert month to string
std::string desired_month;
switch ( month )
{
case 1:
desired_month.assign( "Jan" );
break;
case 2:
desired_month.assign( "Feb" );
break;
case 3:
desired_month.assign( "Mar" );
break;
case 4:
desired_month.assign( "Apr" );
break;
case 5:
desired_month.assign( "May" );
break;
case 6:
desired_month.assign( "Jun" );
break;
case 7:
desired_month.assign( "Jul" );
break;
case 8:
desired_month.assign( "Aug" );
break;
case 9:
desired_month.assign( "Sep" );
break;
case 10:
desired_month.assign( "Oct" );
break;
case 11:
desired_month.assign( "Nov" );
break;
case 12:
desired_month.assign( "Dec" );
break;
default:
// Should never reach this due to check above
desired_month.assign( "Dec" );
break;
}
// Build the command
std::stringstream sstream;
sstream << "\nSETDATE "
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( day )
<< " "
<< desired_month
<< " "
<< std::setfill( '0' )
<< std::setw( 4 )
<< std::to_string( year )
<< ";\n";
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Send the string command
serial.writeString( sstream.str( ) );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Set the time on the network analyzer
// SETTIME HH:MM:SS;
bool lars::network_analyzer::nwa_time ( int hour, int minute, int second )
{
try
{
// Check the input year
if ( ( hour < 0 ) or (hour > 23 ) )
throw timeout_exception( "Input hour out of range [0, 23]." );
// Check the input month
if ( ( minute < 0 ) or (minute > 59 ) )
throw timeout_exception( "Input minute out of range [0, 59]." );
// Check the input day
if ( ( second < 0 ) or (second > 59 ) )
throw timeout_exception( "Input second out of range [0, 59]." );
// Build the command
std::stringstream sstream;
sstream << "\nSETTIME "
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( hour )
<< ":"
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( minute )
<< ":"
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( second )
<< ";\n";
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Send the string command
serial.writeString( sstream.str( ) );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Set the date on the network analyzer
// SETDATE DD MMM YYYY;
bool lars::network_analyzer::nwa_date_to_system ( )
{
try
{
// Retrieve the current system date/time
// Ex: "2016-05-09T18:30:21+0000"
std::string current_system_datetime = GetStdoutFromCommand( "date -Iseconds" );
// Parse the hour, minute, and second
int year = std::stoi( current_system_datetime.substr( 0, 4 ) );
int month = std::stoi( current_system_datetime.substr( 5, 2 ) );
int day = std::stoi( current_system_datetime.substr( 8, 2 ) );
// Check the input year
if ( ( year < 0 ) or (year > 9999 ) )
throw timeout_exception( "Input year out of range [0, 9999]." );
// Check the input month
if ( ( month < 1 ) or (month > 12 ) )
throw timeout_exception( "Input month out of range [1, 12]." );
// Check the input day
if ( ( day < 1 ) or (day > 31 ) )
throw timeout_exception( "Input day out of range [1, 31]." );
// Convert month to string
std::string desired_month;
switch ( month )
{
case 1:
desired_month.assign( "Jan" );
break;
case 2:
desired_month.assign( "Feb" );
break;
case 3:
desired_month.assign( "Mar" );
break;
case 4:
desired_month.assign( "Apr" );
break;
case 5:
desired_month.assign( "May" );
break;
case 6:
desired_month.assign( "Jun" );
break;
case 7:
desired_month.assign( "Jul" );
break;
case 8:
desired_month.assign( "Aug" );
break;
case 9:
desired_month.assign( "Sep" );
break;
case 10:
desired_month.assign( "Oct" );
break;
case 11:
desired_month.assign( "Nov" );
break;
case 12:
desired_month.assign( "Dec" );
break;
default:
// Should never reach this due to check above
desired_month.assign( "Dec" );
break;
}
// Build the command
std::stringstream sstream;
sstream << "\nSETDATE "
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( day )
<< " "
<< desired_month
<< " "
<< std::setfill( '0' )
<< std::setw( 4 )
<< std::to_string( year )
<< ";\n";
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Send the string command
serial.writeString( sstream.str( ) );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Set the time on the network analyzer to the current system time
// SETTIME HH:MM:SS;
bool lars::network_analyzer::nwa_time_to_system ( )
{
try
{
// Retrieve the current system date/time
// Ex: "2016-05-09T18:30:21+0000"
std::string current_system_datetime = GetStdoutFromCommand( "date -Iseconds" );
// Parse the hour, minute, and second
int hour = std::stoi( current_system_datetime.substr( 11, 2 ) );
int minute = std::stoi( current_system_datetime.substr( 14, 2 ) );
int second = std::stoi( current_system_datetime.substr( 17, 2 ) );
// Check the input year
if ( ( hour < 0 ) or (hour > 23 ) )
throw timeout_exception( "Input hour out of range [0, 23]." );
// Check the input month
if ( ( minute < 0 ) or (minute > 59 ) )
throw timeout_exception( "Input minute out of range [0, 59]." );
// Check the input day
if ( ( second < 0 ) or (second > 59 ) )
throw timeout_exception( "Input second out of range [0, 59]." );
// Build the command
std::stringstream sstream;
sstream << "\nSETTIME "
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( hour )
<< ":"
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( minute )
<< ":"
<< std::setfill( '0' )
<< std::setw( 2 )
<< std::to_string( second )
<< ";\n";
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Send the string command
serial.writeString( sstream.str( ) );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Print the identifier and version of the network analyzer
bool lars::network_analyzer::nwa_print_version ( )
{
try
{
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Request the device's version information
serial.writeString( "\nIDN?;\n" );
// Capture result string
std::string nwa_string = serial.readStringUntil( "\n" );
// Print the response
std::cout << stamp( ) << " [" << dev_name << "] Product ID: " << nwa_string << std::endl;
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Reset the network analyzer to preset values
bool lars::network_analyzer::nwa_rest_to_presets ( )
{
try
{
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// -- Reset to presets
// Send command
serial.writeString( "\nOPC?; PRES;\n" );
// Capture result string
std::string nwa_string = serial.readStringUntil( "\n" );
// Verify we've completed presets
if ( std::stoi( nwa_string ) != 1 )
throw timeout_exception( "Setting NWA to preset values failed." );
// -- Disable Transform Mode
/*
// Send command
serial.writeString("\nOPC?; TIMDTRAN OFF;\n");
// Capture result string
nwa_string.assign(serial.readStringUntil("\n"));
// Verify we've completed presets
if (std::stoi(nwa_string) != 1)
throw timeout_exception("Setting NWA to disable transform mode failed.");
// -- Disable corrections and set to linear frequency sweep
// Send command (no response expected)
serial.writeString("\nCORR OFF; LINFREQ;\n");
*/
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Reset the network analyzer to preset values
bool lars::network_analyzer::nwa_set_debugging ( bool debug_enabled )
{
try
{
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Decide if debugging is enabled or not
if ( debug_enabled )
// Request the device's version information
serial.writeString( "\nDEBUON;\n" );
else
// Request the device's version information
serial.writeString( "\nDEBUOFF;\n" );
// Verify Integrity
// Request the current debug mode from the NWA
serial.writeString( "\nDEBU?;\n" );
// Capture result string
std::string nwa_string = serial.readStringUntil( "\n" );
// Verify we've completed presets
if ( std::stoi( nwa_string ) != debug_enabled )
throw timeout_exception( "Setting the NWA debug mode failed." );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Clear all status registers
// CLES;
bool lars::network_analyzer::nwa_clear_status_registers ( )
{
try
{
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Send the command (no result expected)
serial.writeString( "\nCLES;\n" );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}
};
// Set collection frequency range
// STOP %f MHz; STAR %f MHz;
bool lars::network_analyzer::nwa_set_frequency_range ( float start_frequency, float end_frequency )
{
try
{
// Verify the integrity of the input starting frequency
if ( ( start_frequency < 30.0e3f ) or (start_frequency > 3.0e9f ) )
throw timeout_exception( "Starting frequency is out of range [30.0e3, 3.0e9]." );
// Verify the integrity of the input ending frequency
if ( ( end_frequency < 30.0e3f ) or (end_frequency > 3.0e9f ) )
throw timeout_exception( "Ending frequency is out of range [30.0e3, 3.0e9]." );
// Verify the starting frequency is smaller than the ending frequency
if ( end_frequency < start_frequency )
throw timeout_exception( "Ending frequency must be larger than starting frequency." );
// Reduce input value (starting frequency) to 3 digit precision
std::stringstream start_stream;
start_stream << std::setprecision( 3 )
<< std::scientific
<< start_frequency;
start_frequency = std::stof( start_stream.str( ) );
// Reduce input value (ending frequency) to 3 digit precision
std::stringstream end_stream;
end_stream << std::setprecision( 3 )
<< std::scientific
<< end_frequency;
end_frequency = std::stof( end_stream.str( ) );
// Build the command
std::stringstream sstream;
sstream << "\nSTAR "
<< std::setprecision( 3 )
<< std::scientific
<< start_frequency
<< " Hz; STOP "
<< std::setprecision( 3 )
<< std::scientific
<< end_frequency
<< " Hz;\n";
// Build the string
std::string nwa_string = sstream.str( );
// Open the serial port if it's closed
if ( not serial.isOpen( ) ) serial.open( dev_address, dev_baud );
// Send command
serial.writeString( sstream.str( ) );
// Verify Integrity
// Request the current setting from the NWA
serial.writeString( "\nSTOP?;\n" );
// Capture result string
nwa_string.assign( serial.readStringUntil( "\n" ) );
// Verify we've completed presets ( 10% threshold )
float actual_end_frequency = std::stof( nwa_string );
if ( actual_end_frequency != end_frequency )
throw timeout_exception( "Setting the NWA ending frequency failed." );
// Request the current setting from the NWA
serial.writeString( "\nSTAR?;\n" );
// Capture result string
nwa_string.assign( serial.readStringUntil( "\n" ) );
// Verify we've completed presets ( 10% threshold )
float actual_start_frequency = std::stof( nwa_string );
if ( actual_start_frequency != start_frequency )
throw timeout_exception( "Setting the NWA starting frequency failed." );
// Close the serial port if it's closed
if ( serial.isOpen( ) ) serial.close( );
// return success
return true;
}
catch ( boost::system::system_error& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error in " << __FILE__ << "(" << __LINE__ << "): "
<< e.what( ) << std::endl;
return false;
}
catch ( timeout_exception& e )
{
// On error, output the message
std::cout << stamp( ) << " [" << dev_name << "] Error: " << e.what( ) << std::endl;
return false;
}