home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / communic / asycls11 / asynch.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  5.9 KB  |  162 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  Asynchronous Class for C++
  4. //  Copyright (C) 1991 by Jui-Lin Hung and SPD!
  5. //
  6. //  You may freely use or incorporate these routines into your own programs
  7. //  without royalty to me, as I believe this is beneficial to programmers.
  8. //  However, I would like to request that if you distribute the source code,
  9. //  you would include this header in the source file and not remove it.
  10. //  Thank you, and I hope these routines are useful.
  11. //
  12. //  April, 1991 - Version 1.1
  13. //
  14. /////////////////////////////////////////////////////////////////////////////
  15.  
  16.  
  17. #ifndef __ASYNCH_H
  18. #define __ASYNCH_H
  19.  
  20. #ifndef __DOS_H
  21.     #include <dos.h>
  22. #endif
  23.  
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. // Definitions used by the object class Asynch
  29. enum COM_PORTS {
  30.     COM1 =  0x01,   // Com port 1
  31.     COM2 =  0x02,   // Com port 2
  32.     COM3 =  0x03,   // Com port 3
  33.     COM4 =  0x04    // Com port 4
  34. };
  35.  
  36. // Character input/output buffer length define
  37. #define IBUF_LEN    2048    // Incoming buffer
  38. #define OBUF_LEN    1024    // Outgoing buffer
  39.  
  40. // Port addresses for the 8259 Programmable Interrupt Controller (PIC)
  41. #define ICR         0x20    // Interrupt Control Register port
  42. #define IMR         0x21    // Interrupt Mask Register port
  43.  
  44. // An End Of Interrupt needs to be sent to the 8259 control port when
  45. // a hardware interrupt ends
  46. #define EOI         0x20    // End Of Interrupt
  47.  
  48. // TXR  Output data to the serial port
  49. // RXR  Input data from the serial port
  50. // LCR  Initialize the serial port
  51. // IER  Controls interrupt generation
  52. // IIR  Identifies interrupts
  53. // MCR  Send control signals to the modem
  54. // LSR  Monitor the status of the serial port
  55. // MSR  Receive status of the modem
  56. #define TXR         0x00    // Transmit Register    (WRITE)
  57. #define RXR         0x00    // Receive Register     (READ)
  58. #define IER         0x01    // Interrupt Enable Register
  59. #define IIR         0x02    // Interrupt ID Register
  60. #define LCR         0x03    // Line Control Register
  61. #define MCR         0x04    // Modem Control Register
  62. #define LSR         0x05    // Line Status Register
  63. #define MSR         0x06    // Modem Status Register
  64.  
  65. #define DTR         0x01    // Data Terminal Ready
  66. #define RTS         0x02    // Request To Send.  There is data to send.
  67. #define MCI         0x08    // Modem Control Interrupt
  68.  
  69. #define CTS         0x10    // Clear To Send
  70. #define DSR         0x20    // Data Set Ready
  71.  
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74.  
  75.  
  76. // Asynchronous port information structure
  77. typedef struct _ainfo_t {
  78.     unsigned char   port;               // serial port
  79.     volatile int    inhead,intail;      // pointers to the in buffer
  80.     volatile int    outhead,outtail;    // pointers to the out buffer
  81.     volatile char   inbuf[IBUF_LEN];    // in buffer
  82.     volatile char   outbuf[OBUF_LEN];   // out buffer
  83.     int             base;               // base address to this port
  84.     int             irq;                // interrupt number for this port
  85.     int             flow;               // high-speed modem flow control
  86.     unsigned int    baud;               // maximum baud speed for modem
  87.     int             nohangup;           // hang up at end of session?
  88. } _ainfo_t;
  89.  
  90. extern _ainfo_t _ainfo;
  91.  
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94.  
  95.  
  96. // Function prototype for serial interrupt
  97. void far interrupt asynch_irq(...); // interrupt handler
  98.  
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101.  
  102.  
  103. // Asynchronous object class definition
  104. class Asynch {
  105. private:
  106.     // Private member functions
  107.     void asynchInit();              // interrupt initialization
  108. public:
  109.     // Constructors/Destructors
  110.     Asynch(unsigned char);          // constructors
  111.     Asynch(unsigned char,unsigned int);
  112.    ~Asynch(void);                   // destructor
  113.  
  114.     // Public member functions
  115.     void setBaud(unsigned int);     // sets the baudrate
  116.     int dtr(void);                  // returns 1 if dtr high, 0 if not
  117.     inline void setDtr(void);       // sets DTR high
  118.     void dropDtr(void);             // sets DTR low
  119.  
  120.     // Operator overloaders
  121.     Asynch &operator<<(char);       // overloaded << operator for chars
  122.     Asynch &operator<<(char *);     // overloaded << operator for strings
  123.     Asynch &operator>>(char &);     // overloaded >> operator for chars
  124.  
  125.     // C type functions
  126.     char ainkey(void);              // inputs character from port
  127.     void aputch(char);              // outputs character to port
  128.     void aputs(char *);             // outputs string to port
  129.     void aprintf(char *, ...);      // outputs formatted string to port
  130.  
  131.     // Inline functions
  132.     int  inCount(void)              // counts chars left in the in buffer
  133.         {
  134.             int len = _ainfo.intail - _ainfo.inhead;
  135.             return((len>0) ? len:(-len));
  136.         }
  137.     int  outCount(void)             // counts chars left in the out buffer
  138.         {
  139.             int len = _ainfo.outtail - _ainfo.outhead;
  140.             return((len>0) ? len:(-len));
  141.         }
  142.     void flushInBuf(void)           // dumps in buffer
  143.         {
  144.             disable();                          // disable interrupts
  145.             _ainfo.inhead=_ainfo.intail=0;      // dump buffer
  146.             enable();                           // enable interrupts
  147.         }
  148.     void flushOutBuf(void)          // dumps out buffer
  149.         {
  150.             disable();                          // disable interrupts
  151.             _ainfo.outhead=_ainfo.outtail=0;    // dump buffer
  152.             enable();                           // enable interrupts
  153.         }
  154.     void flushAllBufs(void)         // flushes both input and output buffers
  155.         {
  156.             flushInBuf(), flushOutBuf();
  157.         }
  158. };
  159.  
  160.  
  161. #endif // __ASYNCH_H
  162.