home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12088a < prev    next >
Text File  |  1991-07-27  |  6KB  |  200 lines

  1. /** LISTING 6 ****** TERM.C **************************
  2.    Simple terminal emulation program that 
  3.    demonstrates the use of the UART API.
  4. *****************************************************/
  5.  
  6. #include <conio.h>
  7. #include <malloc.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stddef.h>
  11. #include "uart.h"
  12.  
  13. void Usage(void);
  14.  
  15. main()
  16. {
  17.    static char a_funcname[] = "TERM-main:";
  18.    int retcode,running,rx_tx_char,input_echo;
  19.    unsigned int cur_port;
  20.    struct t_port_info *p_cur_port_info; 
  21.  
  22.    printf("\nTERM - Initializing...\n");
  23.  
  24.    g_uart_irq = 4;
  25.    g_num_ports = 1;
  26.  
  27.    /* Allocate space for an array where we will store 
  28.       information about all the ports */
  29.    gp_port_info = (struct t_port_info *) 
  30.        calloc(g_num_ports,sizeof(struct t_port_info));
  31.    if (gp_port_info == NULL)
  32.    {
  33.        fprintf(stderr,"%s No memory for port info\n",
  34.                a_funcname);
  35.        exit(1);
  36.    }
  37.  
  38.    /* Set parameters for each port */
  39.    gp_port_info[0].base_address = 0x3f8; /* COM1 */
  40.    gp_port_info[0].baud_rate = 2400L;   
  41.    gp_port_info[0].data_bits = 8;
  42.    gp_port_info[0].parity = 0;     /* No Parity */
  43.    gp_port_info[0].stop_bits = 1;
  44.  
  45.    /* Port will use hardware flow control */
  46.    gp_port_info[0].obey_rts_cts = 1;
  47.  
  48.    /* Initialize the UART API */
  49.    retcode = Init_UART();
  50.    if (retcode == -1)
  51.    {
  52.        fprintf(stderr,
  53.            "%s Unable to initialize UART API\n",
  54.            a_funcname);
  55.        exit(1);
  56.    }
  57.  
  58.    /* Display function key menu */
  59.    Usage();
  60.  
  61.    cur_port = 1;
  62.    running = 1;
  63.    input_echo = 0;
  64.    printf("\nPort %u echo %s\n",cur_port,
  65.           (input_echo ? "on" : "off"));
  66.    while (running)
  67.    {
  68.        /* Check for received data */
  69.        rx_tx_char = Read_char(cur_port);
  70.        if (rx_tx_char >= 0)
  71.        {   /* Got something! */
  72.            
  73.            /* Display the received character */
  74.            printf("%c",(rx_tx_char & 0x00ff));
  75.  
  76.            /* Display any error conditions */
  77.            if (rx_tx_char & OVERRUN_ERROR)
  78.                printf("\n\tOVERRUN\t");
  79.            if (rx_tx_char & PARITY_ERROR)
  80.                printf("\n\tPARITY ERROR\t");
  81.            if (rx_tx_char & FRAMING_ERROR)
  82.                printf("\n\tFRAMING_ERROR\t");
  83.            if (rx_tx_char & BREAK_SIGNAL)
  84.                printf("\n\tBREAK\t");
  85.            if (rx_tx_char & RING_INDICATION)
  86.                printf("\n\tRING\t");
  87.            if (rx_tx_char & BUFFER_OVERRUN)
  88.                printf("\n\tBUFFER OVERRUN\t");
  89.  
  90.        }   /* End received something */
  91.  
  92.        /* Check the keyboard */
  93.        if (kbhit())
  94.        {   /* A key has been hit, retrieve it */
  95.            rx_tx_char = getch();
  96.            if (rx_tx_char == 0)
  97.            {   /* Function key was pressed */
  98.  
  99.                /* Read and process the extended scan 
  100.                   code */
  101.                rx_tx_char = getch();
  102.                switch (rx_tx_char)
  103.                {
  104.                    /* F1 - Help */
  105.                    case 59 :
  106.                        Usage();
  107.                        break;
  108.  
  109.                    /* F2 - Toggle port number */
  110.                    case 60 :
  111.                        cur_port++;
  112.                        if (cur_port > g_num_ports)
  113.                            cur_port = 1;
  114.                        printf("\nPort %u\n",cur_port);
  115.                        break;
  116.  
  117.                    /* F3 - View port status */
  118.                    case 61 :
  119.                        printf("\nPort %u echo %s\n",
  120.                          cur_port,
  121.                          (input_echo ? "on" : "off"));
  122.  
  123.                        /* Set pointer to port's info 
  124.                           entry */
  125.                        p_cur_port_info = 
  126.                           &(gp_port_info[cur_port -1]);
  127.  
  128.                        printf("Base Address %4.4Xh\n",
  129.                            p_cur_port_info->
  130.                            base_address);
  131.                        if (p_cur_port_info->
  132.                            max_tx_chars > 1)
  133.                            printf("\tUses FIFO\n\t");
  134.                        else
  135.                            printf("\tNo FIFO\n\t");
  136.  
  137.                        printf("DSR %s DCD %s CTS %s\n",
  138.                          (p_cur_port_info->dsr_state ?
  139.                           "on" : "off"),
  140.                          (p_cur_port_info->dcd_state ?
  141.                           "on" : "off"),
  142.                          (p_cur_port_info->cts_state ?
  143.                           "on" : "off") );
  144.  
  145.                        break;
  146.  
  147.                    /* F4 - Toggle input echo */
  148.                    case 62 :
  149.                        if (input_echo)
  150.                            input_echo = 0;
  151.                        else
  152.                            input_echo = 1;
  153.                        printf("\nEcho %s\n",
  154.                          (input_echo ? "on" : "off"));
  155.                        break;
  156.  
  157.                    /* F10 - Exit */
  158.                    case 68 :
  159.                        running = 0;
  160.                        break;
  161.  
  162.                    default : break;
  163.  
  164.                }   /* End switch rx_tx_char */
  165.                
  166.            }   /* End function key was pressed */
  167.            else
  168.            {   /* Normal key was pressed */
  169.  
  170.                /* If echo is turned on, display the
  171.                   character entered */
  172.                if (input_echo)
  173.                   printf("%c",(rx_tx_char & 0x00ff));
  174.  
  175.                /* Transmit the entered character */
  176.                Send_char(cur_port,(char) (rx_tx_char & 0x00ff) );
  177.  
  178.            }   /* End normal key was pressed */
  179.  
  180.        }   /* End a key has been hit */
  181.  
  182.    }   /* End while running */
  183.  
  184.    printf("\n\nShutting down...\n\n");
  185.  
  186.    /* Exit to DOS after executing Exit_UART (registered
  187.       with atexit by Init_UART) */
  188.    exit(0);
  189. }
  190.  
  191. void Usage(void)
  192. {
  193.    printf("\nTERM: F1=Help (this text)");
  194.    printf("\n      F2=Toggle current port number");
  195.    printf("\n      F3=View port status");
  196.    printf("\n      F4=Toggle input echo");
  197.    printf("\n     F10=Exit\n");
  198.    return;
  199. }
  200.