'                               Image Rejection
'                               ---------------

'version 2
'4 May 95

COMMON sgcode$, atten$, sgen$, ch$, ctr$
CLOSE
sgen = VAL(sgcode$)
'sgen = 1
atten$ = "27"


DIM fr$(15)
DIM fr(15)
DIM imag(15)
DIM im$(15)

SCREEN 9
CLS
COLOR 14, 1

start:
' initial set up of gp-ib

 OPEN "ieeectrl" FOR RANDOM AS #1
 OPEN "ieeedata" FOR RANDOM AS #2
 IOCTL #1, "init" + CHR$(13)
 IOCTL #1, "clear" + CHR$(13)
 IOCTL #1, "to 10 end=13 eos=13" + CHR$(13)

 OPEN "com2:9600,n,8,2" FOR RANDOM AS #3
 OPEN "c:\qb\gpr\res\file" FOR APPEND AS #4

 PRINT #4, "Image Channel"
 SLEEP 5
 GOTO test

adret:
  IF sgen = 1 THEN RETURN
  IOCTL #1, "3" + CHR$(13)
  IOCTL #2, adret$ + CHR$(13)
  RETURN


mar:
  IF sgen = 2 THEN RETURN
  COLOR 11
  IOCTL #1, "3" + CHR$(13)
  IOCTL #2, mar$ + CHR$(13)
  ' PRINT mar$
RETURN

at:
  COLOR 13
  IOCTL #1, atten$ + CHR$(13)
  IOCTL #2, at$ + CHR$(13)
  ' PRINT at$
  SLEEP 1
RETURN

ip:
COLOR 14
   ip$ = IOCTL$(#2)
'   PRINT ip$
   ip# = VAL((MID$(ip$, s, e)))
   RETURN

freqip:
mar$ = "cfrq?": GOSUB mar
s = 12
e = 14
GOSUB ip
ip# = ip# / 1000000
'PRINT ip#; " MHz"
RETURN

levip:
mar$ = "rflv?": GOSUB mar
s = 22
e = 6
GOSUB ip
u$ = MID$(ip$, 12, 4)
'PRINT ip#; u$
RETURN

freq:
adfr = VAL(fr$)
adfr = adfr * 1000000
adret$ = "F " + STR$(adfr)
GOSUB adret

mar$ = "cfrq " + fr$ + " mhz"
GOSUB mar
RETURN

lev:
  mar$ = "rflv " + l$ + "dbm"
  GOSUB mar
  adret$ = "A " + l$
  GOSUB adret
RETURN

gpr:
  PRINT #3, gpr$
  INPUT #3, r$
  SLEEP 1
  er$ = LEFT$(r$, 1)

IF er$ <> "E" THEN RETURN
IF gpr$ = "REM" THEN RETURN

eror:
BEEP
COLOR 15
PRINT
PRINT "                 Error in GPR code   :    "; gpr$
BEEP
SLEEP
RETURN


test:
at$ = "A 100,": GOSUB at
gpr$ = "REM": GOSUB gpr
mar$ = "*RST": GOSUB mar
mar$ = "imode normal": GOSUB mar
mar$ = "mod:OFF": GOSUB mar
mar$ = "rflv:ON": GOSUB mar
adret$ = "FM 0": GOSUB adret
adret$ = "AM 0": GOSUB adret
SLEEP 2

gpr$ = "VN": GOSUB gpr

rec$ = LEFT$(r$, 4)

'************************************************************
IF VAL(LEFT$(r$, 5)) = 4305 THEN rec$ = MID$(STR$(4301), 2, 4)
'************************************************************

sn$ = RIGHT$(r$, 4)
fv$ = MID$(r$, 5, 5)
COLOR 15
PRINT "Receiver type         GPR"; rec$
PRINT "Serial no.               "; sn$



LOCATE 5, 20
COLOR 14
PRINT "Image Rejection"
COLOR 11
PRINT TAB(10); "Frequency"; TAB(24); "Image"; TAB(36); "Measured"

FOR flp = 1 TO 12
READ fr(flp), imag(flp)

NEXT flp

        DATA 27,633,75,681,95,697,130,734,170,772,220,824
        DATA 250,102,350,90,440,1042,712,110,781,179,999,399

'new / old type gpr
sn = VAL(sn$)
IF sn >= 3000 THEN
  new = 1

  imag(10) = 1316
  imag(11) = 1385
  imag(12) = 1605
ELSE
  new = 0
END IF

lp = 1

DO
  im$(lp) = STR$(imag(lp))
  fr$(lp) = STR$(fr(lp))
 
  ofs = 0
  sgofs = 0
  at$ = "A 100,": GOSUB at
  gpr$ = "BW 1": GOSUB gpr
  mar$ = "rflv:OFF": GOSUB mar

  res$ = ""
  rc43$ = LEFT$(rec$, 2)
  IF rc43$ = "43" THEN
    IF lp = 7 OR lp = 8 THEN
      rescol = 13
      res$ = "----"
      GOTO result
    END IF
  END IF

  fr$ = fr$(lp)
  fr# = VAL(fr$)
  fr# = fr# * 1000000
  ofs = 0
  ofset = 0

birdy:
  blp = 1
  bird(2) = 0
 
  DO
    f# = fr# + ofset
    f$(blp) = STR$(f#)
    gpr$ = "FR " + f$(blp)
    GOSUB gpr
    SLEEP 3
 
    IF blp = 1 THEN
      gpr$ = "CL": GOSUB gpr
      SLEEP 5
    END IF
   
    gpr$ = "SG": GOSUB gpr
    bird(blp) = VAL(r$)
    IF bird(1) < -22 THEN blp = 4
    blp = blp + 1
    ofset = ofset + 12500
  LOOP UNTIL blp >= 3
 
  IF bird(2) < bird(1) THEN
    bd$ = "*"
    f$ = f$(2)
    sgofs = .0125
  ELSE
    bd$ = " "
    f$ = f$(1)
    sgofs = 0
  END IF
 
  gpr$ = "FR " + f$: GOSUB gpr
 
level:
  sgfr = VAL(f$)
  sgfr = sgfr / 1000000
  fr$ = STR$(sgfr)
  GOSUB freq
 

  l$ = "-6": GOSUB lev
  mar$ = "rflv:ON": GOSUB mar
  SLEEP 2
  lev = -6

adjust:
  DO
    SLEEP 1
    gpr$ = "SG": GOSUB gpr
 
    adlev! = VAL(r$)
    inlev! = lev - adlev!
    IF inlev! <> 0 THEN
      l$ = STR$(inlev!)
      lev = inlev!
      GOSUB lev
     ' BEEP
    END IF
  LOOP UNTIL adlev! = 0

  freq = VAL(im$(lp))
  freq# = freq - sgofs
  fr$ = STR$(freq#)
  GOSUB freq

  at$ = "A 50,": GOSUB at
  SLEEP 2
  gpr$ = "SG": GOSUB gpr
  statt% = VAL(r$)
  at = (50 + statt%) + 4

atts:
  DO
    at = at - 1
      IF at = 0 THEN GOTO result
    att$ = STR$(at)
    at$ = "A " + att$ + ",": GOSUB at
   SLEEP 1
    gpr$ = "SG": GOSUB gpr
    r = VAL(r$)
  LOOP UNTIL r >= -.5
  rdp = r
  rescol = 10

result:

  LOCATE 7 + lp, 10
  COLOR 11
  PRINT fr$(lp); " MHz"
  LOCATE 7 + lp, 24
  PRINT im$(lp); " MHz"; "  "; bd$
  LOCATE 7 + lp, 36

 
IF res$ = "----" THEN
  bd$ = " "
  fail$ = " "
  COLOR rescol
  PRINT res$
ELSE
  res = (100 - at) - rdp
  IF res < 55 THEN
    fail$ = "!"
    COLOR 12
  ELSE
    fail$ = " "
    COLOR 10
  END IF
 
  PRINT USING "###.#"; res;
  PRINT " dB   "; fail$
  res$ = STR$(res)
END IF
 
  PRINT #4, fr$(lp)
  PRINT #4, im$(lp)
  PRINT #4, res$
  PRINT #4, bd$
'  PRINT #4, fail$
  lp = lp + 1
LOOP UNTIL lp = 13
PRINT #4, "Image end"

mar$ = "*RST": GOSUB mar
gpr$ = "LOC": GOSUB gpr
SLEEP 3

CLOSE
IF ctr$ = "1" THEN CHAIN "c:\qb\gpr\ifrej"
CHAIN "c:\qb\gpr\menu"

END





