⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comm.hpp

📁 用C++写的RS232串口异步通讯功能模块
💻 HPP
📖 第 1 页 / 共 2 页
字号:

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
//  COMM.HPP -- header file for C++ programs using MCOMM async routines.   //
//    Zortech C++ 2.0, Turbo C++ 1.00 version.                             //
//                                                                         //
//    Mike Dumdei,  6 Holly Lane,  Texarkana TX  75503    (c) 1989,1990    //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

#if !defined(COMM_HPP)
#define COMM_HPP

#if !defined(MCOMM)
  #define _C_ cdecl
  #define _N_ near
  #define _F_ far
  #define MCOMM
#endif

//////////////////////////////////////////////
//  Return codes                            //
//////////////////////////////////////////////
#define     R_OK           0x0000
#define     R_NOMEM        0x0001
#define     R_BAUDERR      0x0002
#define     R_PARITYERR    0x0004
#define     R_DTABITERR    0x0008
#define     R_STPBITERR    0x0010
#define     R_IRQUSED      0x0020
#define     R_VCTRUSED     0x0040
#define     R_NOPORT       0x0080
#define     R_PORTUSED     0x0100
#define     R_UARTERR      0x0200

#define     R_TXERR        (-1)

//////////////////////////////////////////////
//  Interrupt bit masks                     //
//////////////////////////////////////////////
#define     IRQ3           0x08
#define     IRQ4           0x10

//////////////////////////////////////////////
//  Vector numbers                          //
//////////////////////////////////////////////
#define     VCTR3          0x0b
#define     VCTR4          0x0c

//////////////////////////////////////////////
//  Standard Comm Ports                     //
//   Note - due to the ability to overload  //
//   the open call, COM1 and COM2 are def-  //
//   ined differently in the C++ version.   //
//////////////////////////////////////////////
#define     COM1        0
#define     COM2        1

//////////////////////////////////////////////
//  Defines for async_regs function         //
//////////////////////////////////////////////
#define     RXreg       0
#define     TXreg       0
#define     IERreg      1
#define     IIDreg      2
#define     FCRreg      2
#define     LCRreg      3
#define     MCRreg      4
#define     LSRreg      5
#define     MSRreg      6
#define     LObaud      0
#define     HIbaud      1

//////////////////////////////////////////////
//  Miscellaneous bit masks                 //
//////////////////////////////////////////////
#define     B_DTR        0x0001
#define     B_RTS        0x0002

/////////////////////////////////////////////////////////////////////////
//  Status bit masks                                                   //
/////////////////////////////////////////////////////////////////////////
  //  S T A T 2  //
#define     B_XUSE       0x0001   // using XON/XOFF protocol bit
#define     B_XRXD       0x0002   // XOFF received bit
#define     B_XSENT      0x0004   // XOFF sent bit
#define     B_TXEMPTY    0x0008   // nothing to transmit bit
#define     B_CTS        0x0010   // clear to send bit
#define     B_DSR        0x0020   // data set ready bit
#define     B_FLOWHALT   0x0040   // flow halt indicator bit
#define     B_MNTR_CD    0x0080   // monitor CD bit

  //  S T A T 1  //
#define     B_RXOVF      0x0100   // rx buffer overflow indic bit
#define     B_OVERRUN    0x0200   // char overrun error bit
#define     B_PARITY     0x0400   // parity error bit
#define     B_FRAMING    0x0800   // framing error bit
#define     B_BREAK      0x1000   // line break signal detected
#define     B_THRE       0x2000   // used in interrupt hdlr (always 0)
#define     B_RXEMPTY    0x4000   // rx buffer empty bit
#define     B_CD         0x8000   // carrier detect bit mask

  //  S T A T 3  //
#define     B_IGNERR     0x0001   // discard chars with par, frm'g errs
#define     B_16550      0x0002   // 16550 detected
#define     B_FIFO       0x0004   // 16550 FIFOs enabled
#define     B_ORGFIFO    0x0008   // FIFOs were on when port was opened
#define     B_FARBUFFER  0x0010   // using FAR ring buffers
#define     B_XUSET      0x0020   // using XON/XOFF on txmt side bit
#define     B_XUSER      0x0040   // using XON/XOFF on recv side bit
#define     B_XONANY     0x0080   // accept any char for an XON


/////////////////////////////////////////////////////////////////////////
//                                                                     //
//  A s y n c P o r t   C l a s s                                      //
//                                                                     //
//                    * * *  W A R N I N G  * * *                      //
//     This class must have the port variables byte aligned.  As       //
//     written, ZTC 2.0 will byte align this structure without the     //
//     need to direct it to pack structures.  If you are using         //
//     a different implementation of C++ be sure this data is          //
//     byte aligned by your compiler.                                  //
/////////////////////////////////////////////////////////////////////////

class AsyncPort
{
      // port variables
  private:
    int ComBase;          // base port address of for comm port
    char IRQMask;         // mask for IRQ used by this port
    char Vector;          // interrupt vector used by this port
    char BPDSstr[10];     // current baud,parity,data,stop parms
    int RxSize;           // size of receive bufr
    int TxSize;           // size of transmit bufr
    int RingSeg;          // segment of rx/tx bufrs (leave 0 for NEAR)
    int RingOfst;         // offset of rx/tx bufrs, alias TxTop
    int RxTop;            // ptr to begin of rx bufr, alias TxBtm
    int RxBtm;            // ptr to end of rx bufr (end + 1)
    int TxIn;             // ptr to where next 'put' goes
    int TxOut;            // ptr to where next tx'd comes from
    int TxFree;           // remaining tx bufr space
    int RxIn;             // pointer to where next rx'd goes
    int RxOut;            // ptr to where next 'get' comes from
    int RxFree;           // remaining bytes in rx bufr space
    int BaudDvsr;         // baud rate divisor
    int XoffTrip;         // send XOFF when this many bytes left in rxbuf
    int XonTrip;          // send XON when this many bytes left in rxbuf
    int OldVctrOfst;      // OFST address of original interrupt vector
    int OldVctrSeg;       // SEG address of original interrupt vector
    int OldDvsr;          // original value of Baud Divisor Regs
    char Stat2;           // secondary status byte
    char Stat1;           // primary status byte
    char TxStat;          // determines when tx_intrpts need turned on
    char MSRMask;         // slcts bits in MSR montrd for flow cntrl
    char MSRVal;          // present value of modem status register
    char Stat3;           // another flag, used for XONANY char
    char PDSinfo;         // parity, # dta bits, # stop bits
    char StripMask;       // mask for high bit stripping
    char XTxRptInit;      // initialization value for XTxRpt below
    char XTxRpt;          // countdown val befr repeat tx of XOFF
    char TxImmedChar;     // char to be tx'd 1st opportunity
    char HdlrUsed;        // assigned interrupt handler for this port
    char Old8259Msk;      // original 8259 interrupt mask
    char OldLCR;          // original Line Control Reg value
    char OldMCR;          // original Modem Cntrl Reg value
    char OldIER;          // original Interrupt Enable Reg value
    int TxByteCnt;        // number bytes to send per tx interrupt
    char FCRVal;          // FIFO control register data byte
    char IERVal;          // interrupt enable register value

      // declaration of default ring buffer variables
    static int DefRxBufSz;      // default receive ring buffer size
    static int DefTxBufSz;      // default transmit buffer size
    static int DefMemType;      // default to NEAR or FAR memory flag

  public:
    AsyncPort();                    // constructors
    AsyncPort(int RxBufSz, int TxBufSz, int UseFarMem = 0);

    ~AsyncPort();                   // destructor

                                    // allocate ring buffers function
    int AllocBuffers(int RxBufSz, int TxBufSz, int UseFarMem = 0);
    int FreeBuffers();              // free ring buffers function
                                    // reset default buffers function
    static void SetDefaultBuffers(int RxDefSz, int TxDefSz, int DefMem = 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -