home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / PROGRAMM / SERIAL.ZIP / DUH1.C < prev    next >
C/C++ Source or Header  |  1992-01-29  |  3KB  |  151 lines

  1. /********************************************************************
  2. * duh1.c - An extremely dumb terminal program (ROM BIOS Version)
  3. *          Copyright (c) 1992 By Mark Goodwin
  4. ********************************************************************/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dos.h>
  8.  
  9. #define TRUE 1
  10. #define FALSE 0
  11.  
  12. #define PORT 2                        /* serial port */
  13. #define BAUDRATE 2400                /* baud rate */
  14.  
  15. /* function prototypes */
  16. void open_port(int n, int b);
  17. void put_serial(int n, int c);
  18. int get_serial(int n);
  19. int in_ready(int n);
  20.  
  21. void main(void)
  22. {
  23.     int c;
  24.  
  25.     printf("Duh No. 1 - An Extremely Dumb Terminal Program\n");
  26.     printf("Copyright (c) 1992 By Mark Goodwin\n\n");
  27.  
  28.     /* open the serial port */
  29.     open_port(PORT, BAUDRATE);
  30.  
  31.     /* main program loop */
  32.     while (TRUE) {
  33.         /* process keyboard presses */
  34.         if (kbhit()) {
  35.             c = getch();
  36.             switch (c) {
  37.                 case 0:                        /* exit on Alt-X */
  38.                     if (getch() == 45)
  39.                         exit(0);
  40.                     break;
  41.                 default:
  42.                     put_serial(PORT, c);    /* send to the serial port */
  43.             }
  44.         }
  45.         /* process remote characters */
  46.         if (in_ready(PORT)) {
  47.             /* get character from serial port*/
  48.             c = get_serial(PORT);
  49.             /* display it if one was available */
  50.             if (c != EOF)
  51.                 putch(c);
  52.         }
  53.     }
  54. }
  55.  
  56. /* open serial port using ROM BIOS routine */
  57. void open_port(int n, int b)
  58. {
  59.     union REGS regs;
  60.  
  61.     /* load AH with the function code */
  62.     regs.h.ah = 0x00;
  63.     /* load AL with the baud rate and 8-N-1 */
  64.     switch (b) {
  65.         case 9600:
  66.             regs.h.al = 0xe3;
  67.             break;
  68.         case 4800:
  69.             regs.h.al = 0xc3;
  70.             break;
  71.         case 2400:
  72.             regs.h.al = 0xa3;
  73.             break;
  74.         case 1200:
  75.             regs.h.al = 0x83;
  76.             break;
  77.         case 300:
  78.             regs.h.al = 0x43;
  79.             break;
  80.         case 150:
  81.             regs.h.al = 0x23;
  82.             break;
  83.         default:
  84.             regs.h.al = 0x03;
  85.     }
  86.     /* load DX with the port number */
  87.     if (n == 1)
  88.         regs.x.dx = 0;
  89.     else
  90.         regs.x.dx = 1;
  91.     /* call the ROM BIOS routine */
  92.     int86(0x14, ®s, ®s);
  93. }
  94.  
  95. /* put character to serial port */
  96. void put_serial(int n, int c)
  97. {
  98.     union REGS regs;
  99.  
  100.     /* load AH with the function code */
  101.     regs.h.ah = 0x01;
  102.     /* load AL with the character */
  103.     regs.h.al = c;
  104.     /* load DX with the port number */
  105.     if (n == 1)
  106.         regs.x.dx = 0;
  107.     else
  108.         regs.x.dx = 1;
  109.     /* call the ROM BIOS routine */
  110.     int86(0x14, ®s, ®s);
  111. }
  112.  
  113. /* get character from serial port */
  114. int get_serial(int n)
  115. {
  116.     union REGS regs;
  117.  
  118.     /* load AH with the function code */
  119.     regs.h.ah = 0x02;
  120.     /* load DX with the port number */
  121.     if (n == 1)
  122.         regs.x.dx = 0;
  123.     else
  124.         regs.x.dx = 1;
  125.     /* call the ROM BIOS routine */
  126.     int86(0x14, ®s, ®s);
  127.     /* return EOF if timed out */
  128.     if (regs.h.ah & 0x80)
  129.         return EOF;
  130.     return regs.h.al;
  131. }
  132.  
  133. /* check to see if a character is ready */
  134. int in_ready(int n)
  135. {
  136.     union REGS regs;
  137.  
  138.     /* load AH with the function code */
  139.     regs.h.ah = 0x03;
  140.     /* load DX with the port number */
  141.     if (n == 1)
  142.         regs.x.dx = 0;
  143.     else
  144.         regs.x.dx = 1;
  145.     /* call the ROM BIOS routine */
  146.     int86(0x14, ®s, ®s);
  147.     /* check for received data ready */
  148.     if (regs.h.ah & 1)
  149.         return TRUE;
  150.     return FALSE;
  151. }