home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / SER1200.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-14  |  4.0 KB  |  130 lines

  1. /* SER1200.C - From page 495 of "Microsoft C Programming for    */
  2. /* the IBM" by Robert Lafore. This program modifies the pro-    */
  3. /* gram SER300.C to communicate with the serial port at 1200 b. */
  4. /****************************************************************/
  5.  
  6. #include "dos.h"           /*for union REGS*/
  7. #define COM 1              /*serial port no. 0 = COM1, 1 = COM2*/
  8. #define CONF 0x83          /*1200bpi, no par, 1 stop, 8 bits*/
  9. #define RS232 0x14         /*RS-232 serial port int number*/
  10. #define DATAR 0x100        /*data ready bit in status word*/
  11. #define TRUE 1
  12. #define FALSE 0
  13. #define VIDEO 0x10         /*ROM BIOS video int number*/
  14. union REGS inregs, outregs;   /*for ROM BIOS calls*/
  15. int far *fptr;
  16. int row, col;
  17.  
  18. main()
  19. {
  20. char ch, receive();
  21. int j;
  22. init();
  23. clscr();                /*clear screen for putscr()*/
  24.  
  25.    while(TRUE)  {
  26.       if(kbhit())  {    /*if key pressed*/
  27.          ch = getch();
  28.          send(ch);            /*send char to serial port*/
  29.       }
  30.       if(ready())  {          /*if serial port ready*/
  31.          ch = receive();      /*get char*/
  32.          putscr(ch);
  33.       }
  34.    }
  35. }
  36.  
  37. /* clscr() */     /*clear the screen*/
  38. clscr()
  39. {
  40. int addr;
  41.  
  42.    fptr = (int far *) 0xB0000000;
  43.    for(addr = 0; addr < 2000; addr++)
  44.       *(fptr + addr) = 0x0700;
  45.    row = 0; col = 0;          /*initialize for putscr()*/
  46.    inregs.h.ah = 2;           /*'set cursor position' service*/
  47.    inregs.h.dh = row;
  48.    inregs.h.dl = col;
  49.    inregs.h.bh = 0;           /*page number*/
  50.    int86(VIDEO, &inregs, &outregs);
  51. }
  52.  
  53. /* putscr() */    /* puts char on screen at row, col */
  54. putscr(ch)
  55. char ch;
  56. {
  57. int j;
  58.  
  59.    switch(ch)  {
  60.       case(0x0A):             /*if return*/
  61.          col = 0;             /*start at beginning of line*/
  62.          break;
  63.       case(0x0D):             /*if linefeed*/
  64.          ++row;               /*go to next row*/
  65.          break;
  66.       case(0x08):             /*if backspace*/
  67.          if(col > 0)
  68.             --col;            /*backspace cursor*/
  69.          *(fptr + col + row*80) = 0x0700;  /*print space*/
  70.          break;
  71.       default:
  72.          *(fptr + col + row*80) = (int)ch | 0x0700;
  73.          ++col;               /*display & go to next col*/
  74.    }
  75.    if(col >= 80)  {           /*if past end of row*/
  76.       col = 0;                /*do carriage return and*/
  77.       ++row;                  /*linefeed*/
  78.    }
  79.    if(row >= 25)              /*if below bottom row*/
  80.       row = 0;                /*start at top*/
  81.    if(col == 0)               /*if at beginning of line*/
  82.       for(j = 0; j < 80; j++)    /*clear line*/
  83.          *(fptr + j + row*80) = 0x0700;
  84.    inregs.h.ah = 2;           /* 'set cursor position' service*/
  85.    inregs.h.dh = row;
  86.    inregs.h.dl = col;
  87.    inregs.h.bh = 0;           /*page number*/
  88.    int86(VIDEO, &inregs, &outregs);
  89. }
  90.  
  91. /* init() */      /* initialize serial port */
  92. init(conf)
  93. char conf;        /*addr of configuration code*/
  94. {
  95.    inregs.h.ah = 0;        /* 'initialize port' service */
  96.    inregs.x.dx = COM;      /*port number*/
  97.    inregs.h.al = CONF;     /*configuration*/
  98.    int86(RS232, &inregs, &outregs);
  99. }
  100.  
  101. /* send() */      /* send char to serial port */
  102. send(c)
  103. char c;
  104. {
  105.    inregs.h.ah = 1;        /* 'send char' service */
  106.    inregs.x.dx = COM;      /*port number*/
  107.    inregs.h.al = c;        /*character*/
  108.    int86(RS232, &inregs, &outregs);
  109. }
  110.  
  111. /* ready */    /* get serial port ready status */
  112. ready()
  113. {
  114.    inregs.h.ah = 3;        /* 'get status' service */
  115.    inregs.x.dx = COM;      /*port number*/
  116.    int86(RS232, &inregs, &outregs);
  117.    return(outregs.x.ax & DATAR);    /*return 'data ready' status*/
  118. }
  119.  
  120. /* receive */     /* get character from serial port */
  121. char receive()
  122. {
  123.    inregs.h.ah = 2;        /* 'receive char' service */
  124.    inregs.x.dx = COM;      /*port number*/
  125.    int86(RS232, &inregs, &outregs);
  126.    return(outregs.h.al & 0x7F);     /*return character*/
  127.                                     /* (AND off 8th bit*/
  128. }
  129.  
  130.