home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv4_6 / unix / unixbsc.arc / IOCTL.BAS next >
Encoding:
BASIC Source File  |  1989-06-19  |  7.9 KB  |  276 lines

  1. ' == [ GENERAL NOTES   ] ================================================
  2. '                                                                       =
  3. '    This Basic program demonstrates how to interface to the     =
  4. '    AsyncTermio device driver.  The interface used finds it's    =
  5. '    origin on AT&T's UNIX systems.                     =
  6. '                                    =
  7. '    The generalized UNIX serial link interface was originally moved    =
  8. '    to MS-DOS for C programmers that desire to port serial comm    =
  9. '    software between MS-DOS and UNIX.                =
  10. '                                    =
  11. '    Most Basic programmers will tell you that Basic's COM port    =
  12. '    management is sufficient for most applications.  We agree.    =
  13. '    However, should you desire to use the generalized UNIX serial    =
  14. '    port interface, Async TermIO will suffice.            =
  15. '                                    =
  16. '    With the OPEN COM statement, Basic provides:            =
  17. '                                    =
  18. '        + Speed, Parity, Stopbits, Character Length Control.    =
  19. '        + Asynchronous buffering of input and output.        =
  20. '        + Call back functions that are invoked on COM activity.    =
  21. '        + Exception handling.                    =
  22. '        + Timing.                        =
  23. '                                    =
  24. '    With the OPEN COM statement, Basic DOES NOT provide:        =
  25. '                                    =
  26. '        + Xon/Xoff Flow Control.                =
  27. '        + The interface is not generalized across environments.    =
  28. '            and is not portable to other operating systems.    =
  29. '        + Not as diverse as the generalized UNIX interface.    =
  30. '                                    =
  31. '    The trade-offs of writing serial communications software are     =
  32. '    discussed at length in the Async TermIO documentation.        =
  33. '                                    =
  34. '    Please review the publication (See Async TermIO    User's Manual)    =
  35. '        "Portable Serial Communications Between MS-DOS & UNIX"    =
  36. '                                    =
  37. ' =======================================================================
  38. '                                    =
  39. '    Copyright (c) 1988    Boulder Software Group            =
  40. '                P.O. BOX 14200                =
  41. '                Boulder, CO   80308-4200        =
  42. '                                    =
  43. '                (303) 440-7868 (Voice)            =
  44. '                (303) 449-8612 (FAX)            =
  45. '                                                                       =
  46. ' =======================================================================
  47. '
  48.  
  49. '
  50. ' == [ INCLUDE FILES   ] ================================================
  51. '                                                                       =
  52. '    The include file TERMIO.H contains several constants that are    =
  53. '    use to set flags, compare values, function declarations, etc.    =
  54. '                                                                       =
  55. ' =======================================================================
  56. '
  57.     
  58.     ' $INCLUDE: 'TERMIO.H'
  59.  
  60.  
  61. '
  62. ' == [ TTYDEVICEIOCTL  ] ================================================
  63. '                                                                       =
  64. '    TtyDeviceIoctl is responsible for taking the INTEGER        =
  65. '    FileHandle, the Command (what to do), and a Basic record    =
  66. '    of type TtyParam.  Depending on the Command (see printed doc)    =
  67. '    the device attributes set in the TtyParam record will be issued    =
  68. '    to the device associated with the FileHandle.            =
  69. '                                    =
  70. '    It is somewhat more awkward to implement this function in    =
  71. '    Basic than in C, however, it is possible.            =
  72. '                                    =
  73. ' =======================================================================
  74. '                                    =
  75. '    NOTE:                                =
  76. '                                    =
  77. '    Normally the function TtyDeviceIoctl() would be named        =
  78. '    Ioctl() (as it is in C), however, Basic reserves IOCTL        =
  79. '    as a keyword and thus we can't use it as an identifier.        =
  80. '                                    =
  81. '    The name TtyDeviceIoctl() was arbitrarily chosen, the        =
  82. '    source code is provided below, so feel free to change it    =
  83. '    to what ever you want.  Note that TtyDeviceIoctl() will be    =
  84. '    described as the function ioctl() in all accompanying         =
  85. '    documentation.                            =
  86. '                                                                       =
  87. ' =======================================================================
  88. '
  89. '
  90. '
  91. FUNCTION TtyDeviceIoctl( FileHandle AS INTEGER, Command AS INTEGER, TtyParams AS Termio ) STATIC
  92.  
  93.     DIM        TtyDevice        AS    MsdosTermio
  94.     DIM        InRegs            AS    RegType
  95.     DIM        OutRegs            AS    RegType
  96.     
  97.  
  98.     '
  99.     '    Make a copy of the record (struct) for the device call
  100.     '
  101.  
  102.     TtyDevice.TtyParams    = TtyParams
  103.  
  104.     '
  105.     '    Set the mode of the device identified by FileHandle to
  106.     '    true binary.  The BIN bit must be set in the device attribute
  107.     '    word inside the device driver.
  108.     '
  109.  
  110.     IF( SetBinaryMode( FileHandle ) <> PASS ) THEN
  111.         
  112.         TtyDeviceIoctl    = FAIL
  113.         EXIT FUNCTION
  114.  
  115.     END    IF
  116.  
  117.  
  118.     '
  119.     '    If the Command is TCSBRK, TCXONC, or TCFLSH then
  120.     '    the argument should be passed as an integer.
  121.     '    If this is the case, read the current value of the
  122.     '    termio record (struct) contained internally to the 
  123.     '    device driver
  124.     '    
  125.  
  126.     SELECT CASE ( Command )
  127.         CASE    TCSBRK, TCXONC, TCFLSH
  128.     
  129.             '
  130.             '    Read the current struct
  131.             '
  132.  
  133.             TtyDevice.FileHandle    = FileHandle
  134.             TtyDevice.Command    = Command
  135.             TtyDevice.Arguement    = 0
  136.  
  137.             InRegs.AX        = &H4400 OR ( ReadCXBytes AND &H00FF )
  138.             InRegs.BX        = FileHandle
  139.             InRegs.CX        = LEN( TtyDevice )
  140.             InRegs.DX        = VARPTR( TtyDevice )
  141.             InRegs.DS        = VARSEG( TtyDevice )
  142.  
  143.             CALL INTERRUPT( &H0021, InRegs, OutRegs )
  144.  
  145.             IF ( ( OutRegs.Flags AND &H0001 ) <> 0 ) THEN
  146.                 TtyDeviceIoctl    = FAIL
  147.                 EXIT FUNCTION
  148.             END IF
  149.  
  150.         CASE    TCGETS, TCSETS, TCSETAW, TCSETAF
  151.             
  152.         CASE    ELSE
  153.             TtyDeviceIoctl    = FAIL
  154.             EXIT FUNCTION
  155.     END    SELECT
  156.  
  157.     '
  158.     '    Examine the Command and determine whether to read or
  159.     '    write bytes to using the rea MS-DOS IOCTL function call
  160.     '
  161.  
  162.     SELECT CASE( Command )
  163.         CASE    TCGETS
  164.             FunctionValue        = ReadCXBytes
  165.  
  166.         CASE    TCSETS, TCSETAW, TCSETAF
  167.             FunctionValue        = WriteCXBytes
  168.             TtyDevice.Arguement    = 0
  169.  
  170.         '    
  171.         '    Special case: When Command is TCSETBRK, TCXONC, or 
  172.         '    TCFLSH then the Argument (TtyParams) contains an
  173.         '    integer ( 0, 1, or 2 per the command) than can be
  174.         '    found by referencing the first member of the structure
  175.         '    (Ciflag) that Basic "thought" was passed.
  176.         '
  177.  
  178.         CASE    TCSETBRK, TCXONC, TCFLSH
  179.             FunctionValue        = WriteCXBytes
  180.             TtyDevice.Arguement    = TtyParams.Ciflag
  181.  
  182.         CASE    ELSE
  183.             TtyDeviceIoctl        = FAIL
  184.             EXIT FUNCTION
  185.     END    SELECT
  186.  
  187.     '
  188.     '    Perform the operation on the structure via MS-DOS
  189.     '
  190.  
  191.     TtyDevice.FileHandle    = FileHandle
  192.     TtyDevice.Command    = Command
  193.  
  194.     InRegs.AX        = &H4400 OR ( FunctionValue AND &H00FF )
  195.     InRegs.BX        = FileHandle
  196.     InRegs.CX        = LEN( TtyDevice )
  197.     InRegs.DX        = VARPTR( TtyDevice )
  198.     InRegs.DS        = VARSEG( TtyDevice )
  199.  
  200.     CALL INTERRUPT( &H0021, InRegs, OutRegs )
  201.  
  202.     '
  203.     '    Copy the information from the device driver to the
  204.     '    callers record.
  205.     '
  206.  
  207.     TtyParams    = TtyDevice.TtyParams
  208.  
  209.     '
  210.     '    See if we failed the function call
  211.     '
  212.  
  213.     IF ( ( OutRegs.Flags AND &H0001 ) <> 0 ) THEN
  214.         TtyDeviceIoctl    = FAIL
  215.         EXIT FUNCTION
  216.     END IF
  217.  
  218.  
  219.     TtyDeviceIoctl    = PASS
  220.  
  221. END    FUNCTION
  222.  
  223.  
  224. '
  225. ' == [ SETBINARYMODE   ] ================================================
  226. '                                                                       =
  227. '    This function is used by TtyDeviceIoctl() to set binary mode    =
  228. '    in the device's attribute block.  The user should not have to    =
  229. '    call this function.                        =
  230. '                                                                       =
  231. ' =======================================================================
  232. '
  233. '
  234. '
  235. FUNCTION SetBinaryMode( FileHandle AS INTEGER )
  236.  
  237.     DIM        InRegs            AS    RegType
  238.     DIM        OutRegs            AS    RegType
  239.  
  240.     '
  241.     '    Obtain a copy of the device attribute word from the
  242.     '    device driver identified by FileHandle
  243.     '
  244.  
  245.     InRegs.AX        = &H4400
  246.     InRegs.BX        = FileHandle
  247.  
  248.     CALL INTERRUPT( &H0021, InRegs, OutRegs )
  249.  
  250.     IF ( ( OutRegs.Flags AND &H0001 ) <> 0 ) THEN
  251.         SetBinaryMode    = FAIL
  252.         EXIT FUNCTION
  253.     END IF
  254.  
  255.     '
  256.     '    Or in the bit that indicates that the device will
  257.     '    operate in binary mode
  258.     '
  259.  
  260.     InRegs.AX        = &H4401
  261.     InRegs.BX        = FileHandle
  262.     InRegs.DX        = ( OutRegs.DX AND &H00FF ) OR &H0020
  263.  
  264.     CALL INTERRUPT( &H0021, InRegs, OutRegs )
  265.  
  266.     IF ( ( OutRegs.Flags AND &H0001 ) <> 0 ) THEN
  267.         SetBinaryMode    = FAIL
  268.         EXIT FUNCTION
  269.     END IF
  270.  
  271.     SetBinaryMode    = PASS
  272.  
  273. END    FUNCTION
  274.  
  275.  
  276.