home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ Games Programming
/
CPPGAMES.ISO
/
thx
/
include
/
serial.h
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-08
|
3KB
|
144 lines
// ------ serial.h
#ifndef SERIAL_H
#define SERIAL_H
#include <dos.h>
#undef disable
typedef int bool;
const int true = 1;
const int false = 0;
const int systimer = 8;
class Timer {
int timer;
public:
Timer()
{ timer = -1; }
bool timed_out()
{ return timer == 0; }
void set(int secs)
{ timer=secs*182/10+1; }
void disable()
{ timer = -1; }
bool running()
{ return timer > 0; }
void countdown()
{ --timer; }
bool disabled()
{ return timer == -1; }
};
const int xon = 17;
const int xoff = 19;
const int PIC01 = 0x21; // 8259 Programmable Interrupt Ctrlr
const int PIC00 = 0x20; // " " " "
const int EOI = 0x20; // End of Interrupt command
// --------------- line status register values
const int XmitDataReady = 0x20;
// ------------ modem control register values
const int DTR = 1;
const int RTS = 2;
const int OUT2 = 8;
// ------------ modem status register values
const int RLSD = 0x80;
const int DSR = 0x20;
const int CTS = 0x10;
// ----------- interrupt enable register signals
const int DataReady = 1;
// ------------- serial input interrupt buffer
const int BufSize = 1024;
const int SafetyLevel = (BufSize/4);
const int Threshold = (SafetyLevel*3);
// ----- com port initialization parameter byte
union portinit {
struct {
unsigned wordlen : 2;
unsigned stopbits : 1;
unsigned parity : 3;
unsigned brk : 1;
unsigned divlatch : 1;
} initbits;
char initchar;
};
struct CommParameters {
int port;
int parity;
int stopbits;
int databits;
int baud;
};
class CommPort {
portinit initcom;
char *mp_recvbuff;
bool xonxoff_enabled;
char *mp_nextin, *mp_nextout;
int buffer_count;
CommParameters commparms;
bool waiting_for_xon;
bool waiting_to_send_xon;
static CommPort *mp_CommPort;
int timeout;
static Timer serialtimer;
int BasePort()
{ return (0x3f8-((commparms.port-1)<<8)); }
int TxData()
{ return BasePort(); }
int RxData()
{ return BasePort(); }
int DivLSB()
{ return BasePort(); }
int DivMSB()
{ return BasePort()+1; }
int IntEnable()
{ return BasePort()+1; }
int IntIdent()
{ return BasePort()+2; }
int LineCtl()
{ return BasePort()+3; }
int ModemCtl()
{ return BasePort()+4; }
int LineStatus()
{ return BasePort()+5; }
int ModemStatus()
{ return BasePort()+6; }
int irq()
{ return 4-(commparms.port-1); }
int vector()
{ return 12-(commparms.port-1); }
int ComIRQ()
{ return ~(1 << irq()); }
void CommInterrupt();
friend void interrupt newcomint(...);
friend void interrupt newtimer(...);
public:
CommPort(const CommParameters& cp);
~CommPort();
void Initialize();
int readcomm();
bool writecomm(int ch);
void clear_serial_queue();
bool carrier()
{ return (inp(ModemStatus()) & RLSD) != 0; }
bool input_char_ready()
{ return mp_nextin != mp_nextout; }
void SetTimeout(int to)
{ timeout = to; }
const CommParameters& CommParms()
{ return commparms; }
void EnableXonXoff()
{ xonxoff_enabled = true; }
void DisableXonXoff()
{ xonxoff_enabled = false; }
};
#endif