home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / comos2.zip / COMMOS2.HPP < prev    next >
Text File  |  1993-02-21  |  11KB  |  285 lines

  1. /**************************************************************************
  2.  *
  3.  *  COMMOS2.HPP
  4.  *
  5.  *  This file provides the headers for the IOPL and basic serial services
  6.  *  provided by this library.
  7.  *_________________________________________________________________________
  8.  *
  9.  *  Copyright (c) 1992 by ASH Software, Inc.
  10.  *
  11.  *  Update History
  12.  *
  13.  *    11/28/1992 - Module created
  14.  *
  15.  **************************************************************************/
  16.  
  17. #if !defined(COMMOS2__HPP)
  18.   #define COMMOS2__HPP
  19.  
  20.   //
  21.   // Basic definitions
  22.   //
  23.  
  24.   #define TIMEOUT_INDEFINITE          -1L
  25.  
  26.   #define PARITY_NONE                  0
  27.   #define PARITY_ODD                   1
  28.   #define PARITY_EVEN                  2
  29.   #define PARITY_MARK                  3
  30.   #define PARITY_SPACE                 4
  31.  
  32.   #define STOPBITS_1                   0
  33.   #define STOPBITS_1_POINT_5           1
  34.   #define STOPBITS_2                   2
  35.  
  36.   #define TRANSMIT_BREAK               0x01
  37.   #define TRANSMIT_NO_BREAK            0x00
  38.  
  39.   //
  40.   // Comm Mode definitions
  41.   //
  42.  
  43.   #define COMM_MODE_NOTSTARTED                     0ul
  44.   #define COMM_MODE_OPEN                           1ul
  45.   #define COMM_MODE_WRITE                          2ul
  46.   #define COMM_MODE_READ                           3ul
  47.   #define COMM_MODE_READTIMEOUT                    4ul
  48.   #define COMM_MODE_READUNTILBYTE                  5ul
  49.   #define COMM_MODE_SETBAUD                        6ul
  50.   #define COMM_MODE_SETLINECNTRL                   7ul
  51.   #define COMM_MODE_CLEARTXBUFFER                  8ul
  52.   #define COMM_MODE_CLEARRXBUFFER                  9ul
  53.   #define COMM_MODE_QUERYRXCOUNT                  10ul
  54.   #define COMM_MODE_QUERYRXBUFSIZE                11ul
  55.   #define COMM_MODE_QUERYTXCOUNT                  12ul
  56.   #define COMM_MODE_QUERYTXBUFSIZE                13ul
  57.   #define COMM_MODE_GETDCBINFO                    14ul
  58.   #define COMM_MODE_SETDCBINFO                    15ul
  59.   #define COMM_MODE_SETREADTIMEOUT                16ul
  60.   #define COMM_MODE_SETWRITETIMEOUT               17ul
  61.  
  62.   //
  63.   // Comm Error Codes
  64.   //
  65.  
  66.   #define COMM_ERROR_NOERROR                       0ul
  67.   #define COMM_ERROR_SYSINITFAILED                 1ul
  68.   #define COMM_ERROR_INVALIDMAXPORTS               2ul
  69.   #define COMM_ERROR_ALREADYINITIALIZED            3ul
  70.   #define COMM_ERROR_MAXPORTSEXCEEDED              4ul
  71.   #define COMM_ERROR_OPENFAILED                    5ul
  72.   #define COMM_ERROR_PORTNOTFOUND                  6ul
  73.   #define COMM_ERROR_WRITEFAILED                   7ul
  74.   #define COMM_ERROR_READFAILED                    8ul
  75.   #define COMM_ERROR_IOCTLFAILED                   9ul
  76.   #define COMM_ERROR_TIMEOUTEXCEEDED              10ul
  77.   #define COMM_ERROR_BAUDFAILED                   11ul
  78.   #define COMM_ERROR_LINECNTRLFAILED              12ul
  79.   #define COMM_ERROR_CLEARBUFFERFAILED            13ul
  80.   #define COMM_ERROR_SEMAPHOREFAILED              14ul
  81.   #define COMM_ERROR_DCBINFOFAILED                15ul
  82.   #define COMM_ERROR_SETTIMEOUTFAILED             16ul
  83.  
  84.   //
  85.   // IOPL 16-bit interface function prototypes to be called by
  86.   // 32-bit applications
  87.   //
  88.  
  89.   VOID APIENTRY16 PortOutByte(USHORT usPortAddress,BYTE bParam);
  90.   VOID APIENTRY16 PortOutWord(USHORT usPortAddress,USHORT usParam);
  91.   APIRET16 APIENTRY16 PortInpByte(USHORT usPortAddress);
  92.   APIRET16 APIENTRY16 PortInpWord(USHORT usPortAddress);
  93.  
  94. /*------------------------------------------------------------------------*
  95.  *  class CommPort
  96.  *
  97.  *  This class provides support for serial communications under OS/2 2.x.
  98.  *  This class assumes the presence of an appropriate device driver for
  99.  *  the serial port(s) which will be addressed as the class uses Dos...
  100.  *  APIs to provide the port access.
  101.  *
  102.  *  All methods return COMM_ERROR_NOERROR (which is defined to be 0) if
  103.  *  successful or the CommPort class error number if a failure occurred.
  104.  *
  105.  *  Public Methods
  106.  *
  107.  *    CommPort(VOID)
  108.  *      This constructor initializes the class.  There are no arguments.
  109.  *
  110.  *    ~CommPort(VOID)
  111.  *      This destructor destroys the class.  There are no arguments.
  112.  *
  113.  *    ULONG Open(INT iCommPort)
  114.  *      This method opens the port.  The argument is the port number
  115.  *      which must be 1 <= iCommPort <= number of ports.
  116.  *
  117.  *    ULONG GetLastError(PULONG pulCommError,PULONG pulDosError,
  118.  *                       PULONG pulCommMode)
  119.  *      This method returns the last error generated inside the class.
  120.  *      If successful, on return pulCommError will contain the class
  121.  *      error code, pulDosError will contain the Dos error code, and
  122.  *      pulCommMode will contain the mode which was active when the
  123.  *      error occurred.
  124.  *
  125.  *    ULONG Initialize(USHORT usBaudRate = 9600,
  126.  *                     USHORT usParity   = PARITY_NONE,
  127.  *                     USHORT usDataBits = 8,
  128.  *                     USHORT usStopBits = STOPBITS_1,
  129.  *                     USHORT usTxBreak  = TRANSMIT_NO_BREAK)
  130.  *      This method initializes a comm port.  The arguments are the
  131.  *      baud rate, the parity, the number of data bits, the number of
  132.  *      stop bits, and the transmit break flag.
  133.  *
  134.  *    ULONG ClearBuffers(VOID)
  135.  *      This method clears both the transmit and receive buffers.  There
  136.  *      are no arguments.
  137.  *
  138.  *    ULONG ClearTxBuffer(VOID)
  139.  *      This method clears the transmit buffer.  There are no arguments.
  140.  *
  141.  *    ULONG ClearRxBuffer(VOID)
  142.  *      This method clears the receive buffer.  There are no arguments.
  143.  *
  144.  *    ULONG Write(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  145.  *                PULONG pulWritten)
  146.  *      This method writes the data stored in pucDataArea to the comm
  147.  *      port.  The number of characters to be written is passed in
  148.  *      ulDataAreaSize and the number of characters actually written
  149.  *      are returned in pulWritten.
  150.  *
  151.  *    ULONG Read(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  152.  *               PULONG pulRead);
  153.  *      This method reads data from the comm port and stores the data
  154.  *      in the buffer pointed to by pucDataArea.  The size of the buffer
  155.  *      is passed in ulDataAreaSize and the number of characters read
  156.  *      is placed in the location pointed to by pulRead.  This routine
  157.  *      does not wait for characters to arrive and returns only those
  158.  *      characters which are available when the method is invoked.
  159.  *
  160.  *    ULONG ReadTimeOut(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  161.  *                      PULONG pulRead,LONG lTimeOutmSec);
  162.  *      This method reads data from the comm port and stores the data
  163.  *      in the buffer pointed to by pucDataArea.  The size of the buffer
  164.  *      is passed in ulDataAreaSize and the number of characters read
  165.  *      is placed in the location pointed to by pulRead.  This routine
  166.  *      returns when the number of characters specified are available
  167.  *      or the number of milliseconds specified in lTimeOutmSec has
  168.  *      elapsed.  If a time out occurs, no characters are read from the
  169.  *      port.
  170.  *
  171.  *    ULONG ReadUntilByte(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  172.  *                        PULONG pulRead,UCHAR ucWaitByte,
  173.  *                        LONG lTimeOutmSec);
  174.  *      This method reads data from the comm port and stores the data
  175.  *      in the buffer pointed to by pucDataArea.  The size of the buffer
  176.  *      is passed in ulDataAreaSize and the number of characters read
  177.  *      is placed in the location pointed to by pulRead.  This routine
  178.  *      returns when the number of characters specified are available,
  179.  *      or the specified byte is received, or the number of milliseconds
  180.  *      specified in lTimeOutmSec has elapsed.  If a time out occurs, 
  181.  *      all characters read until that time are returned.
  182.  *
  183.  *    ULONG QueryRxCount(PULONG pulCount)
  184.  *      This method returns the number of characters in the receive 
  185.  *      buffer in pulCount.
  186.  *
  187.  *    ULONG QueryRxBufferSize(PULONG pulSize)
  188.  *      This method returns the size of the receive buffer in pulSize.
  189.  *
  190.  *    ULONG QueryTxCount(PULONG pulCount)
  191.  *      This method returns the number of characters in the transmit
  192.  *      buffer in pulCount.
  193.  *
  194.  *    ULONG QueryTxBufferSize(PULONG pulSize)
  195.  *      This method returns the size of the transmit buffer in pulSize.
  196.  *
  197.  *    ULONG Close(VOID)
  198.  *      This method closes the comm port.  The class can then be used
  199.  *      to open the same port or another port at a later time.  There
  200.  *      are no arguments.
  201.  *
  202.  *    ULONG SetDCBInfo(VOID)
  203.  *      This method sets the DCB information for the comm port.
  204.  *
  205.  *    ULONG GetDCBInfo(VOID)
  206.  *      This method gets the DCB information for the comm port.
  207.  *
  208.  *    ULONG SetReadTimeOut(ULONG ulTimeMsec)
  209.  *      This method sets the read timeout for the comm port.
  210.  *
  211.  *    ULONG SetWriteTimeOut(ULONG ulTimeMsec)
  212.  *      This method sets the write timeout for the comm port.
  213.  *------------------------------------------------------------------------*/
  214.  
  215.   class CommPort
  216.     {
  217.     private:
  218.  
  219.       HFILE
  220.         hCommPort;
  221.  
  222.       HMTX
  223.         hCommReadSem,
  224.         hCommWriteSem;
  225.  
  226.       DCBINFO
  227.         DCBOriginal,
  228.         DCBInfo;
  229.  
  230.       INT
  231.         fPortOpen,
  232.         fPortOpened;
  233.  
  234.       USHORT 
  235.         usBaudRate,
  236.         usParity,
  237.         usDataBits,
  238.         usStopBits,
  239.         usTxBreak;
  240.  
  241.       ULONG
  242.         ulCommError,
  243.         ulDosError,
  244.         ulCommMode;
  245.  
  246.       ULONG QueryQueue(USHORT usMode,PULONG pulValue);
  247.  
  248.     public:
  249.  
  250.       CommPort(VOID);
  251.       ~CommPort(VOID);
  252.       ULONG Open(INT iCommPort);
  253.       ULONG GetLastError(PULONG pulCommError,PULONG pulDosError,
  254.                          PULONG pulCommMode);
  255.       ULONG Initialize(USHORT usBaudRate = 9600,
  256.                        USHORT usParity   = PARITY_NONE,
  257.                        USHORT usDataBits = 8,
  258.                        USHORT usStopBits = STOPBITS_1,
  259.                        USHORT usTxBreak  = TRANSMIT_NO_BREAK);
  260.       ULONG ClearBuffers(VOID);
  261.       ULONG ClearTxBuffer(VOID);
  262.       ULONG ClearRxBuffer(VOID);
  263.       ULONG Write(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  264.                   PULONG pulWritten);
  265.       ULONG Read(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  266.                  PULONG pulRead);
  267.       ULONG ReadTimeOut(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  268.                         PULONG pulRead,LONG lTimeOutmSec);
  269.       ULONG ReadUntilByte(PUCHAR pucDataArea,ULONG ulDataAreaSize,
  270.                           PULONG pulRead,UCHAR ucWaitByte,
  271.                           LONG lTimeOutmSec);
  272.       ULONG QueryRxCount(PULONG pulCount);
  273.       ULONG QueryRxBufferSize(PULONG pulSize);
  274.       ULONG QueryTxCount(PULONG pulCount);
  275.       ULONG QueryTxBufferSize(PULONG pulSize);
  276.       ULONG Close(VOID);
  277.       ULONG SetDCBInfo(VOID);
  278.       ULONG GetDCBInfo(VOID);
  279.       ULONG SetReadTimeOut(ULONG ulTimeMsec);
  280.       ULONG SetWriteTimeOut(ULONG ulTimeMsec);
  281.     };
  282.   typedef CommPort *pCommPort;
  283.  
  284. #endif
  285.