File: api2.tcl

package info (click to toggle)
tk2 1.1-10
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 356 kB
  • ctags: 182
  • sloc: tcl: 6,182; makefile: 44; sh: 3
file content (1678 lines) | stat: -rwxr-xr-x 33,909 bytes parent folder | download | duplicates (5)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
####################################################################
# This file is part of tk2, a utility program for the
# ICOM IC-R2 receiver.
# 
#    Copyright (C) 2001, 2002, Bob Parnass
# 
# tk2 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.
# 
# tk2 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 tk2; 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		252
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)		03
set ImageAddr(MemoryOffset)		03
set ImageAddr(MemoryModes)		06
set ImageAddr(MemoryToneCode)		06
set ImageAddr(MemorySkip)		07
set ImageAddr(MemoryToneFlag)		07
set ImageAddr(MemorySteps)		07
set ImageAddr(SearchFreqFirst)		0C80
set ImageAddr(SearchDuplexFirst)	0C83
set ImageAddr(SearchOffsetFirst)	0C83
set ImageAddr(SearchModeFirst)		0C86
set ImageAddr(SearchToneFlagFirst)	0C87
set ImageAddr(SearchStepFirst)		0C87
set ImageAddr(SearchFreqSecond)		0C88
set ImageAddr(SearchDuplexSecond)	0C8B
set ImageAddr(SearchOffsetSecond)	0C8B
set ImageAddr(SearchModeSecond)		0C8E
set ImageAddr(SearchToneFlagSecond)	0C8F
set ImageAddr(SearchStepSecond)		0C8F
set ImageAddr(BandStackFreq)		0E10 ;# 10 of them
set ImageAddr(BandStackDuplex)		0E13
set ImageAddr(BandStackOffset)		0E13
set ImageAddr(BandStackModes)		0E16 ;# 10 of them
set ImageAddr(BandStackToneCode)	0E16
set ImageAddr(BandStackSkip)		0E17
set ImageAddr(BandStackToneFlag)	0E17
set ImageAddr(BandStackSteps)		0E17
set ImageAddr(DialStep)			0E60
set ImageAddr(Resume)			0E62
set ImageAddr(Pause)			0E63
set ImageAddr(BankScan)			0E65
set ImageAddr(Beep)			0E68
set ImageAddr(Lamp)			0E69
set ImageAddr(AutoOff)			0E6A
set ImageAddr(PowerSave)		0E6B
set ImageAddr(Monitor)			0E6C
set ImageAddr(DialAccel)		0E6D
set ImageAddr(UserComment)		0FA0
set ImageAddr(FileVersion)		0FB0


set Band(USA,0,low)		.005
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)		107.995
set Band(USA,3,low)		76.000	;# Euro models
set Band(USA,3,high)		76.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)		1599.995

##########################################################
# Translation tables
##########################################################

# 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(9)		"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)		"9"

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(?) "3"

set RMode(0) NFM
set RMode(1) WFM
set RMode(2) AM
set RMode(3) ?

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



set ToneFlag(0)	off
set ToneFlag(1)	tsql

set RToneFlag(off)	0
set RToneFlag(tsql)	1


# 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/.tk2rc" $Home]
			set labelfile [format "%s/.tk2la" $Home]

			set DisplayFontSize "Courier 56 bold"
			}
		{macintosh} \
			{

			# Configuration file should be
			# named $HOME/.tk2rc

			# Use forward slashes within Tcl/Tk
			# instead of colons.

			set Home $env(HOME)
			regsub -all {:} $Home "/" Home
			set rcfile [format "%s/.tk2rc" $Home]
			set labelfile [format "%s/.tk2la" $Home]

			# The following font line may need changing.
			set DisplayFontSize "Courier 56 bold"
			}
		{windows} \
			{

			# Configuration file should be
			# named $tk2/tk2.ini
			# Use forward slashes within Tcl/Tk
			# instead of backslashes.

			set Home $env(tk2)
			regsub -all {\\} $Home "/" Home
			set rcfile [format "%s/tk2.ini" $Home]
			set labelfile [format "%s/tk2.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

	catch {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 IC-R2"
	grab set .pbw
	set p [MakeWaitWindow .pbw.g 0 PaleGreen]
	set pc 0
	gauge_value $p $pc
	update


	set db 0

	# Open serial port.
	OpenDevice

	# 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}

			# Close serial port.
			DisableCControl

			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.
		set code 1
		} \
	else \
		{
		# Data xfer suceeded.
		set code 0
		}

	# Zap the progress bar.
	grab release .pbw
	catch {destroy .pbw}

	# Close serial port.
	DisableCControl

	return $code
}


##########################################################
# 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 ""

	# Open serial port.
	OpenDevice


	SendCloneOut

	# For each message.
	for {set i 0} {$i < 500} {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
			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

			# Close serial port.
			DisableCControl

			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

	# Close serial port.
	DisableCControl

	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
#	This procedure exits if there is an error in opening or
#	configuring the serial port.
#
##########################################################

proc OpenDevice {} \
{
	global Pgm
	global GlobalParam
	global Sid
	global tcl_platform

	set msg ""

	 set platform $tcl_platform(platform) 
	 switch -glob $platform \
		{
		{unix} \
			{

			if [ catch { open $GlobalParam(Device) "r+"} \
				Sid] \
				{
				set msg "Error while trying to open "
				append msg "serial port "
				append msg $GlobalParam(Device)
				}
			}
		{macintosh} \
			{
			if [ catch { open $GlobalParam(Device) "r+"} \
				Sid] \
				{
				set msg "Error while trying to open "
				append msg "serial port "
				append msg $GlobalParam(Device)
				}
			}
		{windows} \
			{
			if [ catch { open $GlobalParam(Device) RDWR} \
				Sid] \
				{
				set msg "Error while trying to open "
				append msg "serial port "
				append msg $GlobalParam(Device)
				}
			}
		default \
			{
			set msg "$Pgm error: Platform $platform "
			append msg "not supported."
			}
		}

	waiter 500

	# If port opened ok,
	if { $msg == "" } \
		{
		# Set up the serial port parameters (similar to stty)
		if {[SetSerialP n]} \
			{
			set msg "$Pgm error: "
			append msg "Cannot configure serial port\n"
			append msg "$GlobalParam(Device)"
			# Close serial port.
			DisableCControl
			}
		}

	if {$msg != ""} \
		{
		Tattle $msg
		tk_dialog .opnerror "Serial port error" \
			$msg error 0 OK
		exit
		}
	waiter 1000

	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]


	if { $GlobalParam(Debug) > 0 } \
		{
		binary scan $cmd "H*" s

		# Insert a space between each pair of hex digits
		# to improve readability.

		regsub -all ".." $s { \0} s
		set msg ""
		set msg [ append msg "---> " $s]
		Tattle $msg
		}

	# Write data to serial port.

	puts -nonewline $Sid $cmd
	flush $Sid
	return
}

###################################################################
# Interrogate radio for version/model/user information
#
# Returns:
#	- a 4 byte version of IC-R2 we have.
#	- empty string if error occurred.
#
# Notes:
#	My IC-R2 returns this string:
#	21 27 00 01 20 20 20 20 ... 20 05 09 00
###################################################################
proc GetModelInfo { } \
{
	global GlobalParam
	global Sid

	# Open serial port.
	OpenDevice

	set cmd [ binary format "H2H2H2H2H2" E0 00 00 00 00 ]

	SendCmd $Sid $cmd

	while {1} \
		{
		# Read messages until we find the
		# one which matches this request.

		set line [ReadRx]

		set len [string length $line]
		set cn [string range $line 0 0]
		binary scan $cn "H*" cn

		# If this is a response to our request.
		if {$cn == "e1"} {break}

		# If we got an NG message from the radio.
		if {$cn == "fa"} {break}
		}

	set len [string length $line]

	# Check if radio sent NG msg. 
	if {$len == 1}  \
		{
		set line ""
		} \
	else \
		{
		set line [string range $line 1 4 ]
		}

	binary scan $line "H*" x
	set GlobalParam(RadioVersion) $x
#	puts stderr "GetModelInfo: RadioVersion= $x"


	# Close serial port.
	DisableCControl

	return $line
}

###################################################################
# Read a CI-V message from the serial port.
#
# Inputs:
#	any	- 0 means ignore messages with a "from address"
#		field which indicates the message is from
#		this computer.
#		- 1 means return any message
#
# Strip off the 2 address bytes.
#
# Returns: the message without the address fields.
###################################################################
proc ReadRx { {any 0} } \
{
	global GlobalParam

	global RadioAddressHex
	global PCAddressHex

	set ignored "ignoring previous echo msg from the radio."

	set line {} 

	while { 1 } \
		{
		# Read message from the bus.

		set line [ReadCIV]

		if { [string length $line] == 0} \
			{
			# Got a read error.
			break
			}

		# Examine the address bytes.
		set to [string range $line 0 0]
		set from [string range $line 1 1]

		if { ([string compare -nocase -length 1 \
			$to $PCAddressHex] != 0) \
			&& ([string compare -nocase -length 1 \
			$to $RadioAddressHex] != 0)} \
			{
			puts stderr "ReadRx: UNKNOWN MESSAGE"
			continue;
			}

		if { $any == 0 } \
			{
			if { [string compare -nocase -length 1 \
				$from $PCAddressHex] == 0} \
				{
				# This message is from us,

				# so ignore it and read again.
				continue
				} \
			} 

		# Strip of the address bytes.
		set line [string range $line 2 end]
		set len [string length $line]

		break
		}
	return $line
}


###################################################################
# Read a CI-V data transfer message from the serial port.
#
# INPUTS:
#	any	- 0 means ignore messages with a "from address"
#		field which indicates the message is from
#		this computer.
#		- 1 means return any message
#
# DESCRIPTION:
#	Read a data transfer message.
#	Calculate a checksum and compare it to the
#		checksum in the message.
#	Return an empty message if there is an error.
#	Strip off the 2 address bytes.
#
# Returns: the data transfer message without the address fields.
###################################################################
proc ReadXferRx { {any 0} } \
{
	global GlobalParam

	global RadioAddressHex
	global PCAddressHex

	set ignored "ignoring previous echo msg from the radio."

	set line {} 

	while { 1 } \
		{
		# Read message from the bus.

		set line [ReadCIV]

		if { [string length $line] == 0} \
			{
			# Got a read error.
			return ""
			}

		# Examine the address bytes.
		set to [string range $line 0 0]
		set from [string range $line 1 1]

		if { ([string compare -nocase -length 1 \
			$to $PCAddressHex] != 0) \
			&& ([string compare -nocase -length 1 \
			$to $RadioAddressHex] != 0)} \
			{
			puts stderr "ReadRx: UNKNOWN MESSAGE"
			continue;
			}

		if { $any == 0 } \
			{
			if { [string compare -nocase -length 1 \
				$from $PCAddressHex] == 0} \
				{
				# This message is from us,

				# so ignore it and read again.
				continue
				} \
			} 
		# Got a message.
		break
		}


	# Strip off the from and to address bytes.
	set line [string range $line 2 end]

	return $line
}


###################################################################
# Read a CI-V message from the serial port.
#
# Returns:
#		The message unless there was an error.
#		The empty string if there was an error.
###################################################################
proc ReadCIV { } \
{
	global GlobalParam
	global Sid


	set collision_error false

	# Skip the 2 byte "fe fe" preamble
	read $Sid 1
	read $Sid 1

	set line ""


	while { 1 } \
		{
		set b [read $Sid 1]

		# A byte of hexadecimal fc means there was an
		# error, usually a collision.

		# Note: I have observered that the radio
		# usually sends 3 consecutive fc bytes after
		# a CIV collision.   Because fc should never appear
		# in the IC-R75 data stream, we consider it 
		# an error whenever we see even a single fc byte.
		#        - Bob Parnass, 2/12/2002

		if { [string compare -nocase -length 1 $b \xfc] == 0} \
			{
			# Got an error, but continue reading bytes
			# until we get an end of message byte fe.

			set collision_error true
			set line [append line $b]
			} \
		elseif { [string compare -nocase -length 1 $b \xfd] == 0} \
			{
			# Got the end of message code byte.
			break
			} \
		elseif { [string compare -nocase -length 1 $b \xfe] == 0} \
			{
			; # Ignore leading preamble bytes.
			} \
		else \
			{
			set line [append line $b]
			}
		}

	if { $GlobalParam(Debug) > 0 } \
		{
		set msg "<--- "
		binary scan $line "H*" x

		regsub -all ".." $x { \0} x

		set msg [append msg $x]
		Tattle $msg
		}

	if { $collision_error == "true" } \
		{
		puts stderr "ReadCIV: collison error."
		set line ""
		}
	return $line
}

###################################################################
# 
# Convert an ASCII string to binary.
# The ASCII string uses two consecutive bytes, e.g., E3, to represent
# one byte of the binary string, e.g. \xE3.
#
# INPUT:	unpacked string
# RETURNS:	packed string
###################################################################

proc PackString { in } \
{
	global Hex2Digit

	set len [string length $in]

	set out ""
#	puts stderr "len $len, anum: "

	for {set i 0} {$i < $len} {incr i 2} \
		{
		set j $i 
		set left [string range $in $j $j]
		incr j 
		set right [string range $in $j $j]

		binary scan $left "H2" ileft
		set dleft $Hex2Digit($ileft)

		binary scan $right "H2" iright
		set dright $Hex2Digit($iright)

		set s ""
		append s $dleft $dright
#		puts -nonewline stderr "$s "
		set hnum [binary format "H2" $s]

		append out $hnum

		# binary scan $left "H*" cl
		# binary scan $right "H*" cr

		# puts stderr "cl= $cl, cr= $cr, num= $num, ileft= $ileft, iright= $iright"
		}
	return $out
}


###################################################################
# 
# Convert a binary string to an ASCII string.
# The ASCII string uses two consecutive bytes, e.g., E3, to represent
# one byte of the binary string, e.g. \xE3.
#
# INPUT:	packed string
# RETURNS:	unpacked string
###################################################################

proc UnpackString { in } \
{
	global Digit2Hex

	set len [string length $in]

	set out ""

	for {set i 0} {$i < $len} {incr i} \
		{
         	set c [string index $in $i]
		binary scan $c "H2" s
		set s [string toupper $s]
         	set left [string index $s 0]
         	set right [string index $s 1]


		set dleft $Digit2Hex($left)
		set dright $Digit2Hex($right)
#		puts stderr "s= $s, $dleft $dright"

		append out [binary format "H2H2" $dleft $dright]
		}
	return $out
}

###################################################################
# Send the radio a command to accept memory data.
###################################################################
proc SendCloneIn { } \
{
	global GlobalParam
	global Sid


	set cmd [ binary format "H2" E3 ]
	append cmd [ binary format "H*" $GlobalParam(RadioVersion) ]

	SendCmd $Sid $cmd

	# Read the echo

	if { [ReadEcho $cmd] } \
		{
		# Error.
		# We did not read back what we wrote.
		puts stderr "SendCloneIn: Error while reading echo."
		}
	return
}


###################################################################
# Send the radio a command to send memory data to computer.
###################################################################
proc SendCloneOut { } \
{
	global GlobalParam
	global Sid


	set cmd [ binary format "H2" E2 ]
	append cmd [ binary format "H*" \
		$GlobalParam(RadioVersion) ]

	SendCmd $Sid $cmd

	# Read the echo

	if { [ReadEcho $cmd] } \
		{
		# Error.
		# We did not read back what we wrote.
		puts stderr "SendCloneOut: Error while reading echo."

		}
	return
}


###################################################################
# Send the radio a data termination command.
###################################################################
proc SendTermination { } \
{
	global GlobalParam
	global Sid


	set cmd [ binary format "H2" E5 ]
	append cmd "Icom Inc."

	SendCmd $Sid $cmd
	return
}


###################################################################
# Interrogate radio for version/model/user information
#
# Returns:
#	- 0 = no errors
#	- otherwise, error
#
# Notes:
###################################################################
proc GetTerminationResult { } \
{
	global GlobalParam
	global Sid


	while {1} \
		{
		# Read messages until we find the
		# one which matches request.

		set line [ReadRx]

		set len [string length $line]
		set cn [string range $line 0 0]
		binary scan $cn "H*" cn

		# If this is a termination result....
		if {$cn == "e6"} {break}
		}
	set x [string range $line 1 1 ]

	set code -1
	if { [string compare -nocase -length 1 $x \x00] == 0} \
		{
		set code 0
		} 
	return $code
}

proc DumpBinary { bstring } \
{
	binary scan $bstring "H*" s

	# Insert a space between each pair of hex digits
	# to improve readability.

	regsub -all ".." $s { \0} s
	return $s
}

proc ReadEcho { sent } \
{
	global GlobalParam
	global Pgm

	if {$GlobalParam(CableEchos) == 0} {return 0}

	set echo [ReadRx 1]
	if { [string compare $sent $echo] } \
		{
		# Error.
		# We did not read back what we wrote.
		puts stderr "$Pgm: Error while reading echo from message $i."
		return 1

		}
	return 0
}


###################################################################
# Set the serial port parameters.
#
#	proc SetSerialP { parity }
#
# INPUTS:
#	parity	-o or e or n
#
#
# RETURNS:
#	0	-ok
#	-1	-error occurred
#
# NOTES:
#
#	Requires tcl/tk 8.4 or later to support the ttycontrol
#	option on fconfigure.
#
#
# From: Rolf.Schroedter@dlr.de Wed Dec 18 00:44:18 2002
#
# The serial stuff in Windows is indeed more complicated than in  Unix.
# You can see this from the volume of source code.
#
# In Windows the -mode "string" interpretation resets
# all TTY states to their default values.
# A simple workaround for you is to set the baud rate
# first and only then the -ttycontrol. The following should work:
#
#	fconfigure $Sid -buffering none -translation binary  \
#		-blocking 1 \
#		-mode 9600,$parity,8,1 -ttycontrol {DTR 1 RTS 0}
#
# Or even
#	fconfigure $Sid -buffering none -translation binary \
#		-blocking 1
#	fconfigure $id	-mode 9600,$parity,8,1
#	fconfigure $id -ttycontrol {DTR 1 RTS 0}
#
# I'll have a look whether there is a way to correct this for
# future Tcl versions.
# On the other hand setting -mode is an elementary thing
# which reconfigures the UART hardware and should not be
# done during communication.
#
###################################################################
proc SetSerialP { parity } \
{
	global Pgm
	global Sid
	global GlobalParam

	set code 0

	# Set up the serial port parameters (similar to stty)
	if {($GlobalParam(DTRline) < 0) \
		&& ($GlobalParam(RTSline) < 0)} \
		{
		if { [catch {fconfigure $Sid \
			-buffering none \
			-translation binary \
			-handshake none \
			-mode 9600,$parity,8,1 -blocking 1 \
			-ttycontrol {DTR 0 RTS 0} }]} \
			{
			set code -1
			}
		} \
	elseif {($GlobalParam(DTRline) < 0) \
		&& ($GlobalParam(RTSline) > 0)} \
		{
		if { [catch {fconfigure $Sid \
			-buffering none \
			-translation binary \
			-handshake none \
			-mode 9600,$parity,8,1 -blocking 1 \
			-ttycontrol {DTR 0 RTS 1} }]} \
			{
			set code -1
			}
		} \
	elseif {($GlobalParam(DTRline) > 0) \
		&& ($GlobalParam(RTSline) < 0)} \
		{
		if { [catch {fconfigure $Sid \
			-buffering none \
			-translation binary \
			-handshake none \
			-mode 9600,$parity,8,1 -blocking 1 \
			-ttycontrol {DTR 1 RTS 0} }]} \
			{
			set code -1
			}
		} \
	else \
		{
		if { [catch {fconfigure $Sid \
			-buffering none \
			-translation binary \
			-handshake none \
			-mode 9600,$parity,8,1 -blocking 1 \
			-ttycontrol {DTR 1 RTS 1} }]} \
			{
			set code -1
			}
		}

	# Delay a half second to give serial port
	# time to settle.
	waiter 500
	return $code
}