-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi3.tcl
executable file
·1531 lines (1240 loc) · 30.5 KB
/
api3.tcl
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
####################################################################
# This file is part of tk3, a utility program for the
# ICOM IC-R3 receiver.
#
# Copyright (C) 2001, 2002, Bob Parnass
#
# tk3 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# tk3 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with tk3; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
####################################################################
set RadioAddress EE
set RadioAddressHex \xEE
set PCAddress EF
set PCAddressHex \xEF
set Nmessages 504
set BytesPerMessage 32
set NBanks 8
set NChanPerBank 50
set VNChanPerBank 50
set ChanNumberRepeat yes
set HasLabels yes
###################################################################
# Starting address (in hexadecimal) for each field in
# the memory image.
###################################################################
set ImageAddr(MemoryFreqs) 00 ;# to C7F
set ImageAddr(MemoryDuplex) 04
set ImageAddr(MemoryOffset) 04
set ImageAddr(MemoryModes) 08
set ImageAddr(MemoryToneCode) 08
set ImageAddr(MemoryToneFlag) 09
set ImageAddr(MemorySkip) 09
set ImageAddr(MemorySteps) 09
set ImageAddr(MemoryLabels) 0A ;# 6 chars each
set ImageAddr(SearchFreqFirst) 1900
set ImageAddr(SearchModeFirst) 1908
set ImageAddr(SearchStepFirst) 1909
set ImageAddr(SearchFreqSecond) 1910
set ImageAddr(SearchModeSecond) 1918
set ImageAddr(SearchStepSecond) 1919
set ImageAddr(BandStackFreq) 1C20 ;# 1F40, 10 of them
set ImageAddr(BandStackDuplex) 1C24
set ImageAddr(BandStackOffset) 1C24
set ImageAddr(BandStackModes) 1C28 ;# 10 of them
set ImageAddr(BandStackToneCode) 1C28
set ImageAddr(BandStackSkip) 1C29
set ImageAddr(BandStackToneFlag) 1C29
set ImageAddr(BandStackSteps) 1C29
set ImageAddr(AMTV) 1C98 ;# AM TV channels 1-83
set ImageAddr(AMTVMem) 1DE4 ;# AM TV memories 0 -9
set ImageAddr(FMTVMem) 1E0C ;# FM TV memories 0 - 49
set ImageAddr(FMTVMemRev) 1ED4 ;# FM TV reverse video
set ImageAddr(FMTVMemSub) 1ED4 ;# FM TV subcarriers 0 - 49
set ImageAddr(AMTVSkip) 1F10 ;# 1 bit for each channel
set ImageAddr(Resume) 1F22
set ImageAddr(Pause) 1F23
set ImageAddr(FrequencySkip) 1F24
set ImageAddr(Beep) 1F26
set ImageAddr(Lamp) 1F27
set ImageAddr(AutoOff) 1F28
set ImageAddr(PowerSave) 1F29
set ImageAddr(Monitor) 1F2A
set ImageAddr(LockEffect) 1F2D
set ImageAddr(UserComment) 1F60
set ImageAddr(FileVersion) 1F70
set ImageAddr(DialStep) 0E60
set ImageAddr(BankScan) 0E65
set ImageAddr(DialAccel) 0E6D
set Band(USA,0,low) .495
set Band(USA,0,high) 1.620
set Band(USA,1,low) 1.625
set Band(USA,1,high) 29.995
set Band(USA,2,low) 30.000
set Band(USA,2,high) 75.995
set Band(USA,3,low) 76.000 ;# Euro models
set Band(USA,3,high) 107.995 ;# Euro models
set Band(USA,4,low) 108.000
set Band(USA,4,high) 135.995
set Band(USA,5,low) 136.000
set Band(USA,5,high) 255.095
set Band(USA,6,low) 255.100
set Band(USA,6,high) 382.095
set Band(USA,7,low) 382.100
set Band(USA,7,high) 769.795
set Band(USA,8,low) 769.800
set Band(USA,8,high) 960.095
set Band(USA,9,low) 960.100
set Band(USA,9,high) 1399.995
set Band(USA,10,low) 1400.000
set Band(USA,10,high) 2450.095
##########################################################
# Translation tables
##########################################################
set LockEffect(NORMAL) 0
set LockEffect(NO_SQL) 1
set LockEffect(NO_VOL) 2
set LockEffect(ALL) 3
set RLockEffect(0) NORMAL
set RLockEffect(1) NO_SQL
set RLockEffect(2) NO_VOL
set RLockEffect(3) ALL
set TVOffset(+3MHz) 0
set TVOffset(+2MHz) 1
set TVOffset(-3MHz) 2
set TVOffset(-2MHz) 3
set RTVOffset(0) +3MHz
set RTVOffset(1) +2MHz
set RTVOffset(2) -3MHz
set RTVOffset(3) -2MHz
# Lamp operation
set Lamp(OFF) 0
set Lamp(ON) 1
set Lamp(AUTO) 2
set RLamp(0) OFF
set RLamp(1) ON
set RLamp(2) AUTO
# Dial select
set Dial(100kHz) 0
set Dial(1MHz) 1
set Dial(10MHz) 2
set RDial(0) 100kHz
set RDial(1) 1MHz
set RDial(2) 10MHz
# Monitor key
set Monitor(PUSH) 0
set Monitor(HOLD) 1
set RMonitor(0) PUSH
set RMonitor(1) HOLD
# Auto Power Off
set AutoOff(OFF) 0
set AutoOff(30) 1
set AutoOff(60) 2
set AutoOff(90) 3
set AutoOff(120) 4
set RAutoOff(0) OFF
set RAutoOff(1) 30
set RAutoOff(2) 60
set RAutoOff(3) 90
set RAutoOff(4) 120
# Scan Pause
set Pause(2) 0
set Pause(4) 1
set Pause(6) 2
set Pause(8) 3
set Pause(10) 4
set Pause(12) 5
set Pause(14) 6
set Pause(16) 7
set Pause(18) 8
set Pause(20) 9
set Pause(HOLD) 10
set RPause(0) 2
set RPause(1) 4
set RPause(2) 6
set RPause(3) 8
set RPause(4) 10
set RPause(5) 12
set RPause(6) 14
set RPause(7) 16
set RPause(8) 18
set RPause(9) 20
set RPause(10) HOLD
# Scan Resume
set Resume(0) 0
set Resume(1) 1
set Resume(2) 2
set Resume(3) 3
set Resume(4) 4
set Resume(5) 5
set Resume(HOLD) 6
set RResume(0) 0
set RResume(1) 1
set RResume(2) 2
set RResume(3) 3
set RResume(4) 4
set RResume(5) 5
set RResume(6) HOLD
set Step(5) "0"
set Step(6.25) "1"
set Step(10) "2"
set Step(12.5) "3"
set Step(15) "4"
set Step(20) "5"
set Step(25) "6"
set Step(30) "7"
set Step(50) "8"
set Step(100) "9"
set Step(AUTO) "10"
set RStep(0) "5"
set RStep(1) "6.25"
set RStep(2) "10"
set RStep(3) "12.5"
set RStep(4) "15"
set RStep(5) "20"
set RStep(6) "25"
set RStep(7) "30"
set RStep(8) "50"
set RStep(9) "100"
set RStep(10) "AUTO"
set Mode(M0) "0"
set Mode(M7) "7"
set Mode(M8) "8"
set Mode(M9) "9"
set Mode(MA) "A"
set Mode(MB) "B"
set Mode(MC) "C"
set Mode(MD) "D"
set Mode(ME) "E"
set Mode(MF) "F"
set Mode(NFM) "0"
set Mode(WFM) "1"
set Mode(AM) "2"
set Mode(AUTO) "3"
set RMode(0) NFM
set RMode(1) WFM
set RMode(2) AM
set RMode(3) AUTO
set Skip(scan) 0
set Skip(pskip) 1
set Skip(skip) 2
set RSkip(0) " "
set RSkip(1) pskip
set RSkip(2) skip
##########################################################
# Encoding in a .ICF file.
##########################################################
set Icf2Hex(g) 0
set Icf2Hex(h) 1
set Icf2Hex(i) 2
set Icf2Hex(j) 3
set Icf2Hex(k) 4
set Icf2Hex(l) 5
set Icf2Hex(m) 6
set Icf2Hex(n) 7
set Icf2Hex(o) 8
set Icf2Hex(p) 9
set Icf2Hex(x) a
set Icf2Hex(y) b
set Icf2Hex(z) c
set Icf2Hex({) d
set Icf2Hex(|) e
set Icf2Hex(}) f
set Hex2Digit(30) 0
set Hex2Digit(31) 1
set Hex2Digit(32) 2
set Hex2Digit(33) 3
set Hex2Digit(34) 4
set Hex2Digit(35) 5
set Hex2Digit(36) 6
set Hex2Digit(37) 7
set Hex2Digit(38) 8
set Hex2Digit(39) 9
set Hex2Digit(41) A
set Hex2Digit(42) B
set Hex2Digit(43) C
set Hex2Digit(44) D
set Hex2Digit(45) E
set Hex2Digit(46) F
set Digit2Hex(0) 30
set Digit2Hex(1) 31
set Digit2Hex(2) 32
set Digit2Hex(3) 33
set Digit2Hex(4) 34
set Digit2Hex(5) 35
set Digit2Hex(6) 36
set Digit2Hex(7) 37
set Digit2Hex(8) 38
set Digit2Hex(9) 39
set Digit2Hex(A) 41
set Digit2Hex(B) 42
set Digit2Hex(C) 43
set Digit2Hex(D) 44
set Digit2Hex(E) 45
set Digit2Hex(F) 46
# CTCSS codes (there are 50 codes total)
set CtcssBias 0
set Ctcss(0.0) 0
set Ctcss(67.0) 0
set Ctcss(69.3) 1
set Ctcss(71.9) 2
set Ctcss(74.4) 3
set Ctcss(77.0) 4
set Ctcss(79.7) 5
set Ctcss(82.5) 6
set Ctcss(85.4) 7
set Ctcss(88.5) 8
set Ctcss(91.5) 9
set Ctcss(94.8) 10
set Ctcss(97.4) 11
set Ctcss(100.0) 12
set Ctcss(103.5) 13
set Ctcss(107.2) 14
set Ctcss(110.9) 15
set Ctcss(114.8) 16
set Ctcss(118.8) 17
set Ctcss(123.0) 18
set Ctcss(127.3) 19
set Ctcss(131.8) 20
set Ctcss(136.5) 21
set Ctcss(141.3) 22
set Ctcss(146.2) 23
set Ctcss(151.4) 24
set Ctcss(156.7) 25
set Ctcss(159.8) 26
set Ctcss(162.2) 27
set Ctcss(165.5) 28
set Ctcss(167.9) 29
set Ctcss(171.3) 30
set Ctcss(173.8) 31
set Ctcss(177.3) 32
set Ctcss(179.9) 33
set Ctcss(183.5) 34
set Ctcss(186.2) 35
set Ctcss(189.9) 36
set Ctcss(192.8) 37
set Ctcss(196.6) 38
set Ctcss(199.5) 39
set Ctcss(203.5) 40
set Ctcss(206.5) 41
set Ctcss(210.7) 42
set Ctcss(218.1) 43
set Ctcss(225.7) 44
set Ctcss(229.1) 45
set Ctcss(233.6) 46
set Ctcss(241.8) 47
set Ctcss(250.3) 48
set Ctcss(254.1) 49
set RCtcss(0) 67.0
set RCtcss(1) 69.3
set RCtcss(2) 71.9
set RCtcss(3) 74.4
set RCtcss(4) 77.0
set RCtcss(5) 79.7
set RCtcss(6) 82.5
set RCtcss(7) 85.4
set RCtcss(8) 88.5
set RCtcss(9) 91.5
set RCtcss(10) 94.8
set RCtcss(11) 97.4
set RCtcss(12) 100.0
set RCtcss(13) 103.5
set RCtcss(14) 107.2
set RCtcss(15) 110.9
set RCtcss(16) 114.8
set RCtcss(17) 118.8
set RCtcss(18) 123.0
set RCtcss(19) 127.3
set RCtcss(20) 131.8
set RCtcss(21) 136.5
set RCtcss(22) 141.3
set RCtcss(23) 146.2
set RCtcss(24) 151.4
set RCtcss(25) 156.7
set RCtcss(26) 159.8
set RCtcss(27) 162.2
set RCtcss(28) 165.5
set RCtcss(29) 167.9
set RCtcss(30) 171.3
set RCtcss(31) 173.8
set RCtcss(32) 177.3
set RCtcss(33) 179.9
set RCtcss(34) 183.5
set RCtcss(35) 186.2
set RCtcss(36) 189.9
set RCtcss(37) 192.8
set RCtcss(38) 196.6
set RCtcss(39) 199.5
set RCtcss(40) 203.5
set RCtcss(41) 206.5
set RCtcss(42) 210.7
set RCtcss(43) 218.1
set RCtcss(44) 225.7
set RCtcss(45) 229.1
set RCtcss(46) 233.6
set RCtcss(46) 241.8
set RCtcss(48) 250.3
set RCtcss(49) 254.1
##########################################################
#
# Initialize a few global variables.
#
# Return the pathname to a configuration file in the user's
# HOME directory
#
# Returns:
# list of 2 elements:
# -name of configuration file
# -name of label file
#
##########################################################
proc InitStuff { } \
{
global argv0
global DisplayFontSize
global env
global Home
global Pgm
global RootDir
global tcl_platform
set platform $tcl_platform(platform)
switch -glob $platform \
{
{unix} \
{
set Home $env(HOME)
set rcfile [format "%s/.tk3rc" $Home]
set labelfile [format "%s/.tk3la" $Home]
set DisplayFontSize "Courier 56 bold"
}
{macintosh} \
{
# Configuration file should be
# named $HOME/.tk3rc
# Use forward slashes within Tcl/Tk
# instead of colons.
set Home $env(HOME)
regsub -all {:} $Home "/" Home
set rcfile [format "%s/.tk3rc" $Home]
set labelfile [format "%s/.tk3la" $Home]
# The following font line may need changing.
set DisplayFontSize "Courier 56 bold"
}
{windows} \
{
# Configuration file should be
# named $tk3/tk3.ini
# Use forward slashes within Tcl/Tk
# instead of backslashes.
set Home $env(tk3)
regsub -all {\\} $Home "/" Home
set rcfile [format "%s/tk3.ini" $Home]
set labelfile [format "%s/tk3.lab" $Home]
set DisplayFontSize "Courier 28 bold"
}
default \
{
puts "Operating System $platform not supported."
exit 1
}
}
set Home $env(HOME)
# set Pgm [string last "/" $argv0]
set lst [list $rcfile $labelfile]
return $lst
}
###################################################################
# Disable computer control of radio.
###################################################################
proc DisableCControl { } \
{
global Sid
after 500
close $Sid
return
}
##########################################################
# Copy memory image to radio
#
# Returns:
# 0 -ok
# 1 -error
# 2 -error, cannot read radio version info
##########################################################
proc WriteImage { }\
{
global GlobalParam
global Mimage
global Nmessages
global Sid
set totmsgs $Nmessages
set s [GetModelInfo]
binary scan $s "H*" x
set GlobalParam(RadioVersion) $x
if {$GlobalParam(RadioVersion) == ""} \
{
# Error while asking radio for version info.
return 2
}
# Create and display progress bar.
toplevel .pbw
wm title .pbw "Writing to radio"
grab set .pbw
set p [MakeWaitWindow .pbw.g 0 PaleGreen]
set pc 0
gauge_value $p $pc
update
set db 0
# Write "clone in mode" command, including
# radio version information.
SendCloneIn
set bptr 0
set maddr 0
# For each message.
for {set i 0} {$i < $Nmessages} {incr i} \
{
# Variable line containes info in the format it
# will be written to the radio.
set line ""
# Variable bline contains packed hex gulp.
set bline ""
# A message sent to the radio consists of:
# E4 - Payload Data Command code
# Memory Gulp (unpacked so 2 bytes represent 1 byte):
#
# memory address (4 bytes unpacked)
# number of bytes (2 bytes unpacked)
# image data (32 bytes unpacked)
append line [binary format "H2" E4 ]
# Memory address
set hmaddr [format "%04x" $maddr]
set bmaddr [binary format "H4" $hmaddr]
append bline $bmaddr
# Byte count
set hn [binary format "H2" 10]
append bline $hn
# Copy the next chunk of the image
set end [expr {$bptr + 15}]
set s [string range $Mimage $bptr $end]
append bline $s
#
# Calulate and append the checksum byte
#
# Checksum is decimal.
set cksum [CalcCheckSum $bline]
# Convert checksum to hexadecimal.
set hcksum [format "%02x" $cksum]
set bcksum [binary format "H2" $hcksum ]
append bline $bcksum
# Unpack the binary stuff.
# This makes it twice as long.
set msg [DumpBinary $bline]
# puts stderr "WriteImage: before packing:\n$msg\n"
set ubuf [UnpackString $bline]
append line $ubuf
SendCmd $Sid $line
# Read back the message we just sent to "clean out"
# the serial buffers.
# If we don't do this, WindowsXP will hang after
# the download to the radio is completed.
if { [ReadEcho $line] } \
{
# Error.
# We did not read back what we wrote.
puts stderr "WriteImage: Error while reading echo from message $i."
# Data xfer suceeded.
# Zap the progress bar.
grab release .pbw
catch {destroy .pbw}
return 1
}
incr bptr 16
incr maddr 16
# Update progress bar widget.
set pc [ expr $i * 100 / $totmsgs ]
if {$pc >= 100.0} \
{
set pc 99
}
gauge_value $p $pc
}
SendTermination
if {[GetTerminationResult]} \
{
# Data xfer failed.
# Zap the progress bar.
grab release .pbw
catch {destroy .pbw}
# Data xfer suceeded.
# Zap the progress bar.
grab release .pbw
catch {destroy .pbw}
return 1
}
# Data xfer suceeded.
# Zap the progress bar.
grab release .pbw
catch {destroy .pbw}
return 0
}
##########################################################
# Copy memory image from radio
##########################################################
proc ReadImage { } \
{
global GlobalParam
global Mimage
global Nmessages
global Pgm
global Sid
set code 0
set s [GetModelInfo]
binary scan $s "H*" x
set GlobalParam(RadioVersion) $x
if {$GlobalParam(RadioVersion) == ""} \
{
# Error while asking radio for version info.
return 2
}
# Create and display progress bar.
toplevel .pbw
wm title .pbw "Reading IC-R2"
grab set .pbw
set p [MakeWaitWindow .pbw.g 0 PaleGreen]
set pc 0
gauge_value $p $pc
update
set Mimage ""
SendCloneOut
# For each message.
for {set i 0} {$i < 1000} {incr i} \
{
set line [ReadXferRx]
set len [string length $line]
# puts stderr "ReadImage: i= $i, len= $len"
# Update progress bar widget.
set pc [ expr {$i * 100 / $Nmessages} ]
if {$pc >= 100.0} \
{
set pc 99
}
gauge_value $p $pc
set cc [string range $line 0 0]
binary scan $cc "H2" s
# Examine the command code byte.
if { [string compare -nocase -length 1 $cc \xe5] == 0} \
{
# This was a termination message.
# There is no more data to read.
set code 0
set GlobalParam(TermMsg) [DumpBinary $line]
break
}
if {$len != 41} \
{
# Error while reading from radio.
set code -1
break
}
# Got a data record.
# Temorarily convert it from funky unpacked
# format to binary format.
# Then, perform a checksum calculation on it.
set pline [PackString [string range $line 1 42]]
set plen [string length $pline]
# puts stderr "ReadXferRx: line length is: $len"
set dbuf [string range $pline 0 18]
set cksum [string range $pline 19 19]
set ccksum [CalcCheckSum $dbuf]
binary scan $cksum "H*" icksum
scan $icksum "%x" cksum
# puts stderr [format "CHECKSUM radio: %s, calculated: %s\n" \
$cksum $ccksum]
if {$cksum != $ccksum} \
{
set msg [format \
"%s: error, checksum mismatch, radio: %s, calculated: %s\n" \
$Pgm $cksum $ccksum]
Tattle $msg
tk_dialog .error "Checksum error while reading" \
$msg error 0 OK
exit
}
# Strip off memory address and count bytes.
set buf [string range $dbuf 3 end]
append Mimage $buf
}
set GlobalParam(NmsgsRead) $i
# Zap the progress bar.
grab release .pbw
destroy .pbw
return $code
}
###################################################################
# this takes a string and converts the
# first character in it to an integer
# in the range 0-255
#
# if the string is empty, returns an empty string
###################################################################
proc Char2Int { c } \
{
set tmp ""
set n [binary scan $c "c" tmp]
if { ($n == 1) && ($tmp < 0) } \
{
# Force negative number to be positive
set tmp [expr $tmp + 256]
}
return "$tmp"
}
###################################################################
# Calculate the 2s complement modulo 256 checksum byte for a string by
# summing all the ascii character values,
# getting the 2s complement, then modulo 256.
###################################################################
proc CalcCheckSum { s } \
{
set len [string length $s]
set sum 0
binary scan $s "H*" x
# regsub -all ".." $x { \0} x
# puts stderr "CalcCheckSum: $x"
for {set i 0} {$i < $len} {incr i} \
{
set c [string index $s $i]
set tmp [Char2Int $c]
# set xtmp [format "%x" $tmp]
# puts stderr "CalcCheckSum: $i (of $len): xtmp = $xtmp"
set sum [expr {$sum + $tmp}]
}
# set xsum [format "%x" $sum]
# puts stderr "CalcCheckSum: xsum = $xsum"
#
set sum [expr {0 - $sum}]
# set ysum [format "%x" $sum]
# puts stderr "CalcCheckSum: ysum = $ysum"
set sum [expr $sum % 256]
# set zsum [format "%x" $sum]
# puts stderr "CalcCheckSum: zsum = $zsum"
return $sum
}
###################################################################
# Create a string of "n" bytes where each byte is \xff (255 decimal).
###################################################################
proc Padff { n } \
{
set ffrecd ""
set byte [binary format "H2" ff]
for {set i 0} {$i < $n} {incr i} \
{
append ffrecd $byte
}
return $ffrecd
}
##########################################################
# Open the serial port.
# Notes:
# This procedure sets the global variable Sid.
#
# Returns:
# "" -ok
# else -error message
##########################################################
proc OpenDevice {} \
{
global Pgm
global GlobalParam
global Sid
global tcl_platform
set platform $tcl_platform(platform)
switch -glob $platform \
{
{unix} \
{
set Sid [open $GlobalParam(Device) "r+"]
}
{macintosh} \
{
set Sid [open $GlobalParam(Device) "r+"]
}
{windows} \
{
set Sid [open $GlobalParam(Device) RDWR]
}
default \
{
set msg "$Pgm error: Platform $platform not supported."
Tattle $msg
return $msg
}
}
# Set up the serial port parameters (similar to stty)
if [catch {fconfigure $Sid \
-buffering full \
-translation binary \
-mode 9600,n,8,1 -blocking 1}] \
{
set msg "$Pgm error: "
set msg [append msg "Cannot configure serial port\n"]
set msg [append msg "$GlobalParam(Device)"]
Tattle $msg
return $msg
}
return ""
}
###################################################################
# Return the preamble for messages sent from computer to radio.
###################################################################
proc MsgPreamble { } \
{
global RadioAddress
global PCAddress
# byte 0 = FE
# byte 1 = FE
# byte 2 = (radio's unique address)
# byte 3 = (computer's address)
set preamble [ binary format "H2H2H2H2" fe fe \
$RadioAddress $PCAddress ]
return $preamble
}
###################################################################
#
# Send "command" to radio.
# Write command to error stream if Debug flag is set.
#
###################################################################
proc SendCmd { Sid command } \
{
global GlobalParam
set cmd [MsgPreamble]
append cmd $command
append cmd [binary format "H2" fd]