home *** CD-ROM | disk | FTP | other *** search
- // Written by Chris Sokol
-
- #include <string.h>
- #include "tty.h"
-
- int TTY::Avail()
- {
- return TTYAvail(handle);
- }
-
- int TTY::Baud(ulong br)
- {
- int bs;
-
- if (br == 300)
- bs = TTY300;
- else if (br == 1200)
- bs = TTY1200;
- else if (br == 2400)
- bs = TTY2400;
- else if (br == 4800)
- bs = TTY4800;
- else if (br == 9600)
- bs = TTY9600;
- else if (br == 19200)
- bs = TTY19200;
- else if (br == 38400)
- bs = TTY38400;
- else if (br == 57600)
- bs = TTY57600;
- else if (br == 115200)
- bs = TTY115200;
- else
- return 0;
-
- error = TTYPut(handle, TTYtxbaud << 8 + bs);
-
- return !error;
- }
-
- int TTY::Bits(uint bits)
- {
- if (bits < 5 || bits > 8)
- return 0;
-
- linctl &= 0xfc;
- linctl |= bits - 5;
-
- outlinc = 1;
-
- return 1;
- }
-
- int TTY::Brk(int on)
- {
- if (on)
- linctl |= 0x40;
- else
- linctl &= 0xbf;
-
- outlinc = 1;
-
- return 1;
- }
-
- int TTY::CTS()
- {
- return !!(mdmstat & 0x10);
- }
-
- int TTY::Done()
- {
- return TTYDone(handle);
- }
-
- int TTY::DSR()
- {
- return !!(mdmstat & 0x20);
- }
-
- int TTY::DTR(int on)
- {
- if (on)
- mdmctl |= 1;
- else
- mdmctl &= 0xfe;
-
- outmdmc = 1;
-
- return 1;
- }
-
- int TTY::Error()
- {
- int err = error;
-
- error = 0;
- return err;
- }
-
- void TTY::FlushCtl()
- {
- if (outlinc)
- if (!TTYPut(handle, TTYtxlinc << 8 | linctl))
- outlinc = 0;
-
- if (outmdmc)
- if (!TTYPut(handle, TTYtxmdmc << 8 | mdmctl))
- outmdmc = 0;
- }
-
- uint TTY::Get()
- {
- uint d;
-
- d = TTYGet(handle);
-
- switch (d / 256)
- {
- case TTYrxlins:
- linstat = d & 0xff;
- break;
-
- case TTYrxmdms:
- mdmstat = d & 0xff;
- break;
- }
-
- return d;
- }
-
- int TTY::Opened()
- {
- return (handle >= 0);
- }
-
- int TTY::Prty(char p)
- {
- uchar pbits;
-
- switch (p)
- {
- case '-':
- pbits = 0;
- break;
-
- case 'e':
- pbits = 0x18;
- break;
-
- case 'm':
- pbits = 0x38;
- break;
-
- case 'o':
- pbits = 0x08;
- break;
-
- case 's':
- pbits = 0x28;
- break;
-
- default:
- return 0;
- }
-
- linctl &= 0xc7;
- linctl |= pbits;
-
- outlinc = 1;
-
- return 1;
- }
-
- int TTY::Put(uchar c)
- {
- if (outmdmc || outlinc)
- FlushCtl();
-
- error = TTYPut(handle, TTYtxchar << 8 | c);
-
- return !error;
- }
-
- int TTY::RING()
- {
- return !!(mdmstat & 0x40);
- }
-
- int TTY::RLSD()
- {
- return !!(mdmstat & 0x80);
- }
-
- int TTY::RTS(int on)
- {
- if (on)
- mdmctl |= 0x02;
- else
- mdmctl &= 0xfd;
-
- outmdmc = 1;
-
- return 1;
- }
-
- int TTY::Stop(uint bits)
- {
- if (!bits || bits > 2)
- return 0;
-
- if (bits == 2)
- linctl |= 0x04;
- else
- linctl &= 0xfb;
-
- outlinc = 1;
-
- return 1;
- }
-
- TTY::TTY(char *name, uint rxl, uint txl)
- {
- char pname[11];
- int io, irq, type;
-
- strncpy(pname, name, 10);
- pname[10] = 0;
-
- strupr(pname);
-
- if (!strcmp(pname, "COM1"))
- {
- io = 0x3f8;
- irq = 4;
- type = TTYnorm;
- }
- else if (!strcmp(pname, "COM2"))
- {
- io = 0x2f8;
- irq = 3;
- type = TTYnorm;
- }
- else if (!strcmp(pname, "COM3"))
- {
- io = 0x3e8;
- irq = 4;
- type = TTYnorm;
- }
- else if (!strcmp(pname, "COM4"))
- {
- io = 0x2e8;
- irq = 3;
- type = TTYnorm;
- }
- else if (!memcmp(pname, "VER", 3))
- {
- int pnum;
-
- if (pname[3] >= '0' && pname[3] <= '7')
- pnum = pname[3] - '0';
- else
- {
- error = TTYbadtype;
- handle = -1;
-
- return;
- }
-
- io = 0x280;
- irq = 15;
- type = TTYv8at0 + pnum;
- }
-
- linctl = 3;
- linstat = 0;
- mdmctl = 9;
- mdmstat = 0;
- outlinc = 0;
- outmdmc = 0;
- rxbuf = new uint[rxl];
- rxlen = rxl;
- txbuf = new uint[txl];
- txlen = txl;
-
- error = TTYOpen(io, irq, type, rxbuf, rxlen, txbuf, txlen);
-
- if (error >= 0)
- {
- handle = error;
- error = 0;
- }
- else
- handle = -1;
- }
-
- TTY::~TTY()
- {
- if (handle >= 0)
- {
- TTYClose(handle);
-
- delete rxbuf;
- delete txbuf;
- }
- }
-
- int TTY::WaitCTS(int ena)
- {
- error = TTYPut(handle, (TTYtxhwfc << 8) | !!ena);
-
- return !error;
- }
-
- int TTY::XOnOff(int ena)
- {
- error = TTYPut(handle, (TTYtxxofc << 8) | !!ena);
-
- return !error;
- }
-