'******** PROGRAM TO PROGRAM AND TEST A TYPE 7815 TRANSPONDER (VLCT) ********

'Nov 96 board test a sep prog which is shelled
'       New 23 address slot selection
'NEW PROGRAMMING SCHEME WITHOUT MAGNET

'*** Updated (actually downgraded) by PJF on 31.10.97 to do the following:
'1. Allow only VLCT Programming to occur - making the ANT redundant
'2. Convert for use with the older-type VLCTs with magnets

'*** Updated file: 7815PRG4.BAS
'*** Influenced by VLCT-TZZ.BAS


DECLARE SUB WaitNotice ()
DECLARE SUB DisplayTAT (ANTComms%, VLCTAddress%, TZnum%)
DECLARE SUB DelayPeriod (DelayTime!)
DECLARE SUB FlushBuffer (CommsRef%, BufferLimit!)
DECLARE SUB SlowTATSetup (ANTComms%, VLCTAddress%, TZnum%)
DECLARE SUB ComInstruction (ProgFlag%)
DECLARE SUB FindComPorts (NumOfComPorts%)
DECLARE SUB IntroMessage (NumOfComPorts%)
DECLARE SUB Logo ()
DECLARE FUNCTION SetupFSK% (FSKComms%)
DECLARE SUB ErrorMessage (ErrNum%)
DECLARE SUB ErrorSub ()
DECLARE FUNCTION SetupANT% (ANTComms%, NumOfComPorts%)
DECLARE SUB Delay (DelayTime!)

'*** Common Shared variables
COMMON SHARED ErrorFlag%, ComPort%

'*************** Specifying & initialising Event traps ********************
ON ERROR GOTO ErrorHandler
'**************************************************************************


JustProgrammedFlag% = 0
flagarr = 0
CLS :

'--------TRANSLATION ARRAYS -----------------------------
'-----non array values ANT----23 addresses-----------------
DATA "00,7F,21,00,11,","00,2E,73,00,01,","00,54,FB,00,04,"
DATA "00,4F,1F,00,11,","00,5E,75,00,01,","00,24,F9,00,04,"
DATA "00,1F,1D,00,11,","00,8E,77,00,01,","00,F4,F7,00,03,"
DATA "00,EF,1B,00,10,","00,BE,79,00,01,","00,C4,F5,00,03,"
DATA "00,BF,19,00,10,","00,EE,7B,00,01,","00,94,F3,00,03,"
DATA "00,8F,17,00,10,","00,1E,7D,00,02,","00,64,F1,00,03,"
DATA "00,5F,15,00,10,","00,4E,7F,00,02,","00,34,EF,00,03,"
DATA "00,2F,13,00,10,","00,7E,81,00,02,","00,04,ED,00,03,"
DATA "00,FF,11,00,0F,","00,AE,83,00,02,","00,D4,EB,00,02,"
DATA "00,CF,0F,00,0F,","00,DE,85,00,02,","00,A4,E9,00,02,"
DATA "00,9F,0D,00,0F,","00,0E,87,00,03,","00,74,E7,00,02,"
DATA "00,6F,0B,00,0F,","00,3E,89,00,03,","00,44,E5,00,02,"
DATA "00,3F,09,00,0F,","00,6E,8B,00,03,","00,14,E3,00,02,"
DATA "00,0F,07,00,0F,","00,9E,8D,00,03,","00,E4,E1,00,01,"
DATA "00,DF,05,00,0E,","00,CE,8F,00,03,","00,B4,DF,00,01,"
DATA "00,AF,03,00,0E,","00,FE,91,00,03,","00,84,DD,00,01,"
DATA "00,7F,01,00,0E,","00,2E,93,00,04,","00,54,DB,00,01,"
DATA "00,4E,FF,00,0E,","00,5E,95,00,04,","00,24,D9,00,01,"
DATA "00,1E,FD,00,0E,","00,8E,97,00,04,","00,F4,D7,00,00,"
DATA "00,EE,FB,00,0D,","00,BE,99,00,04,","00,C4,D5,00,00,"
DATA "00,BE,F9,00,0D,","00,EE,9B,00,04,","00,94,D3,00,00,"
DATA "00,8E,F7,00,0D,","00,1E,9D,00,05,","00,64,D1,00,00,"
DATA "00,5E,F5,00,0D,","00,4E,9F,00,05,","00,34,CF,00,00,"
DIM loopdelay$(23): DIM Pulse2Pulse$(23): DIM turnaround$(23)
FOR n = 1 TO 23
READ a$: loopdelay$(n) = a$
READ Pulse2Pulse$(n)
READ turnaround$(n)
NEXT n
'------------ values of R6 for each address -
DATA 6,10,14,18,22,26,30,34,38,42,46,50,54
DIM add$(14)
FOR n = 13 TO 1 STEP -1
	READ add
	add$(n) = CHR$(add)
NEXT n
'--------------------------- array uses a vector ------------------
DATA 11,8,7,6,5,4,3,2,14
DIM ADDVECT(9)
FOR n = 1 TO 9: READ ADDVECT(n): NEXT n
'-- array for TZ translation - actual divisors TzFrq(N) -1 actually used
DIM TzFrq(10)
DATA 66,64,62,60,58,56,54,53,52
FOR n = 1 TO 9
	READ TzFrq(n)
	TzFrq(n) = TzFrq(n) - 1
NEXT n

'--------------------------- RX ANT freq number x5Hz ---------------
DIM FTAB$(9)
DIM Freq$(9)
DATA "1D,98,","1E,85,","1F,81,","20,8D,","21,AD,","22,E1,","24,2B,"
DATA "24,CD,","25,8F,"
FOR n = 1 TO 9: READ a$:
FTAB$(n) = a$
Freq$(n) = "2390/10,B8," + a$ + a$ + "00,00,"
'PRINT fREQ$(n)
NEXT n
'-----------------------  array delays for each address for ANT ---
DATA "D6,7A,0,1,","06,7C,0,2,","36,7E,0,2,","66,80,0,2,","96,82,0,2,"
DATA "C6,84,0,2,","F6,86,0,2,","26,88,0,3,","56,8A,0,3,"
DIM Row$(9):
FOR n = 1 TO 9
READ Row$(n)
NEXT n
'-----------------------  array delays for Column address for ANT --
DATA "A6,78,0,1,","D6,7A,0,1,","06,7C,0,2,","36,7E,0,2,","66,80,0,2,","96,82,0,2,"
DATA "C6,84,0,2,","F6,86,0,2,","26,88,0,3,"
DIM Col$(9): FOR n = 1 TO 9: READ Col$(n): NEXT n
'-----------------------  array delay for time after row for fxed delay
DATA "B0,12,0,1,","80,10,0,1,","50,0E,0,1,","20,0C,0,1,","F0,0A,0,0,","C0,08,0,0,"
DATA "90,06,0,0,","60,04,0,0,","30,02,0,0,"
DIM RLEFT$(9): FOR n = 1 TO 9: READ RLEFT$(n): NEXT n
'----------------------- array delay for time after col before RX
DATA "28,17,0,2,","F8,15,0,1,","C8,13,0,1,","98,11,0,1,","68,0F,0,1,"
DATA "38,0D,0,1,","08,0B,0,1,","D8,09,0,0,","A8,07,0,0,"
DIM CLEFT$(9): FOR n = 1 TO 9: READ CLEFT$(n): NEXT n
'---------------------------------------------------------------------
PW$ = "60,09,9A,00,00,"         'TODL=70=01110000 b6=pulse b5=trig out
PWNT$ = "40,9,9A,0,0,"                      'b4=flash LED active
								'time = 0000099Ah/1229000 = 2ms



'**************************** Start of Program **********************
DEL! = .3
ATT$ = "12"
NumOfComPorts% = 1

CALL Logo
'CALL FindComPorts(NumOfComPorts%)
CALL IntroMessage(NumOfComPorts%)

MainMenu:

CALL Logo
Row% = 20: Col% = 8
LOCATE Row%, Col%
PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%
PRINT " Ί ENSURE THE MAGNET IS IN PLACE 15 SECONDS BEFORE PROGRAMMING  Ί "
LOCATE CSRLIN, Col%
PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "


Row% = 10: Col% = 10
LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%: PRINT " Ί 1/ Program Transponder (Programming Box Switch = PROGRAM) Ί "
LOCATE CSRLIN, Col%: PRINT " Ί 2/ Quit to DOS                                            Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "

'*** Display the last ID programmed
IF JustProgrammedFlag% = 1 THEN
		LOCATE CSRLIN, Col%: PRINT " Ί            Last Transponder programmed: A";
		PRINT LTRIM$(STR$(ProgrammedAddr%)); "-"; LTRIM$(STR$(ProgrammedTZ%));
		IF ProgrammedAddr% <= 9 THEN
				PRINT "              Ί "
			ELSE
				PRINT "             Ί "
		END IF
	ELSE
		LOCATE CSRLIN, Col%: PRINT " Ί          A Transponder has yet to be programmed           Ί "
END IF

LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

'*** Flush keyboard buffer
DO: LOOP UNTIL INKEY$ = ""

DO
	'*** This is a bit clumsy, but for speed, I'm trying not to disrupt
	'*** JEK's program flow too much. To explain, when Display$ = ""
	'*** then a VLCT test is not in progress, and the program is simply
	'*** waiting for a key press.
	IF Display$ <> "" THEN

			'*** If measuring the TAT (verifying correct ID) then display..
			CALL DisplayTAT(ANTComms%, VLCTAddress%, TZnum%)

			'*** Check to see that another menu option has not been selected
			'*** If not, then re-trigger a TAT measurement
			KeyPress$ = INKEY$
			IF KeyPress$ <> "1" AND KeyPress$ <> "2" AND KeyPress$ <> "3" THEN
					PRINT #ANTComms%, "5";                  'START
					CALL DelayPeriod(3)
					QuitMainMenu% = 0
				ELSE
					QuitMainMenu% = 1
			END IF
		ELSE
			'*** If not testing, then simply wait for a keypress
			DO
				KeyPress$ = INKEY$
			LOOP UNTIL KeyPress$ >= ""
			IF KeyPress$ = "1" OR KeyPress$ = "2" OR KeyPress$ = "3" THEN
					QuitMainMenu% = 1
				ELSE
					QuitMainMenu% = 0
			END IF
	END IF

LOOP UNTIL QuitMainMenu% = 1

'*** Flush the keyboard buffer
DO: LOOP UNTIL INKEY$ = ""

CLOSE #ANTComms%

COLOR 7, 0
flagping = 0
FLAG = 0

IF KeyPress$ = "1" THEN GOTO eet
'IF KeyPress$ = "2" THEN GOTO TestT          'This will not be used
IF KeyPress$ = "2" THEN GOTO QuitToDOS

CALL Logo

Display$ = ""

'******************************** Programming ****************************
eet:
ProgFlag% = 1
JustProgrammed% = 1
TestFlag% = 0

'*** If the number of Com Ports is only 1, then ensure the Programming Unit
'*** is connected to Com1
'IF NumOfComPorts% = 1 THEN
'        CALL Logo
'        CALL ComInstruction(ProgFlag%)
'END IF

Display$ = ""
GOTO ProgEEPROM
'**************************************************************************


'********************************* Testing ********************************
TestT:
ProgFlag% = 0

'*** If the number of Com Ports is only 1, then ensure the ANT
'*** is connected to Com1
IF NumOfComPorts% = 1 THEN
		CALL Logo
		CALL ComInstruction(ProgFlag%)
END IF

CLS
GOSUB TPDRTST

Display$ = "  TESTING TPDR   A" + STR$(VLCTAddress%) + " TZ" + STR$(TZnum%) + " "

JustProgrammed% = 0

GOTO MainMenu
'**************************************************************************


'*********************************** Quitting *****************************
QuitToDOS:

'*** Deal with ANT set-up requirements in order to reset the ANT
'*** ...but only if a least two Com Ports exist, or if only 1 Com Port exists
'*** but the last action was TEST.
IF (NumOfComPorts% = 2) OR (TestFlag% = 1) THEN
	IF SetupANT%(ANTComms%, NumOfComPorts%) <> 0 THEN
			CLOSE
			END
	END IF
END IF
CLOSE

'*** Set the color and clear the screen
COLOR 7, 0
CLS

END
'***************************************************************************

'~~~~~~~~~~~ EEPROM PROGRAMMING and TEST FOR NON ARRAY TPDR ~~~~~~~~
'------------------ PROGRAMMING ---------------------------------
ProgEEPROM:

'*** Deal with VLCT Programming Box set-up requirements.
'*** Quit if an error has occured (a non-zero value returned)
IF SetupFSK%(FSKComms%) <> 0 THEN
	CLOSE FSKComms%
	GOTO MainMenu
END IF

'*** If the PC only has one Com Port, then ignore the ANT during Programming
IF NumOfComPorts% = 2 THEN
	CALL Logo

	Row% = 10: Col% = 10
	LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
	LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
	LOCATE CSRLIN, Col%: PRINT " Ί     INITIALISING TYPE 7343 ACOUSTIC NAVIGATION TESTER     Ί "
	LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
	LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

	'*** Deal with ANT set-up requirements.
	'*** Quit if an error has occured (a non-zero value returned)
	IF SetupANT%(ANTComms%, NumOfComPorts%) <> 0 THEN
			CLOSE ANTComms%
			NoANT% = 1
		ELSE
			NoANT% = 0
	END IF

	flagarr = 0

	IF NoANT% = 0 THEN GOSUB antping   'sets up ant to listen for pings
END IF


CALL Logo
Row% = 20: Col% = 8
LOCATE Row%, Col%
PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%
PRINT " Ί ENSURE THE MAGNET IS IN PLACE 15 SECONDS BEFORE PROGRAMMING Ί "
LOCATE CSRLIN, Col%
PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "


GOSUB seladdfrq   'returns with VLCTAddress% and frq


'*** Assign Address and TZ reply to different variables, which will be used
'*** to display the previously programmed ID.
ProgrammedAddr% = VLCTAddress%
ProgrammedTZ% = TZnum%

CALL Logo
Row% = 10: Col% = 22
LOCATE Row%, Col%
PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%
PRINT " Ί       Remove the Magnet,       Ί "
LOCATE CSRLIN, Col%
PRINT " Ί                                Ί "
LOCATE CSRLIN, Col%
PRINT " Ί         Wait 4 seconds,        Ί "
LOCATE CSRLIN, Col%
PRINT " Ί                                Ί "
LOCATE CSRLIN, Col%
PRINT " Ί ...then hit any key to Program Ί "
LOCATE CSRLIN, Col%
PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

DO: LOOP UNTIL INKEY$ = ""
DO: LOOP UNTIL INKEY$ <> ""


'Output$ = CHR$(0) + "VLCT" + add$(VLCTAddress%) + CHR$(frq) + ">"

Byte1$ = CHR$(255) + CHR$(255) + "<"
Byte2$ = add$(VLCTAddress%) + CHR$(frq) + ">"
OutByte$ = Byte1$ + Byte2$

'*** TESTING
'PRINT "*"; OutByte$; "*"
'DO: LOOP UNTIL INKEY$ = ""
'DO: LOOP UNTIL INKEY$ <> ""


PRINT #FSKComms%, OutByte$


CALL Logo
Row% = 12: Col% = 5
LOCATE Row%, Col%
PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%
PRINT " Ί The Transponder will ping three times if programming is successful Ί "
LOCATE CSRLIN, Col%
PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

CALL DelayPeriod(4)
DO: LOOP UNTIL INKEY$ = ""



IF NumOfComPorts% = 2 THEN CLOSE ANTComms%
CLOSE FSKComms%

JustProgrammedFlag% = 1

GOTO MainMenu


'-----------------TEST  ----------------------------------------
TPDRTST:

CALL Logo

Row% = 10: Col% = 10
LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
LOCATE CSRLIN, Col%: PRINT " Ί     INITIALISING TYPE 7343 ACOUSTIC NAVIGATION TESTER     Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

'*** Clear flag associated with Testing
TestFlag% = 0

'*** Deal with ANT set-up requirements (1)
'*** Quit if an error has occured (a non-zero value returned)
IF SetupANT%(ANTComms%, NumOfComPorts%) <> 0 THEN
	CLOSE ANTComms%
	GOTO MainMenu
END IF

'*** Set flag associated with Testing if ANT Comms are OK
TestFlag% = 1

IF JustProgrammed% = 1 THEN GOTO tt1
CALL Logo
GOSUB seladdfrq
tt1:
addr$ = STR$(addno)

'*** The following code uses the ANT LED to check for correct ID...
'loopdelay$ = loopdelay$(VLCTAddress%)
'Pulse2Pulse$ = Pulse2Pulse$(VLCTAddress%)
'turnaround$ = turnaround$(VLCTAddress%)
'RxeveNT$ = "24,02,66,0,0,54,60,64,0,09,01,15,9A,0,0,00,A8,C7,0,12,"
'OET$ = "2400/08,14," + PW$ + Pulse2Pulse$ + PWNT$ + turnaround$ + RxeveNT$
'OUT$ = OET$ + "2390/D,C1,0E,5D," + rxfreq$ + "00,00,"   'freq table at 2390
'OUT$ = OUT$ + "23DA/00,00,80,80,"                       'atten table at 23DA
'OUT$ = OUT$ + "2016/80,01,"                             'pulse disc
'GOSUB OutAnt
'CLOSE #ANTComms%

'*** Display message to wait
CALL WaitNotice

'*** Setup the ANT using front-panel commands for automatic TAT measurements
CALL SlowTATSetup(ANTComms%, VLCTAddress%, TZnum%)

RETURN

'-----------------CHOOSE ADDRESS AND FREQ-------------------------------
seladdfrq:

Row% = 10: Col% = 15
LOCATE Row%, Col%

PRINT "ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»"
LOCATE CSRLIN, Col%
PRINT "Ί                ENTER THREE DIGITS               Ί"
LOCATE CSRLIN, Col%
PRINT "Ί                                                 Ί"
LOCATE CSRLIN, Col%
PRINT "Ί       Digits 1 & 2:   ADDRESS 1 to 13           Ί"
LOCATE CSRLIN, Col%
PRINT "Ί       Digit 3:        TZ FREQUENCY 1 to 9       Ί"
LOCATE CSRLIN, Col%
PRINT "Ί                                                 Ί"
LOCATE CSRLIN, Col%
PRINT "Ί                                                 Ί"
LOCATE CSRLIN, Col%
PRINT "ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ"

FLAG = 0
COLOR 7, 0

LOCATE (CSRLIN - 2), (Col% + 22)
PRINT "A   - ";

DO: LOOP UNTIL INKEY$ = ""
DO
	a$ = INKEY$
	IF a$ = "" THEN a$ = " "    'Avoids run-time error with ASC function
LOOP UNTIL (ASC(a$) >= 48) AND (ASC(a$) <= 49)

addd$ = a$

LOCATE CSRLIN, (Col% + 24)
PRINT a$;

IF a$ = "0" THEN
	DO
		B$ = INKEY$
		IF B$ = "" THEN B$ = " "    'Avoids run-time error with ASC function
	LOOP UNTIL (ASC(B$) >= 49) AND (ASC(B$) <= 57)
END IF

IF a$ = "1" THEN
	DO
		B$ = INKEY$
		IF B$ = "" THEN B$ = " "    'Avoids run-time error with ASC function
	LOOP UNTIL (ASC(B$) >= 48) AND (ASC(B$) <= 51)
END IF

IF a$ = "2" THEN
	DO
		B$ = INKEY$
		IF B$ = "" THEN B$ = " "    'Avoids run-time error with ASC function
	LOOP UNTIL (ASC(B$) >= 48) AND (ASC(B$) <= 51)
END IF

VLCTAddress% = VAL(addd$ + B$)

LOCATE CSRLIN, (Col% + 25)
PRINT B$;

DO
	C$ = INKEY$
	IF C$ = "" THEN C$ = " "    'Avoids run-time error with ASC function
LOOP UNTIL (ASC(C$) >= 49) AND (ASC(C$) <= 57)

LOCATE CSRLIN, (Col% + 27)
PRINT C$;

TZnum% = VAL(C$)

frq = TzFrq(TZnum%)
rxfreq$ = FTAB$(TZnum%)

RETURN

'---------------SELECT ADDRESSES---------------------------------
seladdRA:
CALL Logo
Row% = 10: Col% = 15
LOCATE Row%, Col%
PRINT "ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»"
LOCATE CSRLIN, Col%
PRINT "Ί   ENTER THREE DIGITS                      Ί"
LOCATE CSRLIN, Col%
PRINT "Ί   1st  =  ROW ADDRESS     1 to 9          Ί"
LOCATE CSRLIN, Col%
PRINT "Ί   2nd  =  COLUMN ADDRESS  1 to 9          Ί"
LOCATE CSRLIN, Col%
PRINT "Ί   3rd DIGIT TZ FREQUENCY  1 to 9          Ί"
LOCATE CSRLIN, Col%
PRINT "ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ"
FLAG = 0: COLOR 7, 0
DO: a$ = INKEY$
LOOP UNTIL a$ <> ""            'loop til key press
Row = VAL(a$): ZZ = ADDVECT(Row)
IF Row = 0 OR Row > 9 THEN PRINT "error": CALL Delay(1): GOTO seladdRA
LOCATE 14, 40: artpdr$ = "R" + a$: PRINT artpdr$
DO: a$ = INKEY$
LOOP UNTIL a$ <> ""            'loop til key press
COLM = VAL(a$): ZZ = ZZ + ADDVECT(COLM) * 16
IF COLM = 0 OR COLM > 9 THEN PRINT "error": CALL Delay(1): GOTO seladdRA
LOCATE 14, 40: artpdr$ = artpdr$ + " C" + a$ + " ": PRINT artpdr$
DO: a$ = INKEY$
LOOP UNTIL a$ <> ""            'loop til key press
COLOR 7, 0
TZnum% = VAL(a$)
IF TZnum% = 0 THEN PRINT "error": CALL Delay(1): GOTO seladdRA
LOCATE 14, 40: artpdr$ = artpdr$ + "TZ" + STR$(TZnum%) + " ": PRINT artpdr$
CALL Delay(1)

frq = TzFrq(TZnum%)
rxfreq$ = FTAB$(TZnum%)   'frq)

RETURN


'############## SUB ROUTINES ############################

'~~~~~~~~~~~~~~RX SET UP ROUTINE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
antping:  'LOOKS FOR PINGS AT 39062 IF PROGRAMMING SUCCESSFUL ANT FLASHES



freqq$ = "2390/10,B8,1E,84,1E,84,00,00,"   'freq table at 2390

antrxsetup:
RxeveNT$ = "24,02,66,0,0,64,15,9A,0,0,54,A8,C7,FF,12,01,04,CD,0,0,00,A0,3C,0,05,"
OET$ = "2400/06,54,70,09,9A,0,0," + RxeveNT$
OUT$ = OET$ + freqq$                      'freq table at 2390
OUT$ = OUT$ + "23DA/00,80,80,"                       'atten table at 23DA
OUT$ = OUT$ + "2016/80,01,"                             'pulse disc
GOSUB OutAnt
CLOSE ANTComms%
RETURN


'------------OUTPUTS STRINGS TO ANT------------------------------------

OutAnt:

PRINT #ANTComms%, "5";
SLEEP (1)
PRINT #ANTComms%, "D";
SLEEP (1)
PRINT #ANTComms%, OUT$;
SLEEP (1)
PRINT #ANTComms%, "G";                 'GO
RETURN

'--------------------INITIALISE ANT--------------------------
StartANT:
CLOSE ANTComms%

'*** Deal with ANT set-up requirements.
'*** Quit if an error has occured (a non-zero value returned)
IF SetupANT%(ANTComms%, NumOfComPorts%) <> 0 THEN CLOSE

PRINT #ANTComms%, CHR$(27); CHR$(27); CHR$(27)               'reset ANT
CALL Delay(1.5)
PRINT "start"   ': SHELL "VTERM"
RETURN

'------------------------------- help ----------------------
Help:
CLS : PRINT : PRINT : PRINT " Connect the Serial>FSK box to COM1"
PRINT " Connect COM2 to the ANT unit Serial I/P"
PRINT " Connect the TDR O/P of the ANT to the input of the Serial>FSK box"
PRINT " Connect the O/P of the Serial>FSK box to the TPDR I/P or"
PRINT "          couple to the TDCR using a acoustic coupling"
PRINT "       If programming signal correctly received then the"
PRINT "       TPDR will ping 3 times and the LED on the ANT will flash 3 times"
PRINT "      TRANSPONDER TEST - sends out the interrogation"
PRINT "       if TPDR replies - then very brief flash of LED"
PRINT "       if NO reply detected LED stays on for half a second"
PRINT " identify Sends a signal to the TPDR which makes it give a series"
PRINT " of pulses out on its programmed ZT freq. It tries each in turn"
PRINT " times each. when the ANT flashes you have identified the freq"
PRINT " The pulses represent the telemetry window and the window(s) of the"
PRINT " programmed address. By looking at what comes out you can tell"
PRINT " what it is programmed and whether it is programmed as an array."

PRINT : PRINT : INPUT " press <CR> to continue ", qq$
RETURN

'-----------------------------------------------------------

'the address for a straight TPDR is the value of R6
' addresses
'VLCTAddress%  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
'R0    94 90 86 82 78 74 70 66 62 58 54 50 46 42 38 34 30 26 22 18 14 10  6
'Asc   ^  Z  V  R  N  J  F  B  >  :  6  2  .  *  &  " -- non printable

' FREQ  TZ  1  2  3  4  5  6  7  8  9                                                  '           A  ?  =  ;  9  7  5  4  3
'   Fn     65 63 61 59 57 55 53 52 51  < TZ FREQ =5MHz/2/(Fn+1)
'           A  ?  =  ;  9  7  5  4  3
' EXAMPLE TPDR A14-6 WILL TRANSFER "VLCT*7>" TO THE fsk


'**************************************************************************
'*** ERROR HANDLER
'**************************************************************************
ErrorHandler:
ErrorFlag% = 1
CALL Logo
CALL ErrorSub
RESUME NEXT

' SUBPROGRAM TO TELL THE OPERATOR TO CONNECT THE APPROPRIATE DEVICE TO COM1
'
SUB ComInstruction (ProgFlag%)

'ProgFlag% = 1 when programming, i.e. the Programming Unit should be connected
'ProgFlag% = 0 when testing, i.e. the ANT should be connected

'*** Flush the keyboard buffer
DO: LOOP UNTIL INKEY$ = ""

IF ProgFlag% = 1 THEN
		Row% = 11: Col% = 16
		LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
		LOCATE CSRLIN, Col%: PRINT " Ί Connect the Type 7009 Programming Unit to COM1 Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί             and switch to PROGRAM              Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί                                                Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί Press any key to continue...                   Ί "
		LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "
	ELSE
		Row% = 11: Col% = 21
		LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
		LOCATE CSRLIN, Col%: PRINT " Ί Connect the Type 7343 ANT to COM1 Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί         and switch to TEST        Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί                                   Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί Press any key to continue...      Ί "
		LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "
END IF

DO: LOOP UNTIL INKEY$ = ""
DO: LOOP UNTIL INKEY$ <> ""

END SUB

SUB Delay (DelayTime!)
StartTime! = TIMER
DO
LOOP UNTIL TIMER > (StartTime! + DelayTime!)

END SUB

'******************** SUBPROGRAM TO GENERATE A DELAY **********************
'
SUB DelayPeriod (DelayTime!)

StartTime! = TIMER
DO
	StopTime! = TIMER
LOOP UNTIL StopTime! - StartTime! > DelayTime!

END SUB

'************* SUBPROGRAM TO DISPLAY THE VLCT TURN-AROUND-TIME *************
'************** AND DETERMINE WHETHER THE CURRENT ID IS VALID **************
'
SUB DisplayTAT (ANTComms%, VLCTAddress%, TZnum%)

ANTReply$ = ""

'*** Determine number of characters in Com Port buffer
NumOfChars% = LOC(ANTComms%)

'*** Print characters returned
x% = 1
DO UNTIL LOC(ANTComms%) = 0
	ANTChar$ = INPUT$(1, #ANTComms%)
	'PRINT "("; x%; ")"; ANTChar$;
	ANTReply$ = ANTReply$ + ANTChar$
	x% = x% + 1
LOOP

'*** Determine whether PASS or FAIL
IF RIGHT$(ANTReply$, 6) = "OPTION" THEN
		VLCTStatus$ = "FAIL"
	ELSE
		VLCTStatus$ = "PASS"
		TATString$ = RIGHT$(ANTReply$, 6)
END IF

'*** Display to screen
Row% = 17: Col% = 10

IF VLCTStatus$ = "PASS" THEN
		COLOR 14, 1
		LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
		LOCATE CSRLIN, Col%: PRINT " Ί             TRANSPONDER RESPONDING CORRECTLY              Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί               TURN-AROUND TIME = ";
		PRINT TATString$;
		PRINT " ms                Ί "
	ELSE
		COLOR 14, 4
		LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
		LOCATE CSRLIN, Col%: PRINT " Ί                ";
	 
		'*** Flash the following text
		COLOR 30, 4
		PRINT "TRANSPONDER NOT RESPONDING";
		   
		COLOR 14, 4
		PRINT "                 Ί "

		LOCATE CSRLIN, Col%: PRINT " Ί               ";
		LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
		LOCATE CSRLIN, Col%: PRINT " Ί     CHECK TEST EQUIPMENT OR RE-PROGRAM THE TRANSPONDER    Ί "
END IF

LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
LOCATE CSRLIN, Col%: PRINT " Ί               Identitiy being checked: A";
PRINT LTRIM$(STR$(VLCTAddress%)); "-"; LTRIM$(STR$(TZnum%));

'*** Adjust text according to address (1 or 2 digits)
IF VLCTAddress% <= 9 THEN
		PRINT "               Ί "
	ELSE
		PRINT "              Ί "
END IF

LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

COLOR 14, 1

'*** TESTING
'PRINT "Number of characters in ANT Com Port buffer ="; NumOfChars%
'PRINT "Number of characters in ANTReply$ ="; LEN(ANTReply$)

END SUB

'****************** SUBPROGRAM TO DISPLAY ERROR MESSAGES *******************
'
SUB ErrorMessage (ErrNum%)

'*** ErrNum% selects the text to be displayed

SELECT CASE ErrNum%
	CASE 24
		Row% = 10: Col% = 15
		LOCATE Row%, Col%
		PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
		LOCATE CSRLIN, Col%

		IF ComPort% = 2 THEN
				PRINT " Ί    ANT COMMUNICATION LINK NOT ESTABLISHED      Ί "
			ELSE
				PRINT " Ί  PROGRAMMER COMMUNICATION LINK NOT ESTABLISHED Ί "
		END IF
		LOCATE CSRLIN, Col%
		PRINT " Ί                                                Ί "
		LOCATE CSRLIN, Col%
		PRINT " Ί          Press any key to continue...          Ί "
		LOCATE CSRLIN, Col%
		PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "
	CASE 77
		Row% = 10: Col% = 15
		LOCATE Row%, Col%
		PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
		LOCATE CSRLIN, Col%
		PRINT " Ί          CHECK THAT THE ANT IS SWITCHED ON          Ί "
		LOCATE CSRLIN, Col%
		PRINT " Ί OR THAT IT IS CONNECTED TO THE APPROPRIATE COM PORT Ί "
		LOCATE CSRLIN, Col%
		PRINT " Ί                                                     Ί "
		LOCATE CSRLIN, Col%
		PRINT " Ί Press any key to continue...                        Ί "
		LOCATE CSRLIN, Col%
		PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "
	CASE ELSE
		Row% = 10: Col% = 15
		LOCATE Row%, Col%: PRINT "Unrecognised text number"
		LOCATE CSRLIN, Col%: PRINT "   in ErrorMessage()   "
END SELECT

END SUB

'****** SUBPROGRAM CONTAINING A LIBRARY OF ERROR HANDLERS SELECTED BY ******
'************** THE ERR FUNCTION FOLLOWING A RUN-TIME ERROR ****************
'
SUB ErrorSub

SELECT CASE ERR
	CASE 24     'Device Timeout
		'*** "Device Timeout" error
		CALL ErrorMessage(24)
		SLEEP (5)
	CASE 77
		'*** "ANT Communications Link" error
		CALL ErrorMessage(77)
		SLEEP (5)
	CASE ELSE
		CALL ErrorMessage(2000)
END SELECT

END SUB

'********** DETERMINE WHETHER THE PC HAS ONE OR TWO COM PORTS *************
'
SUB FindComPorts (NumOfComPorts%)

Row% = 10: Col% = 12
LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%: PRINT " Ί                    PC CONFIGURATION                    Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                        Ί "
LOCATE CSRLIN, Col%: PRINT " Ί How many Communication Ports does your Computer have?  Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                        Ί "
LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

'*** Flush the keyboard buffer
DO: LOOP UNTIL INKEY$ = ""

'*** Point to the INKEY$ point on the screen
LOCATE (CSRLIN - 2), (Col% + 30)

'*** Get the number of Com Ports (valid entries are 1 to 6)
DO
	KeyPress$ = INKEY$
	IF KeyPress$ = "" THEN KeyPress$ = " "'Avoid error with ASC function
LOOP UNTIL (ASC(KeyPress$) >= 49) AND (ASC(KeyPress$) <= 54)

'*** Determine number of Com Ports
IF (ASC(KeyPress$) = 49) THEN
		NumOfComPorts% = 1
	ELSE
		NumOfComPorts% = 2
END IF

END SUB

'**** SUBPROGRAM TO FLUSH THE COM PORT BUFFER IF ABOVE A SPECIFIED SIZE ****
'
SUB FlushBuffer (CommsRef%, BufferLimit)

IF LOC(CommsRef%) >= BufferLimit THEN
		DO UNTIL LOC(CommsRef%) = 0
			Dummy$ = INPUT$(1, #CommsRef%)
		LOOP
END IF

END SUB

'* INTRODUCTORY MESSAGE EXPLAINING INSTRUCTIONS FOR PCs HAVING 2 COM PORTS *
'
SUB IntroMessage (NumOfComPorts%)

PRINT : COLOR 14, 1

'*** flush the keyboard buffer
DO: LOOP UNTIL INKEY$ = ""

Row% = 9: Col% = 12
LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%: PRINT " Ί                  OPERATING INSTRUCTIONS                Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                        Ί "
LOCATE CSRLIN, Col%: PRINT " Ί To Program:                                            Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                        Ί "
LOCATE CSRLIN, Col%: PRINT " Ί 1. Plug Type 7009-410 Programming Box into COM1        Ί "
LOCATE CSRLIN, Col%: PRINT " Ί 2. Connect Loop or Transducer to Programming Box       Ί "
LOCATE CSRLIN, Col%: PRINT " Ί 3. Switch Type 7009-410 Programming Box ON             Ί "
LOCATE CSRLIN, Col%: PRINT " Ί 4. Switch Type 7009-410 Programming Box to PROGRAM     Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                        Ί "
LOCATE CSRLIN, Col%: PRINT " Ί    Note: Available Addresses 1 to 13                   Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                        Ί "
LOCATE CSRLIN, Col%: PRINT " Ί Press any key to continue...                           Ί "
LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

DO: LOOP UNTIL INKEY$ <> ""

END SUB

SUB Logo

COLOR 0, 0
CLS
COLOR 14, 4        '3,4,7,11,12,13 are good, 10 not bad, original = 6

Row% = 2: Col% = 6
LOCATE Row%, Col%
PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%
PRINT " Ί         TYPE 7815-000-02 TRANSPONDER PROGRAMMING SOFTWARE          Ί "
LOCATE CSRLIN, Col%
PRINT " Ί                            Version 1.1                             Ί "
LOCATE CSRLIN, Col%
PRINT " Ί                                                                    Ί "
LOCATE CSRLIN, Col%
PRINT " Ί              Sonardyne International Ltd (c)  Oct 1997             Ί "
LOCATE CSRLIN, Col%
PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

'LOCATE , 6: PRINT "            Author John Kennaugh    Tel O1579-382583                  "

PRINT : COLOR 14, 1

END SUB

'******************** SUBPROGRAM TO INITIALISE THE ANT ********************
'
FUNCTION SetupANT% (ANTComms%, NumOfComPorts%)

'*** SetupANT% returns 0 if no errors
'*** SetupANT% returns a non-zero value if errors are encountered

'*** Common Shared Variable used in Error procedures
ComPort% = 2

'*** Initialise Com2 as the communications link to the ANT
'*** The cs100, ds, cd options mean that a RTS & CTS need to be connected
'*** at the PC end. cs200 means that the OPEN COM will fail in
'*** 10 x 200 milli-seconds if a suitable connector is not plugged into COM1.
ANTComms% = FREEFILE

'*** If only one Com Port, then use Com1, otherwise Com2
IF NumOfComPorts% = 1 THEN
		OPEN "com1:9600,s,7,2,cs200,ds,cd" FOR RANDOM AS #ANTComms%
	ELSE
		OPEN "com2:9600,s,7,2,cs200,ds,cd" FOR RANDOM AS #ANTComms%
END IF

'*** Reset the ANT to Power-Up Mode (discovered <CR> gets out of DDA mode).
'*** The ANT returns 125 or 166 bytes in reply to an <ESC> character.
'*** This fact is used to determine whether a the ANT is switched on.
'*** To check for a physical link, check for an error after <CR> is sent.
'*** First of all, flush the input buffer.
DO UNTIL LOC(ANTComms%) = 0
	Dummy$ = INPUT$(1, #ANTComms%)
LOOP

PRINT #ANTComms%, CHR$(13);       'ASC 13 = <CR>
SLEEP (2)

'*** If a physical link is not established between COM2 & ANT (but most
'*** likely COM2 is disconnected, Error Message 24 (Device Timeout)
'*** will have occured, setting the Common Shared variable, ErrorFlag%.
'*** In this case, flush the input buffer and quit gracefully.
IF ErrorFlag% = 1 THEN
		DO UNTIL LOC(ANTComms%) = 0
			Dummy$ = INPUT$(1, #ANTComms%)
		LOOP
		ErrorFlag% = 0
		SetupANT% = 1
		EXIT FUNCTION
END IF

PRINT #ANTComms%, CHR$(27);       'ASC 27 = <ESC>
SLEEP (2)

'*** Assuming the OPEN COM was successful, if nothing is returned from ANT,
'*** then assume a communications fault (the ANT is probably switched off).
IF LOC(ANTComms%) = 0 THEN    'If nothing in input buffer...
		ERROR 77                        'Simulate user-defined error number 77
		ErrorFlag% = 0                  'Common shared variable
		SetupANT% = 1
		EXIT FUNCTION
END IF

SetupANT% = 0

END FUNCTION

'************** SUBPROGRAM TO INITIALISE THE VLCT PROGRAMMING BOX **********
'
FUNCTION SetupFSK% (FSKComms%)

'*** SetupFSK% returns 0 if no errors
'*** SetupFSK% returns a non-zero value if errors are encountered

'*** Common Shared Variable used in Error procedures
ComPort% = 1

'*** Initialise Com1 as the communications link to the VLCT Programming Box.
'*** op1000 means that the OPEN command will wait 1000ms to be successful.
FSKComms% = FREEFILE
OPEN "com1:150,n,8,1,op1000" FOR RANDOM AS #FSKComms%

'*** If a physical link is not established between COM1 & Box (but most
'*** likely COM1 is disconnected, Error Message 24 (Device Timeout)
'*** will have occured, setting the Common Shared variable, ErrorFlag%.
'*** In this case, flush the input buffer and quit gracefully.
IF ErrorFlag% = 1 THEN
		ErrorFlag% = 0
		SetupFSK% = 1
		EXIT FUNCTION
END IF

SetupFSK% = 0

END FUNCTION

'**** SUBPROGRAM TO SETUP THE ANT FOR AUTOMATIC TAT MEASUREMENTS (V2.19) ****
'******************** (TAKES 5 SECONDS TO SET UP!) **************************
'
SUB SlowTATSetup (ANTComms%, VLCTAddress%, TZnum%)

DelayTime! = .1

PRINT #ANTComms%, "3";                                          'MULTI
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "3";                                          'OLD PAN
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "2";                                          'INTERROGATE
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "2";                                          'SET
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "3";                                          'RX REPLY
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "7";                                         'HF
CALL DelayPeriod(DelayTime!)
'CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "2";                                          'CHAN
CALL DelayPeriod(DelayTime!)


'*** The next section of code determines what state the ANT is in
'*** regarding HF or EHF.
'*** The problem is as follows:
'***   When the ANT is physically switched on, the band default is EHF.
'***   Unfortunately, neither <ESC> or <CR> or both (used for initialisation)
'***   causes the ANT to return to EHF mode - if it was in HF mode it will
'***   still be in HF mode after <CR> or <ESC>.
'***   The HF command "7", toggles between HF and EHF, but the point is
'***   the code cannot read the ANT to find out what band it is in,
'***   or reset it to EHF.

'*** TESTING - Determine number of characters in Com Port buffer
NumOfChars% = LOC(ANTComms%)

'*** Examine the characters in the Com Port Buffer and check the last 7 or 8.
'*** "TZ TPDR" indicates HF mode, and proceedings may continue unaffected;
'*** "EHF TPDR" indicates EHF mode, and extra steps must be made.
ANTReply$ = ""
x% = 1
DO UNTIL LOC(ANTComms%) = 0
	ANTChar$ = INPUT$(1, #ANTComms%)
	'PRINT "("; x%; ")"; ANTChar$;
	ANTReply$ = ANTReply$ + ANTChar$
	x% = x% + 1
LOOP

'*** TESTING
'PRINT
'PRINT "Number of characters in ANT Com Port buffer ="; NumOfChars%
'PRINT "Number of characters in ANTReply$ ="; LEN(ANTReply$)
'IF LEFT$(RIGHT$(ANTReply$, 10), 8) = "EHF TPDR" THEN PRINT "EHF" ELSE PRINT "HF"
'PRINT "Any key to continue..."
'DO: LOOP UNTIL INKEY$ <> ""


'*** Look at the last 10 characters and check for "EHF TPDR"
IF LEFT$(RIGHT$(ANTReply$, 10), 8) = "EHF TPDR" THEN

		PRINT #ANTComms%, "1";                                  'DUMMY CHANNEL
		CALL DelayPeriod(DelayTime!)
		CALL FlushBuffer(ANTComms%, 300)

		PRINT #ANTComms%, CHR$(13);                             '<ENTER>
		CALL DelayPeriod(DelayTime!)
		CALL FlushBuffer(ANTComms%, 300)

		PRINT #ANTComms%, "3";                                  'RX REPLY
		CALL DelayPeriod(DelayTime!)
		CALL FlushBuffer(ANTComms%, 300)

		PRINT #ANTComms%, "7";                                  'HF
		CALL DelayPeriod(DelayTime!)
		CALL FlushBuffer(ANTComms%, 300)

		PRINT #ANTComms%, "2";                                   'CHAN
		CALL DelayPeriod(DelayTime!)
		CALL FlushBuffer(ANTComms%, 300)
END IF

PRINT #ANTComms%, STR$(TZnum%);                                 'TZ NUMBER
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, CHR$(13);                                     '<ENTER>
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "1";                                          'EXIT
CALL DelayPeriod(DelayTime!)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, "5";                                          'START
CALL DelayPeriod(1)         'For safety, this MUST be 1 second
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, STR$(VLCTAddress%);                           'ADDRESS
CALL DelayPeriod(DelayTime)
CALL FlushBuffer(ANTComms%, 300)

PRINT #ANTComms%, CHR$(13);                                     '<ENTER>
CALL DelayPeriod(3)         'This MUST be 3 seconds
							'when an invalid message is returned

END SUB

'***************** SUBPROGRAM TO REQUEST OPERATOR TO WAIT **************
'
SUB WaitNotice

COLOR 30, 1
Row% = 17: Col% = 10

LOCATE Row%, Col%: PRINT " ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ» "
LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                        PLEASE WAIT                        Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
LOCATE CSRLIN, Col%: PRINT " Ί                                                           Ί "
LOCATE CSRLIN, Col%: PRINT " ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ "

COLOR 14, 1

END SUB

