home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / SERIAL-1.ZIP / SERIAL.CPP next >
C/C++ Source or Header  |  1990-05-14  |  8KB  |  347 lines

  1.  
  2.   /*------------------------------------------------------------------*
  3.       SERIAL.CPP
  4.  
  5.       The following code shows how to take advantage of some of
  6.       the Turbo C++ extensions to the C++ language to do asynchronous
  7.       communications without having to write supporting assembly-
  8.       language routines.
  9.  
  10.       This program bypasses the less-than-adequate PC BIOS
  11.       communications routines and installs a serial interrupt
  12.       handler. Direct access to PC hardware allows the program to
  13.       run at faster baud rates and eliminates the need for
  14.       the main program to continuously poll the serial port for
  15.       data; thus implementing background communications. Data that
  16.       enters the serial port is stored in a circular buffer.
  17.  
  18.       * Compile this program with Test Stack Overflow OFF.
  19.  
  20.   *------------------------------------------------------------------*/
  21.  
  22. #include <dos.h>
  23. #include <conio.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "serial.h"
  27.  
  28. #define VERSION 0x0101
  29.  
  30. #define FALSE           0
  31. #define TRUE           (!FALSE)
  32.  
  33. #define NOERROR         0       /* No error               */
  34. #define BUFOVFL         1       /* Buffer overflowed      */
  35.  
  36. #define ESC             0x1B    /* ASCII Escape character */
  37. #define ASCII           0x007F  /* Mask ASCII characters  */
  38. #define SBUFSIZ         0x4000  /* Serial buffer size     */
  39.  
  40. int            SError          = NOERROR;
  41. int            portbase        = 0;
  42. void           interrupt(*oldvects[2])(...);
  43.  
  44. static   char  ccbuf[SBUFSIZ];
  45. unsigned int   startbuf        = 0;
  46. unsigned int   endbuf          = 0;
  47.  
  48.  
  49. /* Handle communications interrupts and put them in ccbuf */
  50. void interrupt com_int(...)
  51. {
  52.     disable();
  53.     if ((inportb(portbase + IIR) & RX_MASK) == RX_ID)
  54.     {
  55.         if (((endbuf + 1) & SBUFSIZ - 1) == startbuf)
  56.             SError = BUFOVFL;
  57.  
  58.         ccbuf[endbuf++] = inportb(portbase + RXR);
  59.         endbuf &= SBUFSIZ - 1;
  60.     }
  61.  
  62.     /* Signal end of hardware interrupt */
  63.     outportb(ICR, EOI);
  64.     enable();
  65. }
  66.  
  67. /* Output a character to the serial port */
  68. serial& serial::operator<<( char x )
  69. {
  70.     long int           timeout = 0x0000FFFFL;
  71.  
  72.     outportb(portbase + MCR,  MC_INT | DTR | RTS);
  73.  
  74.     /* Wait for Clear To Send from modem */
  75.     while ((inportb(portbase + MSR) & CTS) == 0)
  76.         if (!(--timeout))
  77.             return *this;
  78.  
  79.     timeout = 0x0000FFFFL;
  80.  
  81.     /* Wait for transmitter to clear */
  82.     while ((inportb(portbase + LSR) & XMTRDY) == 0)
  83.         if (!(--timeout))
  84.             return *this;
  85.  
  86.     disable();
  87.     outportb(portbase + TXR, x);
  88.     enable();
  89.  
  90.     return *this;
  91. }
  92.  
  93. /* Output a string to the serial port */
  94. serial& serial::operator<<( char *string )
  95. {
  96.     while (*string)
  97.     {
  98.        (*this) << *string;
  99.        string++;
  100.     }
  101.     return *this;
  102. }
  103.  
  104. /* This routine returns the current value in the buffer */
  105. serial &serial::operator>>( char &ch )
  106. {
  107.     if (endbuf == startbuf)
  108.     {
  109.         ch = -1;
  110.         return *this;
  111.     }
  112.  
  113.     ch = ccbuf[startbuf];
  114.     startbuf++;
  115.     startbuf %= SBUFSIZ;
  116.     return *this;
  117. }
  118.  
  119. /* Install our functions to handle communications */
  120. void setvects(void)
  121. {
  122.     oldvects[0] = getvect(0x0B);
  123.     oldvects[1] = getvect(0x0C);
  124.     setvect(0x0B, com_int);
  125.     setvect(0x0C, com_int);
  126. }
  127.  
  128. /* Uninstall our vectors before exiting the program */
  129. void resvects(void)
  130. {
  131.     setvect(0x0B, oldvects[0]);
  132.     setvect(0x0C, oldvects[1]);
  133. }
  134.  
  135. /* Turn on communications interrupts */
  136. void i_enable(int pnum)
  137. {
  138.     int                c;
  139.  
  140.     disable();
  141.     c = inportb(portbase + MCR) | MC_INT;
  142.     outportb(portbase + MCR, c);
  143.     outportb(portbase + IER, RX_INT);
  144.     c = inportb(IMR) & (pnum == COM1 ? IRQ4 : IRQ3);
  145.     outportb(IMR, c);
  146.     enable();
  147. }
  148.  
  149. /* Turn off communications interrupts */
  150. void i_disable(void)
  151. {
  152.     int                c;
  153.  
  154.     disable();
  155.     c = inportb(IMR) | ~IRQ3 | ~IRQ4;
  156.     outportb(IMR, c);
  157.     outportb(portbase + IER, 0);
  158.     c = inportb(portbase + MCR) & ~MC_INT;
  159.     outportb(portbase + MCR, c);
  160.     enable();
  161. }
  162.  
  163. /* Tell modem that we're ready to go */
  164. void comm_on(void)
  165. {
  166.     int                c, pnum;
  167.  
  168.     pnum = (portbase == COM1BASE ? COM1 : COM2);
  169.     i_enable(pnum);
  170.     c = inportb(portbase + MCR) | DTR | RTS;
  171.     outportb(portbase + MCR, c);
  172. }
  173.  
  174. /* Go off-line */
  175. void serial::comm_off(void)
  176. {
  177.     i_disable();
  178.     outportb(portbase + MCR, 0);
  179. }
  180.  
  181. void serial::init_serial(void)
  182. {
  183.     endbuf = startbuf = 0;
  184.     setvects();
  185.     comm_on();
  186. }
  187.  
  188. serial::~serial()
  189. {
  190.     comm_off();
  191.     resvects();
  192. }
  193.  
  194. /* Set the port number to use */
  195. int serial::SetPort(int Port)
  196. {
  197.     int                Offset, far *RS232_Addr;
  198.  
  199.     switch (Port)
  200.     { /* Sort out the base address */
  201.       case COM1 : Offset = 0x0000;
  202.                   break;
  203.       case COM2 : Offset = 0x0002;
  204.                   break;
  205.       default   : return (-1);
  206.     }
  207.  
  208.     RS232_Addr = (int far *)MK_FP(0x0040, Offset);  /* Find out where the port is. */
  209.     if (*RS232_Addr == NULL) return (-1);/* If NULL then port not used. */
  210.     portbase = *RS232_Addr;              /* Otherwise set portbase      */
  211.  
  212.     return (0);
  213. }
  214.  
  215. /* This routine sets the speed; will accept funny baud rates. */
  216. /* Setting the speed requires that the DLAB be set on.        */
  217. int serial::SetSpeed(int Speed)
  218. {
  219.     char        c;
  220.     int        divisor;
  221.  
  222.     if (Speed == 0)            /* Avoid divide by zero */
  223.         return (-1);
  224.     else
  225.         divisor = (int) (115200L/Speed);
  226.  
  227.     if (portbase == 0)
  228.         return (-1);
  229.  
  230.     disable();
  231.     c = inportb(portbase + LCR);
  232.     outportb(portbase + LCR, (c | 0x80)); /* Set DLAB */
  233.     outportb(portbase + DLL, (divisor & 0x00FF));
  234.     outportb(portbase + DLH, ((divisor >> 8) & 0x00FF));
  235.     outportb(portbase + LCR, c);          /* Reset DLAB */
  236.     enable();
  237.  
  238.     return (0);
  239. }
  240.  
  241. /* Set other communications parameters */
  242. int serial::SetOthers(int Parity, int Bits, int StopBit)
  243. {
  244.     int                setting;
  245.  
  246.     if (portbase == 0)                    return (-1);
  247.     if (Bits < 5 || Bits > 8)                return (-1);
  248.     if (StopBit != 1 && StopBit != 2)            return (-1);
  249.     if (Parity != NO_PARITY && Parity != ODD_PARITY && Parity != EVEN_PARITY)
  250.                             return (-1);
  251.  
  252.     setting  = Bits-5;
  253.     setting |= ((StopBit == 1) ? 0x00 : 0x04);
  254.     setting |= Parity;
  255.  
  256.     disable();
  257.     outportb(portbase + LCR, setting);
  258.     enable();
  259.  
  260.     return (0);
  261. }
  262.  
  263. /* Set up the port */
  264. serial::serial(int Port, int Speed, int Parity, int Bits, int StopBit)
  265. {
  266.     flag = 0;
  267.     if (SetPort(Port))
  268.       flag = -1;
  269.     if (SetSpeed(Speed))
  270.       flag = -1;
  271.     if (SetOthers(Parity, Bits, StopBit))
  272.       flag = -1;
  273.  
  274.     if (!flag)
  275.        init_serial();
  276. }
  277.  
  278. /*  Control-Break interrupt handler */
  279. int c_break(void)
  280. {
  281.     i_disable();
  282.     fprintf(stderr, "\nStill online.\n");
  283.  
  284.     return(0);
  285. }
  286.  
  287. main()
  288. {
  289.     /* Communications parameters */
  290.     int        port     = COM2;
  291.     int        speed    = 2400;
  292.     int        parity   = NO_PARITY;
  293.     int        bits     = 8;
  294.     int        stopbits = 1;
  295.  
  296.     int        done  = FALSE;
  297.     char       c;
  298.  
  299.     serial comport(port, speed, parity, bits, stopbits);
  300.  
  301.     ctrlbrk(c_break);
  302.  
  303.     fprintf(stdout, "TURBO C TERMINAL\n"
  304.             "...You're now in terminal mode, "
  305.             "press [ESC] to quit...\n\n");
  306.  
  307.     /*
  308.        The main loop acts as a dumb terminal. We repeatedly
  309.        check the keyboard buffer, and communications buffer.
  310.     */
  311.     do {
  312.         if (kbhit())
  313.         {
  314.             /* Look for an Escape key */
  315.             switch (c=getch())
  316.             {
  317.                 case ESC: done = TRUE;  /* Exit program */
  318.                           break;
  319.  
  320.                 /* You may want to handle other keys here... */
  321.             }
  322.             if (!done)
  323.                 comport << c;
  324.         }
  325.         comport >> c;
  326.         if (c != -1)
  327.             fputc(c & ASCII, stdout);
  328.  
  329.     } while (!done && !SError);
  330.  
  331.     /* Check for errors */
  332.     switch (SError)
  333.     {
  334.         case NOERROR: fprintf(stderr, "\nbye.\n");
  335.                       return (0);
  336.  
  337.         case BUFOVFL: fprintf(stderr, "\nBuffer Overflow.\n");
  338.                       return (99);
  339.  
  340.         default:      fprintf(stderr, "\nUnknown Error, SError = %d\n",
  341.                               SError);
  342.                       return (99);
  343.     }
  344. }
  345.  
  346.  
  347.