home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_09 / 1009050a < prev    next >
Text File  |  1992-04-03  |  3KB  |  132 lines

  1. /***                  LISTING 8               ***/
  2. /***                                          ***/
  3. /***                   term.c                 ***/
  4. /*** **************************************** ***/
  5. /***              TERMINAL PROGRAM            ***/
  6. /*** **************************************** ***/
  7.  
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "serial.h"
  11.  
  12. #define ESC  0x1B
  13. #define CR   0x0D
  14. #define LF   0x0A
  15.  
  16. #define ASCII  0x007F
  17. #define BINARY 0x00FF
  18.  
  19. void main(int argc, char *argv[])
  20. {
  21.  int comport;
  22.  long baud;
  23.  int parity;
  24.  int databits;
  25.  int stopbits;
  26.  int done = 0;
  27.  int Err  = 0;
  28.  int flag = 0;
  29.  int Char_Value;
  30.  
  31.  /* If NO parameters are passed list the syntax */
  32.  
  33.  if(argc == 1)
  34.   {
  35.    printf("\nSyntax  TERM [comport] [baud] [parity]"
  36.       " [databits] [stopbits] [P or I]\n\n");
  37.  
  38.    printf("      [comport]    = 1 - 4\n");
  39.    printf("      [baud]       = 1 - 115200\n");
  40.    printf("      [parity]     = N - None\n");
  41.    printf("                   = E - Even\n");
  42.    printf("                   = O - Odd\n");
  43.    printf("      [databits]   = 5 - 8\n");
  44.    printf("      [stopbits]   = 1 - 2\n");
  45.    printf("      [P or I]     = P - Polled\n");
  46.    printf("                   = I - Interrupt\n");
  47.   }
  48.  
  49.  /*           Else RUN terminal program         */
  50.  
  51.  else
  52.   {
  53.    /*          Set parameter variables          */
  54.  
  55.    comport  = atoi(argv[1]) - 1;
  56.    baud     = atol(argv[2]);
  57.    databits = atoi(argv[4]);
  58.    stopbits = atoi(argv[5]);
  59.  
  60.  
  61.    if(strcmpi(argv[3],"N") == 0)
  62.        parity = NO_PARITY;
  63.  
  64.    else if(strcmpi(argv[3],"E") == 0)
  65.        parity = EVEN_PARITY;
  66.  
  67.    else if(strcmpi(argv[3],"O") == 0)
  68.        parity = ODD_PARITY;
  69.  
  70.     /*** Flag for Interrupt I/O ***/
  71.  
  72.    if(strcmpi(argv[6],"I") == 0)
  73.        flag = 1;
  74.  
  75.    Err = SetSerial(comport,baud,parity,databits,stopbits);
  76.  
  77.    if(!Err)
  78.      {
  79.       printf("Terminal established ..."
  80.                "  ESC to exit... \n");
  81.       Assert(DTR);
  82.  
  83.       if(flag)
  84.      InitSerial(comport); /*** Interrupt I/O ***/
  85.  
  86.       do
  87.        {
  88.     if(kbhit())
  89.      {
  90.       Char_Value = getch();
  91.  
  92.       if(Char_Value == ESC)
  93.        {
  94.         if(flag)         /*** Interrupt I/O ***/
  95.           CloseSerial(comport);
  96.         Assert_Off(DTR);
  97.         done = 1;
  98.        }
  99.  
  100.       else
  101.        {
  102.         SerialOut(Char_Value);
  103.         putch(Char_Value);
  104.  
  105.         if(Char_Value == CR)
  106.           {
  107.            SerialOut(LF);
  108.            putch(LF);
  109.           }
  110.        }
  111.     }
  112.  
  113.       if(flag)   /*** Interrupt I/O ***/
  114.     {
  115.     if((Char_Value = IntSerialIn()) != -1)
  116.       putch(Char_Value & ASCII);
  117.     }
  118.  
  119.        else      /***   Polled I/O   ***/
  120.     {
  121.      if((Char_Value = SerialIn()) != -1)
  122.         putch(Char_Value & ASCII);
  123.     }
  124.       }
  125.      while(!done);
  126.     }
  127.  
  128.    else
  129.     printf("Error setting up port\n");
  130.   }
  131. }
  132.