-
Notifications
You must be signed in to change notification settings - Fork 26
/
NanoVNA_v1_comms.cpp
2698 lines (2233 loc) · 75.5 KB
/
NanoVNA_v1_comms.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
#pragma hdrstop
#include "NanoVNA_v1_comms.h"
#include "CommsUnit.h"
#include "DataUnit.h"
#include "Unit1.h"
#include "SettingsUnit.h"
#include "VNAUsartUnit.h"
#include "BatteryVoltageUnit.h"
#pragma package(smart_init)
#ifdef _DEBUG
//#define SHOW_CMD_START_END
#endif
#define SCAN_MASK_OUT_FREQ (1u << 0)
#define SCAN_MASK_OUT_DATA0 (1u << 1)
#define SCAN_MASK_OUT_DATA1 (1u << 2)
#define SCAN_MASK_NO_CALIBRATION (1u << 3)
#define SCAN_MASK_BINARY (1u << 7)
#define COMMAND_PROMPT "ch>"
CNanoVNA1Comms nanovna1_comms;
CNanoVNA1Comms::CNanoVNA1Comms()
{
m_capture_bm = NULL;
m_pause_comms = false;
m_get_screen_capture = false;
m_mode = MODE_NONE;
m_rx_block.type = SERIAL_STATE_IDLE;
}
CNanoVNA1Comms::~CNanoVNA1Comms()
{
if (m_capture_bm)
delete m_capture_bm;
m_capture_bm = NULL;
}
String __fastcall CNanoVNA1Comms::getSerialStateString(t_serial_state state)
{
String s;
switch (state)
{
case SERIAL_STATE_IDLE: s = "idle"; break;
case SERIAL_STATE_HELP: s = "help"; break;
case SERIAL_STATE_VERSION: s = "version"; break;
case SERIAL_STATE_INFO: s = "info"; break;
case SERIAL_STATE_MARKER: s = "marker"; break;
case SERIAL_STATE_SWEEP: s = "sweep"; break;
case SERIAL_STATE_STAT: s = "stat"; break;
case SERIAL_STATE_TIME: s = "time"; break;
case SERIAL_STATE_VBAT: s = "vbat"; break;
case SERIAL_STATE_VBAT_OFFSET: s = "vbat_offset"; break;
case SERIAL_STATE_INTEGRATOR: s = "integrator"; break;
case SERIAL_STATE_BANDWIDTH: s = "bandwidth"; break;
case SERIAL_STATE_THRESHOLD: s = "threshold"; break;
case SERIAL_STATE_EDELAY: s = "edelay"; break;
case SERIAL_STATE_DEVICE_ID: s = "deviceid"; break;
case SERIAL_STATE_CAL: s = "cal"; break;
case SERIAL_STATE_POWER: s = "power"; break;
case SERIAL_STATE_USART: s = "uart"; break;
case SERIAL_STATE_USART_CFG: s = "uart_cfg"; break;
case SERIAL_STATE_CLEARCONFIG: s = "clearconfig"; break;
case SERIAL_STATE_SAVECONFIG: s = "saveconfig"; break;
case SERIAL_STATE_SCAN: s = "scan"; break;
case SERIAL_STATE_SCAN_BIN: s = "scan_bin"; break;
case SERIAL_STATE_SCANRAW: s = "scanraw"; break;
case SERIAL_STATE_PAUSE: s = "pause"; break;
case SERIAL_STATE_RESUME: s = "resume"; break;
case SERIAL_STATE_RESET: s = "reset"; break;
case SERIAL_STATE_DATA0: s = "data 0"; break;
case SERIAL_STATE_DATA1: s = "data 1"; break;
case SERIAL_STATE_DATA2: s = "data 2"; break;
case SERIAL_STATE_DATA3: s = "data 3"; break;
case SERIAL_STATE_DATA4: s = "data 4"; break;
case SERIAL_STATE_DATA5: s = "data 5"; break;
case SERIAL_STATE_DATA6: s = "data 6"; break;
case SERIAL_STATE_DATA7: s = "data 7"; break;
case SERIAL_STATE_FREQDATA: s = "freq_data"; break;
case SERIAL_STATE_FREQDATA_BIN: s = "freq_data_bin"; break;
case SERIAL_STATE_FREQDATA_RAW: s = "freq_data_raw"; break;
case SERIAL_STATE_SCREEN_CAPTURE: s = "screen_capture"; break;
case SERIAL_STATE_SD_LIST: s = "sd_list"; break;
case SERIAL_STATE_SD_READFILE: s = "sd_readfile"; break;
case SERIAL_STATE_MODE: s = "mode"; break;
default: s = "unknown"; break;
}
return s;
}
void __fastcall CNanoVNA1Comms::reset()
{
m_tx_command = "";
m_tx_commands.resize(0);
m_rx_string = "";
m_pause_comms = false;
m_poll_ms = DEFAULT_POLL_V1_MS;
m_state_timeout_ms = DEFAULT_STATE_TIME_OUT_V1_MS;
m_link_timeout_ms = DEFAULT_LINK_TIME_OUT_V1_MS;
m_mode = MODE_INIT1;
m_poll_timer.mark();
resetRx();
}
void __fastcall CNanoVNA1Comms::resetRx()
{
m_rx_string = "";
m_rx_block.type = SERIAL_STATE_IDLE;
m_rx_block.lines.resize(0);
m_rx_block.bin_data.resize(0);
m_rx_block.bin_data_index = 0;
m_rx_block.timer.mark();
}
void __fastcall CNanoVNA1Comms::setMode(const t_mode mode)
{
// const t_mode prev_mode = m_mode;
m_mode = mode;
// if (mode != prev_mode)
// {
// m_retries = 0;
// if (prev_mode > MODE_IDLE && prev_mode != MODE_POLL && mode == MODE_IDLE)
// {
// m_mode = MODE_POLL;
// poll();
// }
// }
}
void __fastcall CNanoVNA1Comms::pauseComms(bool pause)
{
const bool was_paused = m_pause_comms;
m_pause_comms = pause;
// reset the link timer so as not to immediately timeout the link when it gets unpaused
if (Form1)
{
Form1->m_comms.rx_timer.mark();
Form1->m_comms.link_timer.mark();
}
if (pause)
return;
// un-paused
if (!was_paused)
return;
// was paused, but no more
// do stuff that wasn't being done whilst we were paused
poll();
if (m_mode != MODE_SCAN)
return;
if (data_unit.m_vna_data.cmd_pause)
addSerialTxCommand("pause");
requestScan();
}
int __fastcall CNanoVNA1Comms::serialCommandsSize()
{
return (m_tx_command.IsEmpty()) ? (int)m_tx_commands.size() : 1 + (int)m_tx_commands.size();
}
void __fastcall CNanoVNA1Comms::insertSerialTxCommand(String s, const int command_ends)
{
s = s.Trim();
// remove any trailing "\r\n"
while (s.Length() >= 2)
{
const int i = s.Pos("\r\n");
if (i < (s.Length() - 2))
break;
s = s.SubString(i, s.Length() - 2).Trim();
}
if (s.IsEmpty())
return;
switch (command_ends)
{
case 0: s += "\r\n"; break;
case 1: s += "\n\r"; break;
case 2: s += "\n"; break;
case 3: s += "\r"; break;
}
// insert the command at as the first to next be sent
m_tx_commands.insert(m_tx_commands.begin() + 0, s);
}
void __fastcall CNanoVNA1Comms::addSerialTxCommand(String s, const int command_ends)
{
s = s.Trim();
// remove any trailing "\r\n"
while (s.Length() >= 2)
{
const int i = s.Pos("\r\n");
if (i < (s.Length() - 2))
break;
s = s.SubString(i, s.Length() - 2).Trim();
}
// if (s.IsEmpty())
// return;
switch (command_ends)
{
case 0: s += "\r\n"; break;
case 1: s += "\n\r"; break;
case 2: s += "\n"; break;
case 3: s += "\r"; break;
}
m_tx_commands.push_back(s);
}
void __fastcall CNanoVNA1Comms::sendOutputPowerCommand(int power)
{
if (!data_unit.m_vna_data.cmd_power)
return;
const int value = (power < 0) ? 255 : power;
String s;
s.printf(L"power %d", value);
addSerialTxCommand(s);
}
void __fastcall CNanoVNA1Comms::sendBandwidthCommand()
{
if (!data_unit.m_vna_data.cmd_bandwidth)
return;
String s;
if (data_unit.m_vna_data.adc_Hz <= 0 || data_unit.m_vna_data.audio_samples_count <= 0 || data_unit.m_vna_data.bandwidth_Hz <= 0)
return;
int Hz = data_unit.m_bandwidth_Hz;
if (Hz > data_unit.m_vna_data.max_bandwidth_Hz) Hz = data_unit.m_vna_data.max_bandwidth_Hz;
else
if (Hz < 10) Hz = 10;
int divider = (data_unit.m_vna_data.adc_Hz / data_unit.m_vna_data.audio_samples_count) / (Hz + 1);
// int divider = (BW_HZ / Hz) - 1;
if (divider < 0)
divider = 0;
s.printf(L"bandwidth %d", divider);
// s.printf(L"bandwidth %d Hz", data_unit.m_bandwidth_Hz);
addSerialTxCommand(s);
// addSerialTxCommand("bandwidth");
}
bool __fastcall CNanoVNA1Comms::requestCapture()
{ // request a screen capture from the VNA
if (!m_get_screen_capture || m_rx_block.type != SERIAL_STATE_IDLE)
return false;
m_get_screen_capture = false;
if (!data_unit.m_vna_data.cmd_capture)
return false;
addSerialTxCommand("capture");
return true;
}
void __fastcall CNanoVNA1Comms::setCW(int64_t Hz, int power)
{
String s;
// if (m_poll_timer.millisecs(false) >= m_poll_ms)
// poll();
s.printf(L"scan %lld %lld", Hz, Hz);
addSerialTxCommand(s);
if (data_unit.m_vna_data.cmd_power)
{
const int value = (power < 0) ? 255 : power;
s.printf(L"power %d", value);
addSerialTxCommand(s);
}
}
void __fastcall CNanoVNA1Comms::requestScan()
{
// const int segments = data_unit.m_segments;
const int segment = data_unit.m_segment;
// const int points_per_segment = data_unit.m_points_per_segment;
const int num_points = data_unit.m_points;
const int segments = (data_unit.m_vna_data.type != UNIT_TYPE_TINYSA) ? data_unit.m_segments : 1;
// clear the incoming buffer
if (segment <= 0)
data_unit.m_point_incoming.resize(0);
if (segments <= 0)
return;
const uint32_t seg_span = data_unit.m_freq_span_Hz / segments;
const uint32_t seg_start = data_unit.m_freq_start_Hz + (seg_span * data_unit.m_segment);
const uint32_t seg_stop = seg_start + seg_span;
if (m_poll_timer.millisecs(false) >= m_poll_ms)
poll();
if (data_unit.m_vna_data.type == UNIT_TYPE_JANVNA_V2)
{
}
else
if (data_unit.m_vna_data.type == UNIT_TYPE_TINYSA)
{ // TinySA
uint16_t mask = 0x07;
String s;
if (data_unit.m_vna_data.cmd_scanraw)
s.printf(L"scanraw %u %u %d", seg_start, seg_stop, data_unit.m_points);
else
s.printf(L"scan %u %u %d %u", seg_start, seg_stop, data_unit.m_points, mask);
addSerialTxCommand(s);
}
else
{ // NanoVNA
uint16_t mask = 0;
#if 0
// only request S11 data if the graph is using S11 data
if (settings.timeAverageLevel > 0 || GraphTypeComboBox1->Text.LowerCase().Pos("s11") > 0 || GraphTypeComboBox2->Text.LowerCase().Pos("s11") > 0)
mask |= SCAN_MASK_OUT_DATA0;
#else
mask |= SCAN_MASK_OUT_DATA0;
#endif
#if 0
// only request S21 data if the graph is using S21 data
if (settings.timeAverageLevel > 0 || GraphTypeComboBox1->Text.LowerCase().Pos("s21") > 0 || GraphTypeComboBox2->Text.LowerCase().Pos("s21") > 0)
mask |= SCAN_MASK_OUT_DATA1;
#else
mask |= SCAN_MASK_OUT_DATA1;
#endif
if (settings.calibrationSelection != CAL_SELECT_VNA)
mask |= SCAN_MASK_NO_CALIBRATION; // don't use the VNA's own calibration
//mask |= SCAN_MASK_BINARY;
#if 0
// only request the frequency data on the first scan
if (data_unit.m_total_frames <= 0)
#endif
mask |= SCAN_MASK_OUT_FREQ;
String s;
s.printf(L"%u %u %d %u", seg_start, seg_stop, data_unit.m_points_per_segment, mask);
// s = "scan " + s;
s = (data_unit.m_vna_data.cmd_scan_bin ? "scan_bin " : "scan ") + s;
addSerialTxCommand(s);
}
}
void __fastcall CNanoVNA1Comms::sendTxCommand()
{
if (m_tx_command.IsEmpty())
return;
if (!Form1)
{
m_tx_command = "";
m_tx_commands.resize(0);
return;
}
if (Form1->m_comms.serial.connected)
{ // send the new command through the serial link
Form1->pushCommMessage("tx: " + m_tx_command);
if (Form1->m_comms.serial.TxBytes(AnsiString(m_tx_command).c_str()) <= 0)
{ // failed
Form1->pushCommMessage("tx: error .. " + m_tx_command);
//Form1->m_comms.serial.Disconnect();
//::PostMessage(Form1->Handle, WM_DISCONNECT, 0, 0);
return;
}
Form1->m_comms.rx_timer.mark();
}
#ifdef TCPIPH
if (Form1->m_comms.tcpip.connected)
{ // send the new command through the tcpip link
Form1->pushCommMessage("tx: " + m_tx_command);
if (m_comms.tcpip.TxBytes(AnsiString(m_tx_command).c_str()) <= 0)
{ // failed
Form1->pushCommMessage("tx: " + String("error ") + m_comms.tcpip.lastErrorStr + " .. " + m_tx_command);
//m_comms.tcpip.Disconnect();
//::PostMessage(Form1->Handle, WM_DISCONNECT, 0, 0);
return;
}
Form1->m_comms.rx_timer.mark();
}
#else
if (Form1->m_comms.tcpip)
{
try
{
if (Form1->m_comms.tcpip->Connected())
{ // send the new command through the tcpip link
Form1->pushCommMessage("tx: " + m_tx_command);
Form1->m_comms.tcpip->IOHandler->Write(m_tx_command);
Form1->m_comms.rx_timer.mark();
}
}
catch (Exception &exception)
{
//Application->ShowException(&exception);
Form1->pushCommMessage("tx: tcpip error " + exception.ToString());
}
}
#endif
}
void __fastcall CNanoVNA1Comms::processTxCommands()
{
if (!m_tx_command.IsEmpty())
return; // still waiting for a reply from the previous command
while (!m_tx_commands.empty())
{
// fetch the next queued command
m_tx_command = m_tx_commands.front();
// remove it from the tx queue
m_tx_commands.erase(m_tx_commands.begin() + 0);
if (!m_tx_command.IsEmpty())
break;
}
if (m_tx_command.IsEmpty())
return; // nothing to send
sendTxCommand();
}
void __fastcall CNanoVNA1Comms::poll()
{
m_poll_timer.mark();
switch (m_mode)
{
case MODE_INIT1: // we first just want to see the response to a CR/LF
addSerialTxCommand("");
break;
case MODE_INIT2:
addSerialTxCommand("help");
addSerialTxCommand("version");
addSerialTxCommand("info");
break;
case MODE_INIT3:
if (data_unit.m_vna_data.cmd_deviceid)
addSerialTxCommand("deviceid");
if (data_unit.m_vna_data.cmd_edelay)
addSerialTxCommand("edelay");
if (data_unit.m_vna_data.cmd_vbat_offset)
addSerialTxCommand("vbat_offset");
if (data_unit.m_vna_data.cmd_vbat)
addSerialTxCommand("vbat");
if (data_unit.m_vna_data.cmd_usart_cfg)
addSerialTxCommand("usart_cfg");
if (data_unit.m_vna_data.cmd_threshold)
addSerialTxCommand("threshold");
if (data_unit.m_vna_data.cmd_marker)
addSerialTxCommand("marker");
if (data_unit.m_vna_data.cmd_bandwidth)
addSerialTxCommand("bandwidth");
if (data_unit.m_vna_data.cmd_power)
addSerialTxCommand("power");
if (data_unit.m_vna_data.cmd_sweep)
addSerialTxCommand("sweep");
if (data_unit.m_vna_data.cmd_mode)
addSerialTxCommand("mode");
m_mode = MODE_IDLE;
// if (Form1)
// {
// Form1->ScanOnceSpeedButton->Enabled = true;
// Form1->ScanSpeedButton->Enabled = true;
// }
break;
default:
m_mode = MODE_IDLE;
case MODE_IDLE:
if (m_pause_comms)
break;
if (data_unit.m_vna_data.cmd_usart_cfg && data_unit.m_vna_data.usart_speed <= 0)
addSerialTxCommand("usart_cfg");
if (data_unit.m_vna_data.cmd_deviceid && data_unit.m_vna_data.deviceid < 0)
addSerialTxCommand("deviceid");
if (data_unit.m_vna_data.cmd_vbat_offset && data_unit.m_vna_data.vbat_offset_mv <= 0)
addSerialTxCommand("vbat_offset");
if (data_unit.m_vna_data.cmd_vbat)
addSerialTxCommand("vbat");
if (data_unit.m_vna_data.cmd_marker)
addSerialTxCommand("marker");
if (data_unit.m_vna_data.cmd_sweep)
addSerialTxCommand("sweep");
if (data_unit.m_vna_data.type != UNIT_TYPE_TINYSA)
{
if (data_unit.m_vna_data.cmd_threshold && data_unit.m_vna_data.freq_threshold_Hz <= 0)
addSerialTxCommand("threshold");
//if (data_unit.m_vna_data.cmd_cal)
// addSerialTxCommand("cal");
if (data_unit.m_vna_data.cmd_bandwidth && data_unit.m_vna_data.bandwidth_Hz <= 0)
addSerialTxCommand("bandwidth");
}
else
{
if (data_unit.m_vna_data.cmd_mode)
addSerialTxCommand("mode");
}
break;
case MODE_SINGLE_SCAN:
case MODE_SCAN:
case MODE_GENERATOR:
if (m_pause_comms)
break;
if (data_unit.m_vna_data.cmd_vbat)
addSerialTxCommand("vbat");
break;
}
}
void __fastcall CNanoVNA1Comms::extractVersionInfo(String s)
{
int i;
// Version: 1.0.25 [p:101, IF:12k, ADC:192k, Lcd:320x240]
s = s.Trim().LowerCase();
data_unit.m_vna_data.dislord = (s.Pos("dislord") > 0) ? true : false; // https://github.com/DiSlord/NanoVNA
data_unit.m_vna_data.oneofeleven = (s.Pos("oneofeleven") > 0) ? true : false; // https://github.com/OneOfEleven/NanoVNA
i = s.Pos('[');
if (i <= 0)
return;
s = s.SubString(i + 1, s.Length()).Trim();
i = s.Pos(']');
if (i <= 0)
return;
s = s.SubString(1, i - 1).Trim();
std::vector <String> params;
common.parseString(s, ",", params);
for (unsigned int k = 0; k < params.size(); k++)
{
String s1;
String s2;
s1 = params[k];
if (s1.Pos("p:") == 1)
{
s1 = s1.SubString(3, s1.Length()).Trim();
if (s1.IsEmpty())
continue;
if (!TryStrToInt(s1, i) || i < 51)
continue;
data_unit.m_vna_data.max_points = i;
continue;
}
if (s1.Pos("if:") == 1)
{
s1 = s1.SubString(4, s1.Length()).Trim();
if (s1.IsEmpty())
continue;
const char units = s1[s1.Length()];
s1 = s1.SubString(1, s1.Length() - 1).Trim();
if (!TryStrToInt(s1, i) || i <= 0)
continue;
if (units == 'k')
i *= 1000;
else
if (units == 'm')
i *= 1000000;
data_unit.m_vna_data.if_Hz = i;
continue;
}
if (s1.Pos("adc:") == 1)
{
s1 = s1.SubString(5, s1.Length()).Trim();
if (s1.IsEmpty())
continue;
const char units = s1[s1.Length()];
s1 = s1.SubString(1, s1.Length() - 1).Trim();
if (!TryStrToInt(s1, i) || i <= 0)
continue;
if (units == 'k')
i *= 1000;
else
if (units == 'm')
i *= 1000000;
data_unit.m_vna_data.adc_Hz = i;
if (data_unit.m_vna_data.adc_Hz > 0 && data_unit.m_vna_data.audio_samples_count > 0)
data_unit.m_vna_data.max_bandwidth_Hz = data_unit.m_vna_data.adc_Hz / data_unit.m_vna_data.audio_samples_count;
continue;
}
if (s1.Pos("lcd:") == 1)
{
int j;
s1 = s1.SubString(5, s1.Length()).Trim();
if (s1.IsEmpty())
continue;
i = s1.Pos('x');
if (i <= 0)
continue;
s2 = s1.SubString(1, i - 1).Trim();
s1 = s1.SubString(i + 1, s1.Length()).Trim();
if (!TryStrToInt(s2, i) || !TryStrToInt(s1, j) || i <= 0 || j <= 0)
continue;
data_unit.m_vna_data.lcd_width = i;
data_unit.m_vna_data.lcd_height = j;
continue;
}
}
}
void __fastcall CNanoVNA1Comms::extractInfoCommand()
{
data_unit.m_vna_data.type = UNIT_TYPE_NONE;
data_unit.m_vna_data.dislord = false;
data_unit.m_vna_data.oneofeleven = false;
data_unit.m_vna_data.info = m_rx_block.lines;
data_unit.m_vna_data.lcd_width = 0;
data_unit.m_vna_data.lcd_height = 0;
data_unit.m_vna_data.max_points = 0;
data_unit.m_vna_data.max_bandwidth_Hz = 0;
data_unit.m_vna_data.if_Hz = 0;
data_unit.m_vna_data.adc_Hz = 0;
data_unit.m_vna_data.audio_samples_count = 0;
// original version ..
// Kernel: 4.0.0
// Compiler: GCC 8.3.1 20190703 (release) [gcc-8-branch revision 273027]
// Architecture: ARMv6-M
// Core Variant: Cortex-M0
// Port Info: Preemption through NMI
// Platform: STM32F072xB Entry Level Medium Density devices
// Board: NanoVNA-H
// Build time: Jan 18 2020 - 23:46:28
// dislord version ..
// Board: NanoVNA
// 2016-2020 Copyright @edy555
// Licensed under GPL. See: https://github.com/ttrftech/NanoVNA
// Version: 0.9.3.4 beta FAT16/FAT32- SD Card, by DiSlord
// Build Time: Jun 20 2020 - 17:08:40
// Kernel: 4.0.0
// Compiler: GCC 9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]
// Architecture: ARMv6-M Core Variant: Cortex-M0
// Port Info: Preemption through NMI
// Platform: STM32F072xB Entry Level Medium Density devices
// Board: NanoVNA
// 2019-2020 Copyright @DiSlord (based on @edy555 source)
// Licensed under GPL. See: https://github.com/DiSlord/NanoVNA-D
// Version: 1.0.25 [p:101, IF:12k, ADC:192k, Lcd:320x240]
// Build Time: Sep 2 2020 - 12:38:59
// Kernel: 4.0.0
// Compiler: GCC 9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]
// Architecture: ARMv6-M Core Variant: Cortex-M0
// Port Info: Preemption through NMI
// Platform: STM32F072xB Entry Level Medium Density devices
// tinySA info reply ..
//
// tinySA v0.3
// 2019-2020 Copyright @Erik Kaashoek
// 2016-2020 Copyright @edy555
// SW licensed under GPL. See: https://github.com/erikkaashoek/tinySA
// Version: tinySA_v1.0-80-gb798ea2
// Build Time: Sep 9 2020 - 16:39:59
// Kernel: 4.0.0
// Compiler: GCC 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]
// Architecture: ARMv6-M Core Variant: Cortex-M0
// Port Info: Preemption through NMI
// Platform: STM32F072xB Entry Level Medium Density devices
// remember the current unit type
const int prev_unit_type = data_unit.m_vna_data.type;
for (unsigned int i = 0; i < m_rx_block.lines.size(); i++)
{
String s = m_rx_block.lines[i].Trim().LowerCase();
// if (s.Pos("board:") <= 0)
// continue;
if (s.Pos("nanovna-h7") > 0 || s.Pos("nanovna-h 7") > 0)
{
data_unit.m_vna_data.type = UNIT_TYPE_NANOVNA_H7;
break;
}
if (s.Pos("nanovna-h4") > 0 || s.Pos("nanovna-h 4") > 0)
{
data_unit.m_vna_data.type = UNIT_TYPE_NANOVNA_H4;
break;
}
if (s.Pos("nanovna") > 0 || s.Pos("nanovna-h") > 0)
{
data_unit.m_vna_data.type = UNIT_TYPE_NANOVNA_H;
break;
}
if (s.Pos("tinysa") > 0)
{
data_unit.m_vna_data.type = UNIT_TYPE_TINYSA;
break;
}
}
// set new unit type
switch (data_unit.m_vna_data.type)
{
case UNIT_TYPE_NONE: data_unit.m_vna_data.name = "NONE-unit-type"; break;
case UNIT_TYPE_UNKNOWN: data_unit.m_vna_data.name = "UNKNOWN-unit-type"; break;
case UNIT_TYPE_NANOVNA_H: data_unit.m_vna_data.name = "NanoVNA-H"; break;
case UNIT_TYPE_NANOVNA_H4: data_unit.m_vna_data.name = "NanoVNA-H4"; break;
case UNIT_TYPE_NANOVNA_H7: data_unit.m_vna_data.name = "NanoVNA-H7"; break;
case UNIT_TYPE_NANOVNA_V2: data_unit.m_vna_data.name = "NanoVNA-V2"; break;
case UNIT_TYPE_JANVNA_V2: data_unit.m_vna_data.name = "JanVNA-V2"; break;
case UNIT_TYPE_TINYSA: data_unit.m_vna_data.name = "tinySA"; break;
default: data_unit.m_vna_data.name = "ERROR-unit-type"; break;
}
for (unsigned int i = 0; i < m_rx_block.lines.size(); i++)
{
const String s = m_rx_block.lines[i].Trim().LowerCase();
if (s.Pos("version:") > 0)
extractVersionInfo(s);
}
switch (data_unit.m_vna_data.type)
{
default:
case UNIT_TYPE_NONE:
case UNIT_TYPE_NANOVNA_H:
if (data_unit.m_vna_data.audio_samples_count <= 0)
{
data_unit.m_vna_data.audio_samples_count = 48;
if (data_unit.m_vna_data.adc_Hz > 0 && data_unit.m_vna_data.audio_samples_count > 0)
data_unit.m_vna_data.max_bandwidth_Hz = data_unit.m_vna_data.adc_Hz / data_unit.m_vna_data.audio_samples_count;
}
if (data_unit.m_vna_data.lcd_width <= 0 && data_unit.m_vna_data.lcd_height <= 0)
{
data_unit.m_vna_data.lcd_width = 320;
data_unit.m_vna_data.lcd_height = 240;
}
if (data_unit.m_vna_data.max_points <= 0)
data_unit.m_vna_data.max_points = 101;
if (data_unit.m_vna_data.max_bandwidth_Hz <= 0)
data_unit.m_vna_data.max_bandwidth_Hz = data_unit.m_vna_data.dislord ? 4000 : 2000;
break;
case UNIT_TYPE_NANOVNA_H4:
if (data_unit.m_vna_data.audio_samples_count <= 0)
{
data_unit.m_vna_data.audio_samples_count = 96;
if (data_unit.m_vna_data.adc_Hz > 0 && data_unit.m_vna_data.audio_samples_count > 0)
data_unit.m_vna_data.max_bandwidth_Hz = data_unit.m_vna_data.adc_Hz / data_unit.m_vna_data.audio_samples_count;
}
if (data_unit.m_vna_data.lcd_width <= 0 && data_unit.m_vna_data.lcd_height <= 0)
{
data_unit.m_vna_data.lcd_width = 480;
data_unit.m_vna_data.lcd_height = 320;
}
if (data_unit.m_vna_data.max_points <= 0)
data_unit.m_vna_data.max_points = 401;
if (data_unit.m_vna_data.max_bandwidth_Hz <= 0)
data_unit.m_vna_data.max_bandwidth_Hz = 4000;
break;
case UNIT_TYPE_NANOVNA_H7:
if (data_unit.m_vna_data.audio_samples_count <= 0)
{
data_unit.m_vna_data.audio_samples_count = 96;
if (data_unit.m_vna_data.adc_Hz > 0 && data_unit.m_vna_data.audio_samples_count > 0)
data_unit.m_vna_data.max_bandwidth_Hz = data_unit.m_vna_data.adc_Hz / data_unit.m_vna_data.audio_samples_count;
}
if (data_unit.m_vna_data.lcd_width <= 0 && data_unit.m_vna_data.lcd_height <= 0)
{
data_unit.m_vna_data.lcd_width = 480;
data_unit.m_vna_data.lcd_height = 320;
}
if (data_unit.m_vna_data.max_points <= 0)
data_unit.m_vna_data.max_points = 401;
if (data_unit.m_vna_data.max_bandwidth_Hz <= 0)
data_unit.m_vna_data.max_bandwidth_Hz = 4000;
break;
// case UNIT_TYPE_NANOVNA_V2:
// break;
// case UNIT_TYPE_JANVNA_V2:
// break;
case UNIT_TYPE_TINYSA:
data_unit.m_vna_data.lcd_width = 320;
data_unit.m_vna_data.lcd_height = 240;
break;
}
::PostMessage(Form1->Handle, WM_NEW_UNIT_TYPE, 0, 0);
}
void __fastcall CNanoVNA1Comms::extractPower(String s)
{
const int pos = s.Pos("power:");
if (pos <= 0)
return;
s = s.SubString(pos + 6, s.Length()).Trim();
int value = 255;
if (!TryStrToInt(common.localiseDecimalPoint(s), value))
return;
// const int prev_power = data_unit.m_vna_data.power;
data_unit.m_vna_data.power = value;
if (value >= 255)
value = -1;
settings.outputPower = value;
::PostMessage(Form1->Handle, WM_UPDATE_OUTPUT_POWER, value, 0);
}
void __fastcall CNanoVNA1Comms::processRxBlock()
{
const int segments = (data_unit.m_vna_data.type != UNIT_TYPE_TINYSA) ? data_unit.m_segments : 1;
if (m_rx_block.type == SERIAL_STATE_IDLE)
{
if (m_mode == MODE_INIT1)
{
m_mode = MODE_INIT2;
poll();
}
}
else
{
String s = getSerialStateString(m_rx_block.type);
// if (m_rx_block.timer.millisecs(false) < 5000)
{
if (m_rx_block.lines.size() > 0 || m_rx_block.bin_data.size() > 0)
{
// m_poll_timer.mark();
switch (m_rx_block.type)
{
default:
case SERIAL_STATE_IDLE:
if (m_mode == MODE_INIT1)
{
m_mode = MODE_INIT2;
poll();
}
break;
case SERIAL_STATE_HELP:
data_unit.m_vna_data.help = "";
for (unsigned int i = 0; i < m_rx_block.lines.size(); i++)
{
// original version
// Commands: help exit info threads version reset freq offset saveconfig clearconfig data frequencies port stat gain power sample scan sweep test touchcal touchtest pause resume cal save recall trace marker edelay capture vbat transform threshold vbat_offset
// DiSlords new version
// Commands: scan scan_bin data frequencies freq sweep power bandwidth time saveconfig clearconfig touchcal touchtest pause resume cal save recall trace marker edelay capture vbat reset usart_cfg usart vbat_offset transform threshold help info version color
// TinySA
// Commands: version reset freq dac saveconfig clearconfig data frequencies scan scanraw sweep test touchcal touchtest pause resume caloutput save recall trace trigger marker capture vbat vbat_offset help info color if attenuate level sweeptime leveloffset levelchange modulation rbw mode spur load output deviceid selftest correction threads
//std::vector <String> params;
//common.parseString(m_rx_block.lines[i].Trim().LowerCase(), " ", params);
String s2 = m_rx_block.lines[i].Trim().LowerCase();
if (!s2.IsEmpty())
{
if (s2[s2.Length()] == '?')
{ // the unit replied with an error .. try again
insertSerialTxCommand("help");
}
else
{
const int pos = s2.Pos("commands:");
if (pos > 0)