home *** CD-ROM | disk | FTP | other *** search
- ' == [ GENERAL NOTES ] ================================================
- ' =
- ' This Basic program demonstrates how to interface to the =
- ' AsyncTermio device driver. The interface used finds it's =
- ' origin on AT&T's UNIX systems. =
- ' =
- ' The generalized UNIX serial link interface was originally moved =
- ' to MS-DOS for C programmers that desire to port serial comm =
- ' software between MS-DOS and UNIX. =
- ' =
- ' Most Basic programmers will tell you that Basic's COM port =
- ' management is sufficient for most applications. We agree. =
- ' However, should you desire to use the generalized UNIX serial =
- ' port interface, Async TermIO will suffice. =
- ' =
- ' With the OPEN COM statement, Basic provides: =
- ' =
- ' + Speed, Parity, Stopbits, Character Length Control. =
- ' + Asynchronous buffering of input and output. =
- ' + Call back functions that are invoked on COM activity. =
- ' + Exception handling. =
- ' + Timing. =
- ' =
- ' With the OPEN COM statement, Basic DOES NOT provide: =
- ' =
- ' + Xon/Xoff Flow Control. =
- ' + The interface is not generalized across environments. =
- ' and is not portable to other operating systems. =
- ' + Not as diverse as the generalized UNIX interface. =
- ' =
- ' The trade-offs of writing serial communications software are =
- ' discussed at length in the Async TermIO documentation. =
- ' =
- ' Please review the publication (See Async TermIO User's Manual) =
- ' "Portable Serial Communications Between MS-DOS & UNIX" =
- ' =
- ' =======================================================================
- ' =
- ' Copyright (c) 1988 Boulder Software Group =
- ' P.O. BOX 14200 =
- ' Boulder, CO 80308-4200 =
- ' =
- ' (303) 440-7868 (Voice) =
- ' (303) 447-0711 (FAX) =
- ' =
- ' =======================================================================
- '
-
- '
- ' == [ INCLUDE FILES ] ================================================
- ' =
- ' The include file TERMIO.H contains several constants that are =
- ' use to set flags, compare values, function declarations, etc. =
- ' =
- ' =======================================================================
- '
-
- ' $INCLUDE: 'TERMIO.H'
-
-
- '
- ' == [ GLOBAL VARS ] ================================================
- ' =
- ' =
- ' =======================================================================
- '
-
- DIM TtyParams AS Termio
- DIM FileNumberTty AS INTEGER
- DIM FileHandle AS INTEGER
- DIM FileMode AS INTEGER
-
-
- '
- ' == [ MAIN ] ================================================
- ' =
- ' Main driver program used to demonstrate how to use the =
- ' AsyncTermIO device driver. Each meaningful code fragment =
- ' is commented. =
- ' =
- ' =======================================================================
- '
- '
- ' Set up primitive error trapping (exception handling)
- ' in case something goes "wrong."
- '
-
- ON ERROR GO TO ErrorHandler
-
- '
- ' Obtain a free file number for the I/O stream we wish
- ' to open for the terminal device "TTY01" which is a
- ' device driver you should have installed in your
- ' CONFIG.SYS file.
- '
-
- FileNumberTty = FREEFILE
-
- '
- ' There are several ways to open a file in Basic, the
- ' method shown below is recommended when you intend
- ' to use the AsyncTermIO driver.
- '
-
- OPEN "TTY01" FOR BINARY ACCESS READ WRITE AS #FileNumberTty
-
- '
- ' Set set up the flags in the struct termio
- '
- ' Request that the BREAK character be ignored.
- ' Ignore parity errors .
- ' Start the XON/XOFF mechanism.
- '
- '
-
- TtyParams.Ciflag = BRKINT OR IGNPAR OR IXON OR IXOFF
-
- '
- ' Request no delays after NL, CR, TAB or VT. Expand tabs
- ' to spaces and delay on sending a FF character.
- '
-
- TtyParams.Coflag = NL0 OR CR0 OR TAB3 OR BS1 OR VT0 OR FF1
-
- '
- ' Request that the baud (symbols/sec) rate at 9600, eight
- ' bits of data, one stop bit (default), enable the
- ' the receiver, and hang-up on the last close of the file.
- ' No parity was requested (PARENB flag not present).
- '
-
- TtyParams.Ccflag = B9600 OR CS8 OR CREAD OR HUPCL
-
- '
- ' Request no Clflag options.
- '
-
- TtyParams.Clflag = 0
-
- '
- ' Line discipline is always zero for this device driver
- '
-
- TtyParams.Cline = CHR$(0)
-
-
- '
- ' Since ICANON is not set the MIN/TIME parameters are
- ' used. So set both their values to zero. If the ICANON
- ' flag is set, some of these entries have different meanings.
- ' See the Async TermIO documentation on "MIN/TIME Interaction"
- ' (Somewhere around page 12 for Doc version 4.02, see also
- ' Pages 2 and 3).
- '
-
- MID$( TtyParams.ccc, VINTR, 1 ) = "A"
- MID$( TtyParams.ccc, VQUIT, 1 ) = "B"
- MID$( TtyParams.ccc, VERASE, 1 ) = CHR$( 00 )
- MID$( TtyParams.ccc, VKILL, 1 ) = CHR$( 00 )
- MID$( TtyParams.ccc, VEOF, 1 ) = CHR$( 00 )
- MID$( TtyParams.ccc, VEOL, 1 ) = CHR$( 00 )
- MID$( TtyParams.ccc, RESERVED, 1 ) = CHR$( 00 )
- MID$( TtyParams.ccc, SWTCH, 1 ) = CHR$( 00 )
-
- '
- ' Obtain the file's handle and attribute (mode)
- '
-
- FileMode = FILEATTR( FileNumberTty, 1 )
- FileHandle = FILEATTR( FileNumberTty, 2 )
-
- '
- ' Attempt to set the new parameters for the device
- '
- ' Uncomment the next line of code if you desire to
- ' see what is in the record TtyParams thus far
- '
- '
- ' CALL PrintTermioRecord( TtyParams )
- '
- '
- ' Normally the function TtyDeviceIoctl() would be named
- ' Ioctl() (as it is in C), however, Basic reserves IOCTL
- ' as a keyword and thus we can't use it as an identifier.
- '
- ' The name TtyDeviceIoctl() was arbitrarily chosen, the
- ' source code is provided in the file IOCTL.BAS, so feel free
- ' to change it to what ever you want. The make file MAKEDEMO
- ' will convert IOCTL.BAS to a library that can be linked to
- ' your application under the Microsoft Basic 6.0 compiler;
- ' (IOCTL.LIB).
- '
- ' Note that TtyDeviceIoctl() will be described as the
- ' function ioctl() in all accompanying Async TermIO documentation.
- '
-
- IF TtyDeviceIoctl( FileHandle, TCSETS, TtyParams ) <> PASS THEN
-
- PRINT "TtyDeviceIoctl() can't set new parameters"
- RESET
- SYSTEM
- END IF
-
- '
- ' Call a function to echo characters from the device
- ' attached to the serial link tty01 (COM1).
- '
-
- CALL Tty2Con2Tty( FileNumberTty )
-
- '
- ' Close the device that we opened
- '
-
- CLOSE #FileNumberTty
-
- '
- ' Clear the screen and exit the program.
- '
-
- CLS
- PRINT "Exit from Basic Demo Program for AsyncTermIO"
-
- END
-
-
- '
- ' == [ ERRORHANDLER ] ================================================
- ' =
- ' This is a very lazy error handler, it does not try to recover =
- ' from any errors that may trap to it. All it does is print =
- ' out the exception handling information and kill this process. =
- ' =
- ' =======================================================================
- '
-
- ErrorHandler:
-
- CLS
- PRINT "There has been a runtime error deemed critical"
- PRINT "enough to terminate the program."
- PRINT ""
- PRINT "Errortype: ", ERDEV
- PRINT "On device: ", ERDEV$
- PRINT "Basic ERR: ", ERR
-
- SELECT CASE( ERR AND &h00FF )
- CASE 0
- PRINT "MS-DOS : Attempt to write on a write-protected diskette"
- CASE 1
- PRINT "MS-DOS : Unknown Unit"
- CASE 2
- PRINT "MS-DOS : Drive (or Device) Not Ready"
- CASE 3
- PRINT "MS-DOS : Unknown Command"
- CASE 4
- PRINT "MS-DOS : Data Error (CRC)"
- CASE 5
- PRINT "MS-DOS : Bad Request Structure Length"
- CASE 6
- PRINT "MS-DOS : Seek Error"
- CASE 7
- PRINT "MS-DOS : Unknown Media Type"
- CASE 8
- PRINT "MS-DOS : Sector Not Found"
- CASE 9
- PRINT "MS-DOS : Printer Out Of Paper"
- CASE 10
- PRINT "MS-DOS : Write Fault On Device"
- CASE 11
- PRINT "MS-DOS : Read Fault On Device"
- CASE 12
- PRINT "MS-DOS : General MS-DOS Device Failure"
-
- CASE ELSE
- PRINT "MS-DOS : Unrecognized MS-DOS Error"
- END SELECT
-
- '
- ' To bomb out with Basic Runtime error messages uncomment
- ' the next line of code.
- '
- '
- ' ON ERROR GOTO 0
- '
- '
-
- RESET
- SYSTEM
-
- '
- ' == [ DOSOMETHINGELSE ] ================================================
- ' =
- ' The user can place code that can be executed between arriving =
- ' characters. If a character arrives, it will be buffered and =
- ' readable later. =
- ' =
- ' =======================================================================
- '
- '
- '
- SUB DoSomeThingElse
-
- '
- ' Your code here to process other things while waiting for
- ' characters to arrive.
- '
- '
- '
-
- END SUB
-
-
- '
- ' == [ TTY2CON2TTY ] ================================================
- ' =
- ' Simple SUB that loops looking for characters from the device. =
- ' If it finds one it echos it to the console. Otherwise it =
- ' will process something else (DoSomeThingElse()). =
- ' =
- ' If a key is pressed on the console, it is echoed to the device =
- ' driver. I/O is somewhat clumsy, in that characters entered =
- ' at the console must each be followed by the <Enter> key to be =
- ' read by the INPUT statement. You can tailor I/O specifically =
- ' for your application as is desired. =
- ' =
- ' The SUBs GET and PUT can be used to send and receive blocks of =
- ' character data to and from the device driver. There are =
- ' several examples, coded in C, that can be ported to Basic, =
- ' that demonstrate these techniques. See the directories =
- ' \C\MSC\DEMO1, \C\MSC\DEMO2, and \C\MSC\DEMO3. =
- ' =
- ' =======================================================================
- '
- '
- '
- SUB Tty2Con2Tty( FileNumber AS INTEGER )
-
- DIM InChar AS STRING * 1
- DIM OutChar AS STRING * 1
- DIM TestChar AS STRING * 1
-
- '
- ' Loop looking for keys from the console. If a key has
- ' been pressed send it to the terminal device.
- '
- ' When the user enters "Z" on the serial link, this SUB
- ' will return.
- '
-
-
- WHILE( InChar <> "Z" )
-
- '
- ' Get a character from the device. If EOF is
- ' encountered, do something else in the meantime
- '
-
- GET #FileNumber, , InChar
-
- IF EOF( FileNumber ) THEN
-
- CALL DoSomeThingElse
-
- ELSE
- '
- ' Uncomment the next line if you desire to
- ' echo characters from the device back to the
- ' device
- '
- ' PUT #FileNumber,, InChar
- '
-
- WRITE InChar
-
- END IF
-
- '
- ' See if there is anything hanging around in the
- ' stdin stream (CON). If so, pick it up send it
- ' to the device
- '
-
- IF( KbHit = PASS ) THEN
-
- INPUT ;"", OutChar
- PUT #FileNumber,, OutChar
-
- END IF
-
- WEND
-
- END SUB
-
-
- '
- ' == [ KBHIT ] ==============================================
- ' =
- ' Can't find any function in Basic to see if a keyboard =
- ' character was hit, so gin up a quick one similar to =
- ' MS C's kbhit() function. Most likely, a similar Basic function =
- ' exists, but I can't find it. =
- ' =
- ' =======================================================================
- '
- FUNCTION KbHit
-
- DIM InRegs AS RegType
- DIM OutRegs AS RegType
-
- InRegs.AX = &H0B00
-
- CALL INTERRUPT( &H0021, InRegs, OutRegs )
-
- IF ( ( OutRegs.AX AND &H00FF ) <> 0 ) THEN
- KbHit = PASS
- ELSE
- KbHit = FAIL
- END IF
-
- END FUNCTION
-
-
- '
- ' == [ PRINTTERMIORECORD ] ==============================================
- ' =
- ' Debugging function that prints out the contents of the object =
- ' TtyParams. Uses the same format as the "stty -g < \dev\tty01" =
- ' command line at the MS-DOS prompt. =
- ' =
- ' =======================================================================
- '
- '
- '
- SUB PrintTermioRecord( TtyParams AS Termio )
-
- PRINT "Ciflag : ", OCT$( TtyParams.Ciflag )
- PRINT "Coflag : ", OCT$( TtyParams.Coflag )
- PRINT "Ccflag : ", OCT$( TtyParams.Ccflag )
- PRINT "Clflag : ", OCT$( TtyParams.Clflag )
-
- PRINT "Cline : ", OCT$( ASC( TtyParams.Cline ) )
-
- PRINT "Ccc( VINTR ) : ", OCT$( ASC( MID$( TtyParams.ccc, VINTR, 1 ) ) )
- PRINT "Ccc( VQUIT ) : ", OCT$( ASC( MID$( TtyParams.ccc, VQUIT, 1 ) ) )
- PRINT "Ccc( VERASE ) : ", OCT$( ASC( MID$( TtyParams.ccc, VERASE, 1 ) ) )
- PRINT "Ccc( VKILL ) : ", OCT$( ASC( MID$( TtyParams.ccc, VKILL, 1 ) ) )
- PRINT "Ccc( VEOF ) : ", OCT$( ASC( MID$( TtyParams.ccc, VEOF, 1 ) ) )
- PRINT "Ccc( VEOL ) : ", OCT$( ASC( MID$( TtyParams.ccc, VEOL, 1 ) ) )
- PRINT "Ccc( RESERVED ) : ", OCT$( ASC( MID$( TtyParams.ccc, RESERVED, 1 ) ) )
- PRINT "Ccc( SWTCH ) : ", OCT$( ASC( MID$( TtyParams.ccc, SWTCH, 1 ) ) )
-
- END SUB
-