home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / com_int.arc / COMINT.C next >
Text File  |  1987-09-05  |  12KB  |  360 lines

  1. /*****************************************************************************
  2.         Communication interrupt test program.
  3.  
  4.         Created on : September 5th, 1987
  5.         By         :  Dale Loftis
  6.  
  7.         GENIE Mail :  D.LOFTIS
  8.  
  9. *****************************************************************************/
  10. #include <stdio.h>
  11. #include <dos.h>
  12. /*****************************************************************************
  13.     PROGRAM GLOBAL VARIABLES
  14. *****************************************************************************/
  15. #if __TURBOC__            /* FOR TURBO C only ! */
  16. #define outp(portid,v)    outportb(portid,v)
  17. #endif
  18.  
  19. #define    BAUD110        0x00    /* available BAUD rates in IBM PC/?? */
  20. #define    BAUD150        0x20
  21. #define    BAUD300        0x40
  22. #define    BAUD600        0x60
  23. #define    BAUD1200    0x80
  24. #define    BAUD2400    0xA0
  25. #define    BAUD4800    0xC0
  26. #define    BAUD9600    0xE0
  27.  
  28. #define    NOPARITY    0x00    /* available PARITY options */
  29. #define    ODDPARITY    0x08
  30. #define    EVENPARITY    0x18
  31.  
  32. #define STOPBIT1    0x00    /* available STOP BIT options */
  33. #define STOPBIT2    0x04
  34.  
  35. #define BITS7        0x02    /* available WORD LENGTH options */
  36. #define BITS8        0x03
  37.  
  38. #define    MAXBUF        2048    /* input and output queue size   */
  39.                 /* If you change this be sure to */
  40.                 /* change INT?.ASM's also        */
  41.  
  42. unsigned char com1_output_queue[MAXBUF];/* COM 1 main Transmit buffer */
  43. unsigned int  com1_out_queue_out;    /* removal pointer from output_queue */
  44. unsigned int  com1_out_queue_ptr;    /* insertion pointer to output_queue */
  45.  
  46. unsigned char com1_input_queue[MAXBUF];    /* COM 1 main Receive buffer */
  47. unsigned int  com1_in_queue_out;    /* removal pointer from input_queue */
  48. unsigned int  com1_in_queue_ptr;    /* insertion pointer to input_queue */
  49.  
  50. unsigned char com2_output_queue[MAXBUF];/* COM 2 main Transmit buffer */
  51. unsigned int  com2_out_queue_out;    /* removal pointer from output_queue */
  52. unsigned int  com2_out_queue_ptr;    /* insertion pointer to output_queue */
  53.  
  54. unsigned char com2_input_queue[MAXBUF];    /* COM 2 main Receive buffer */
  55. unsigned int  com2_in_queue_out;    /* removal pointer from input_queue */
  56. unsigned int  com2_in_queue_ptr;    /* insertion pointer to input_queue */
  57.  
  58. unsigned int  com1_rs232_error;        /* current COM 1 error code */
  59. unsigned int  com2_rs232_error;        /* current COM 2 error code */
  60.  
  61. unsigned int  com1_port_status;        /* current COM 1 pin status */
  62. unsigned int  com2_port_status;        /* current COM 2 pin status */
  63.  
  64. unsigned int  com1_interrupt_status;    /* current COM 1 interrupt status */
  65. unsigned int  com2_interrupt_status;    /* current COM 2 interrupt status */
  66. /*****************************************************************************/
  67. main()
  68. {
  69.     while(kbhit())            /* empty keyboard buffer */
  70.        getche();            /* throw away the keys without echoing */
  71.  
  72.     com1_set_interrupt(BAUD1200 |    /* initialize com1 port and interrupt */
  73.                NOPARITY |
  74.                STOPBIT1 |
  75.                BITS8);
  76.  
  77.     com2_set_interrupt(BAUD1200 |    /* initialize com2 port and interrupt */
  78.                NOPARITY |
  79.                STOPBIT1 |
  80.                BITS8);
  81.  
  82.     com1_output_queue_insert("This is how to send out COM 1");
  83.     com2_output_queue_insert("This is how to send out COM 2");
  84.  
  85.     scr_clr();            /* clear screen */
  86.  
  87.     while(!kbhit())            /* go until a key is hit */
  88.     {
  89.         queue_delete(1);    /* check for incoming COM 1 characters */
  90.         queue_delete(2);    /* check for incoming COM 2 characters */
  91.  
  92.         show_com_status();    /* show current com1 and com2 status */
  93.     }
  94.  
  95.     com1_restore_interrupt();    /* replace interrupt vectors before */
  96.     com2_restore_interrupt();    /* leaving otherwise its crash city */
  97. }
  98. /*****************************************************************************
  99.  
  100.     This function displays the status of the buffer pointers, pin status
  101.      and interrupt status.
  102.  
  103.     This is primarily for troubleshooting problems with the interrupts
  104.     and it slows things down to much for continual usage.
  105.  
  106. *****************************************************************************/
  107. show_com_status()
  108. {
  109. int row,col,temp;
  110.  
  111.     temp = read_cursor_position();    /* find where cursor currently sits */
  112.  
  113.     row  = temp >> 8;        /* row is high byte */
  114.     col  = temp & 0xff;        /* column is low byte */
  115.  
  116.     scr_rowcol(24,0);        /* on the bottom line */
  117.  
  118.     printf(
  119. "      %04d - %04d   %04d - %04d  %02xh %02xh %04d - %04d  %04d - %04d  %02xh %02xh  ",
  120.         com1_in_queue_ptr,com1_in_queue_out,
  121.         com1_out_queue_ptr,com1_out_queue_out,
  122.         com1_interrupt_status,com1_port_status,
  123.  
  124.         com2_in_queue_ptr,com2_in_queue_out,
  125.         com2_out_queue_ptr,com2_out_queue_out,
  126.         com2_interrupt_status,com2_port_status
  127.         );
  128.     if(row != 24)
  129.         scr_rowcol(row,col);    /* put cursor back where it was */
  130.     else
  131.     {
  132.         scroll(1,1,80,24,1);    /* scroll screen up             */
  133.         scr_rowcol(23,0);    /* and start new line           */
  134.     }
  135. }
  136. /*****************************************************************************
  137.     This function looks at the COM ? input and output queue pointers
  138.     and displays the characters if they are not equal.
  139. *****************************************************************************/
  140. queue_delete(port)
  141. int port;
  142. {
  143. int row,temp;
  144.  
  145.     switch(port)
  146.     {
  147.     case 1:        /* COM 1 reception queue check */
  148.  
  149.         while(com1_in_queue_ptr != com1_in_queue_out)    /* test for presence of char */
  150.         {
  151.             if(com1_in_queue_out == MAXBUF+1)/* are you at the buffers end? */
  152.                 com1_in_queue_out = 0;    /* YES! then reset to beginning */
  153.  
  154.              if(com1_input_queue[com1_in_queue_out] < 0x20)
  155.             printf("%x",com1_input_queue[com1_in_queue_out++]);
  156.              else
  157.             printf("%c",com1_input_queue[com1_in_queue_out++]);
  158.             
  159.  
  160.             if(com1_rs232_error & 0x1e)    /* mask off non-error conditions */
  161.             {
  162.             temp = read_cursor_position();    /* find where cursor currently sits */
  163.  
  164.             row  = temp >> 8;        /* row is high byte */
  165.             if(row == 24)
  166.             {
  167.                 scroll(1,1,80,24,1);    /* scroll screen up             */
  168.                 scr_rowcol(23,0);    /* and start new line           */
  169.             }
  170.  
  171.                 if(com1_rs232_error & 0x10)
  172.                     printf("Break Detect");
  173.  
  174.                 if(com1_rs232_error &0x08)
  175.                     printf("Framing Error");
  176.  
  177.                 if(com1_rs232_error & 0x04)
  178.                     printf("Parity Error");
  179.  
  180.                 if(com1_rs232_error & 0x02)
  181.                     printf("Overrun Error");
  182.  
  183.                 com1_rs232_error = 0;    /* reset to no errors */
  184.             }
  185.         }
  186.         break;
  187.     case 2:            /* COM 2 reception queue check */
  188.         while(com2_in_queue_ptr != com2_in_queue_out)    /* test for presence of char */
  189.         {
  190.             if(com2_in_queue_out == MAXBUF+1)/* are you at the buffers end? */
  191.                 com2_in_queue_out = 0;    /* YES! then reset to beginning */
  192.     
  193.              if(com2_input_queue[com2_in_queue_out] < 0x20)
  194.             printf("%x",com2_input_queue[com2_in_queue_out++]);
  195.              else
  196.             printf("%c",com2_input_queue[com2_in_queue_out++]);
  197.     
  198.             if(com2_rs232_error & 0x1e)    /* mask off non-error conditions */
  199.             {
  200.             temp = read_cursor_position();    /* find where cursor currently sits */
  201.  
  202.             row  = temp >> 8;        /* row is high byte */
  203.             if(row == 24)
  204.             {
  205.                 scroll(1,1,80,24,1);    /* scroll screen up             */
  206.                 scr_rowcol(23,0);    /* and start new line           */
  207.             }
  208.                 if(com2_rs232_error & 0x10)
  209.                     printf("Break Detect");
  210.     
  211.                 if(com2_rs232_error &0x08)
  212.                     printf("Framing Error");
  213.  
  214.                 if(com2_rs232_error & 0x04)
  215.                     printf("Parity Error");
  216.  
  217.                 if(com2_rs232_error & 0x02)
  218.                     printf("Overrun Error");
  219.  
  220.                 com2_rs232_error = 0;    /* reset to no errors */
  221.             }
  222.         }
  223.         break;
  224.     }
  225. }
  226. /****************************************************************************
  227.        This function places characters into the COM 1 output queue.
  228.  
  229.        Call with:
  230.           com1_output_queue_insert("Send this out COM 1");
  231.  
  232.       Characters will begin sending as soon as the interrupts are enabled.
  233. *****************************************************************************/
  234. com1_output_queue_insert(text)
  235. char *text;
  236. {
  237.     while(*text)            /* go until end of string */
  238.     {
  239.         while( (com1_out_queue_ptr + 1) % MAXBUF ==
  240.             com1_out_queue_out      % MAXBUF)
  241.         {
  242.             outp(0x3f9,0x0f);        /* enable rs232 xmit interrupts */
  243.             printf("COM1 XMIT FULL");
  244.         }
  245.  
  246.         com1_output_queue[com1_out_queue_ptr++] =
  247.                     *text++;     /* install char to xmit */
  248.         outp(0x3f9,0x0f);             /* enable rs232 xmit interrupts */
  249.  
  250.         if(com1_out_queue_ptr == MAXBUF +1)
  251.             com1_out_queue_ptr = 0;    /* reset pointer if wrap-around */
  252.  
  253.     }
  254. }
  255. /****************************************************************************
  256.        This function places characters into the COM 2 output queue.
  257.  
  258.        Call with:
  259.           com2_output_queue_insert("Send this out COM 2");
  260.  
  261.       Characters will begin sending as soon as the interrupts are enabled.
  262. *****************************************************************************/
  263. com2_output_queue_insert(text)
  264. char *text;
  265. {
  266.     while(*text)            /* go until end of string */
  267.     {
  268.         while( (com2_out_queue_ptr + 1) % MAXBUF ==
  269.             com2_out_queue_out      % MAXBUF)
  270.         {
  271.             outp(0x2f9,0x0f);        /* enable rs232 xmit interrupts */
  272.             printf("COM2 XMIT FULL");
  273.         }
  274.  
  275.         com2_output_queue[com2_out_queue_ptr++] =
  276.                     *text++;     /* install char to xmit */
  277.         outp(0x2f9,0x0f);             /* enable rs232 xmit interrupts */
  278.  
  279.         if(com2_out_queue_ptr == MAXBUF +1)
  280.             com2_out_queue_ptr = 0;    /* reset pointer if wrap-around */
  281.  
  282.     }
  283. }
  284. /***************************************************************************
  285.     This function returns the current cursor position.
  286.  
  287.     call with:
  288.         variable = read_cursor_position();
  289.  
  290.     returns int with  row in high byte
  291.                column in low  byte
  292. ***************************************************************************/
  293. int read_cursor_position()
  294. {
  295. union REGS regs;
  296. int temp;
  297.  
  298.     regs.h.ah = 3;            /* func. 3 is read cursor */
  299.     regs.h.bh = 0;            /* video page zero        */
  300.     int86(0x10,®s,®s);    /* int 10h                */
  301.  
  302.     temp  = regs.h.dh * 256;    /* row in High byte       */
  303.     temp |= regs.h.dl;        /* col in Low  byte       */
  304.  
  305.     return(temp);            /* pass value back */
  306. }
  307. /***************************************************************************
  308.     This function positions the cursor at the specified row and column
  309.  
  310.     call with:
  311.         scr_rowcol(row,col);
  312.  
  313.     where row is an integer from 0 to 24.
  314.     and   col is an integer from 0 to 80.
  315.  
  316. ***************************************************************************/
  317. scr_rowcol(row, col)
  318. int row,col;
  319. {
  320. union REGS regs;
  321.         regs.h.ah = 2;        /* display function 2 */
  322.         regs.h.dh = row;    /* assign row         */
  323.         regs.h.dl = col;    /* assign column      */
  324.         regs.h.bh = 0;        /* video page zero    */
  325.         int86(0x10,®s,®s);/* int 10h            */
  326. }
  327. /****************************************************************************/
  328. scr_clr()
  329. {
  330. union REGS regs;
  331.  
  332.     regs.h.ah = 6;        /* video function 6        */
  333.     regs.h.al = 0;        /* 0 means clear screen    */
  334.     regs.h.bh = 0x1f;    /* set video attributes    */
  335.     regs.x.cx = 0;        /* top left row and column */
  336.     regs.h.dh = 24;        /* bottom right row        */
  337.     regs.h.dl = 79;        /* bottom right column     */
  338.     int86(0x10,®s,®s);/* int 10h                 */
  339. }
  340. /****************************************************************************/
  341. scroll(lcol,trow,rcol,brow,lines)   /* scroll a screen area up */
  342.    int lcol, trow, rcol, brow, lines;
  343. {
  344.    union REGS regs;
  345.    regs.h.bh = 0;            /* video page 0 */
  346.    regs.h.dl = --lcol;
  347.    regs.h.dh = --trow;
  348.    regs.h.ah = 2;            /* set cursor position */
  349.    int86(0x10,®s,®s);
  350.  
  351.    regs.h.bh = 0x1f;            /* video attributes */
  352.    regs.h.cl = lcol;
  353.    regs.h.ch = trow;
  354.    regs.h.dl = --rcol;
  355.    regs.h.dh = --brow;
  356.    regs.h.al = lines;
  357.    regs.h.ah = 6;            /* do the scroll */
  358.    int86(0x10,®s,®s);
  359. }
  360.