You are here: RailNet Page 1 Home > Computer-Controlled DCC S-Gauge > Text Version 1 (simple program)

Version$ = "RNLOC1.BAS, 05-18-01"         '(extracted from "RNLOC21m.BAS")
'============================================================================
'Initialize
        CLS                 'clear the screen
        OPEN "COM1:9600,N,8,1,BIN,RS,DS0,CD0,CS0" FOR RANDOM AS #2
        GOSUB Menu                    'Display commands & titles
        DIR = 96  ' Set direction forward
        Key$ = " "                  'Initialize to 'space'; get loco number
'============================================================================
'============================================================================
Exec:                            'Executive Program
       DO UNTIL Key$ = "q"                  'q to exit
          LOCATE 2, 54: PRINT "{ Key$=|" + Key$ + "|}.."
          IF Key$ = " " THEN
             GOSUB GetLocomotiveNumber              'Space bar=get loco number
          ELSE
             LOCATE 4, 1: PRINT "04 Manual Run Mode - Speed Step = "; spd; SPC(25); "-"
             IF LEN(Key$) = 2 THEN            '2 chars=arrow pressed
                PRINT ".."; SPC(60); ".."     'clear old msgs from auto loops
                A = ASC(RIGHT$(Key$, 1))         'isolate right character
                IF A = 77 THEN DIR = 96       ' right arrow = run forward
                IF A = 75 THEN DIR = 80       ' left  arrow = run reverse
                IF A = 72 THEN spd = spd + 1  ' up arrow = increase speed
                IF A = 80 THEN spd = spd - 1  'down arrow = decrease speed
                GOSUB NCE                     'send command to NCE
             END IF
          END IF
          '
          Key$ = INKEY$            'Read keybd; <- = ^K, -> = ^M, ^ = ^H, v= ^P
                          ' need above to reset Key$ after return from auto. sub
       LOOP
         
Exit1:  LOCATE 12, 25: PRINT "## Exiting program (Key$=|" + Key$ + "|)"
        END
         
'============================================================================
'============================================================================
'============================================================================
GetLocomotiveNumber: 'Outputs: B1 & B2, Bytes 1 & 2 convrtd fr hex to decimal
        LOCATE 2, 1: PRINT "02 {Enter Loco Cab Number....."; : INPUT LN$
        IF LN$ = "u" THEN LN$ = "1862" 'union pacific #1862
        IF LN$ = "e" THEN LN$ = "0452" 'erie lackawanna #0452
        IF LN$ = "d" THEN LN$ = "2316" 'd&h u23 #2316   
        IF LN$ = "h" THEN LN$ = "3333" 'd&h c628 #3333  
        IF LN$ = "q" THEN GOTO Exit1:
        IF LEN(LN$) > 4 THEN LN = 0: GOTO GetLocomotiveNumber
        LN = VAL(LN$)
        LNHEX$ = HEX$(LN)
LEAD0:  IF LEN(LNHEX$) < 4 THEN LNHEX$ = "0" + LNHEX$: GOTO LEAD0
        BYTE1$ = LEFT$(LNHEX$, 2) ' separate 2 hex bytes.
        BYTE2$ = RIGHT$(LNHEX$, 2)
        NIBL1$ = LEFT$(BYTE1$, 1): NIBL2$ = RIGHT$(BYTE1$, 1)
          ' PRINT LNHEX$; " "; BYTE1$; " "; BYTE2$; " "; NIBL1$; " "; NIBL2$
        'Pad loco number with 'C000'
        IF NIBL1$ = "0" THEN NIBL1$ = "C": GOTO SPLICE
        IF NIBL1$ = "1" THEN NIBL1$ = "D": GOTO SPLICE
        IF NIBL1$ = "2" THEN NIBL1$ = "E"
       
SPLICE: BYTE1$ = NIBL1$ + NIBL2$ ' put byte back together
        LOCATE 20
        PRINT "20 {lnhex$=" + LNHEX$ + " byte1$/byte2$=" + BYTE1$ + " " + BYTE2$' temporary check on bytes.
        GOSUB CONVRT               'cnvrt byte 1 & 2 from hex to dec for xor
        LOCATE 2: PRINT "02 {............Cab # = '" + LN$ + "'.....}"
RETURN
         
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
CONVRT: 'Convert bytes 1 & 2 (loco number) from hex to decimal for xor
        BYTE$ = BYTE1$: GOSUB HexDec
        B1 = SUM
        BYTE$ = BYTE2$: GOSUB HexDec
        B2 = SUM
        GOSUB NCE             'send command packet to NCE to print direction
RETURN
         
'============================================================================
NCE:   'send new command packet to NCE command station
        IF spd < 0 THEN spd = 0      'Limits Spd range = 0 to 14
        IF spd > 14 THEN spd = 14
             
        IF spd = 0 THEN              'Limits Ctr range = 0, 2 to 15
           Ctr = 0
        ELSE
           Ctr = spd + 1             'Adds 1 to adjust around emer.stop
        END IF                       '(emer. stop =1; need to avoid this)
       
'       = = = = = = = = = = = = = = = = = = =
'       This section sends the command to the command station
        SPDV = DIR + Ctr
        B3 = SPDV: BYTE3$ = HEX$(B3)
        CK = B1 XOR B2 XOR B3
        BYTE4$ = HEX$(CK)
        XMIT$ = "Q " + BYTE1$ + " " + BYTE2$ + " " + BYTE3$ + " " + BYTE4$
        PRINT #2, XMIT$:
         
'       = = = = = = = = = = = = = = = = = = =
         
        LOCATE 3: PRINT " (clean line)                                                   -"
        LOCATE 3: PRINT "03 NCE: Direction= ": LOCATE 3, 20
        IF DIR = 96 THEN PRINT "Forward --> , Speed Step=" + STR$(spd) + ", Ctr=" + STR$(Ctr)
        IF DIR = 80 THEN PRINT "<--Reverse  , Speed Step=" + STR$(spd) + ", Ctr=" + STR$(Ctr)
       
        LOCATE 21, 1
        PRINT "21 {.B1...B2..B3..CK..|.BYTE1.BYTE2.BYTE3.BYTE4.}"
        LOCATE 22, 1
        PRINT "22 {"; B1; B2; B3; CK; "| " + BYTE1$ + "...." + BYTE2$ + "...." + BYTE3$ + "...." + BYTE4$ + "....}"
        LOCATE 23, 1
        PRINT "23 { Xmit String='" + XMIT$ + "' }"
RETURN
         
'============================================================================
HexDec: ' this subroutine converts hex bytes to decimal.
        ' data to be converted enters as BYTE$, is parsed
        ' to left and right characters, converted to hex
        ' by "brute force" lookup, then combined as decimal.
        ' data is returned as SUM.
                NIBL$ = LEFT$(BYTE$, 1) ' (NIB$ IS VALUE TO CONVERT.)
                NIBR$ = RIGHT$(BYTE$, 1)
        ' let's decimalize the left nibble first.
                IF NIBL$ = "A" THEN DECL = 10: GOTO LNIB
                IF NIBL$ = "B" THEN DECL = 11: GOTO LNIB
                IF NIBL$ = "C" THEN DECL = 12: GOTO LNIB
                IF NIBL$ = "D" THEN DECL = 13: GOTO LNIB
                IF NIBL$ = "E" THEN DECL = 14: GOTO LNIB
                IF NIBL$ = "F" THEN DECL = 15: GOTO LNIB
                DECL = VAL(NIBL$) ' this does the digits 0-9
LNIB:           DECL = DECL * 16 ' adjust for weight of left hex char.
        ' now decimalize the right nibble
                IF NIBR$ = "A" THEN DECR = 10: GOTO RNIB
                IF NIBR$ = "B" THEN DECR = 11: GOTO RNIB
                IF NIBR$ = "C" THEN DECR = 12: GOTO RNIB
                IF NIBR$ = "D" THEN DECR = 13: GOTO RNIB
                IF NIBR$ = "E" THEN DECR = 14: GOTO RNIB
                IF NIBR$ = "F" THEN DECR = 15: GOTO RNIB
                DECR = VAL(NIBR$) ' this does the digits 0-9
RNIB:   ' now we will put the nibbles back together as a decimal value                            
                SUM = DECL + DECR
        RETURN
       
         
'============================================================================
Menu:
        LOCATE 1, 3: PRINT "NCE MANUAL TRAIN CONTROL PROGRAM (technical details by Fred Cupp)"
''''''''02 Get Loco Num & Direction
''''''''03: NCE info: direction, speed step, actual counter 'ctr'
''''''''04: Subroutine info: Manual / AutoCircle / AutoPntToPnt
''''''''05: not used
''''''''11: not used                 
        LOCATE 12
        PRINT "12 |--------------------------------------------------------|"
        PRINT "   |                  CHOICES:                              |"
        PRINT "   | Space Bar to enter new loco number                     |"
        PRINT "   | Arrows: Up/Down = Speed Up/Down, -> & <- = forward/rev |"
        PRINT "   | 'q' to quit program                                    |"
        PRINT "   |--------------------------------------------------------|"
        PRINT "                                                     "; Version$
''''''''20  Byte info in hex
''''''''21: Byte Column Labels
''''''''22  Byte values in Dec / Byte values in Hex
''''''''23  Xmit string to NCE
''''''' PRINT "22 --(NOTE: using Line 24 or lower, wipes out line 1 )-------"
''''''' PRINT "23 --(      if Line 1 has been written first         )----------"
RETURN
         


(1H15=[an error occurred while processing this directive])

This page created 8/15/01, and last modified 8/16/01

[an error occurred while processing this directive]