[Edu-sig] Converting from Qbasic

Jeffrey Elkner jeff@elkner.net
03 May 2002 17:04:39 -0400


Hi all!

My students are working on end of the year projects, and one of our best
students, Nick Wheeler, is interested in converting a qbasic program
that controls an electric motor through the parallel port into Python.

The QBasic program is included below, but the main issues seem to me to
be:

1. What are the Python equivalents of PEEK, POKE, and OUT? (how do you
do something like:  

pdata% = PEEK(&H408) + PEEK(&H409) * 256

2.  Alternatively, is there a more pythonic way to do such things.

Students would love to be able to write program to control motors and
things.  We also have a science teacher here who wrote a similar program
in qbasic to measure the activity of mice.  If I could find out how to
do this it would go a long way toward getting Python to be more
generally used at the school.

Thanks!

jeff elkner
yorktown high school
arlington, va


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

REM SINGLE.BAS - Single-Coil Excitation

REM Copyright (c) 1997, Ian Harries and Imperial College, London, UK

REM MS-QBasic v1.1 for MS-DOS

REM IBM-PC Parallel Printer Port Data Register
REM ==========================================
REM        7   6   5   4   3   2   1   0   I/O Port
REM      +---+---+---+---+---+---+---+---+ ========
REM Data |   |   |   |   | C4| C3| C2| C1| Base = 278/378/3BC Hex
REM      +---+---+---+---+---+---+---+---+

REM declare globals

COMMON SHARED pdata%:  ' OUTPUT Port

REM declare subroutines

DECLARE SUB delay ()
DECLARE SUB getLPT ()
DECLARE SUB KeyWait ()
DECLARE SUB rotate ()

REM main program

    CLS
    PRINT
    PRINT "Ian's Parallel Port Stepper Controller"
    PRINT "======================================"
    PRINT

    getLPT

    IF pdata% <> 0 THEN rotate
END


SUB delay
    FOR i% = 1 TO 175 ' dilute to taste
        REM do nothing
    NEXT i%
END SUB

SUB getLPT
    DEF SEG = 0

    pdata% = PEEK(&H408) + PEEK(&H409) * 256

    PRINT "LPT1";
    IF pdata% = 0 THEN PRINT " not found" ELSE PRINT " found at &H";
HEX$(pdata%)
    PRINT
END SUB

SUB KeyWait
    PRINT "<press any key to continue>"
    DO
    LOOP UNTIL INKEY$ <> ""
END SUB

SUB rotate
    OUT pdata%, &H0

    PRINT "All OFF to start with "; : KeyWait
counter = 1
DO WHILE counter <> 0
PRINT ""
         INPUT "Enter turn number: "; counter
         IF counter > 0 THEN

    PRINT "Clockwise";

    FOR i% = 1 TO counter
        OUT pdata%, &H8: delay
        OUT pdata%, &H4: delay
        OUT pdata%, &H2: delay
        OUT pdata%, &H1: delay
        PRINT ".";
    NEXT i%
    ELSE


    PRINT "Anticlockwise";

counter=0-counter

    FOR i% = 1 TO counter
        OUT pdata%, &H1: delay
        OUT pdata%, &H2: delay
        OUT pdata%, &H4: delay
        OUT pdata%, &H8: delay
        PRINT ".";
    NEXT i%
    END IF
LOOP

    OUT pdata%, &H0


    PRINT "All OFF again "; : KeyWait
END SUB