home *** CD-ROM | disk | FTP | other *** search
/ PC Media 4 / PC MEDIA CD04.iso / share / prog / pcl4c42 / simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-21  |  2.2 KB  |  87 lines

  1. /*
  2. **                ---  simple.c ---
  3. **
  4. **  EXAMPLE CODE: This example is meant to be the simpliest
  5. **  possible terminal emulator. Whatever is typed is sent out
  6. **  over the selected serial port, and whatever is received from
  7. **  the serial port is displayed on the screen.
  8. **
  9. **  This example program (not the PCL4C library) is donated to
  10. **  the Public Domain by MarshallSoft Computing, Inc. It is
  11. **  provided as an example of the use of the PCL4C.
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <dos.h>
  19. #include "allocseg.h"
  20. #include "pcl4c.h"
  21.  
  22. #define FALSE 0
  23. #define TRUE !FALSE
  24. #define ESC 0x1b
  25.  
  26.  
  27. /*** Global Variables ***/
  28.  
  29. int Port = COM1;          /* Port COM1 */
  30. int BaudCode = Baud9600;  /* Code for 9600 baud  */
  31. int ErrorCheck(int Code);
  32.  
  33. /*** main ***/
  34.  
  35. void main(int argc, char *argv[])
  36. {
  37.  char c;
  38.  int i, rc;
  39.  char *Ptr;
  40.  if(argc!=2)
  41.    {printf("Usage: SIMPLE port\n");
  42.     exit(1);
  43.    }
  44.  /* get port number from command line */
  45.  Port = atoi(argv[1]) - 1;
  46.  if((Port<0) || (Port>3))
  47.      {printf("Port must be COM1 to COM4\n");
  48.       exit(1);
  49.      }
  50.  /* setup receive buffer */
  51.  ErrorCheck( SioRxBuf(Port,AllocSeg(128),Size128) );
  52.  /* set port parmameters */
  53.  ErrorCheck( SioParms(Port,NoParity,OneStopBit,WordLength8) );
  54.  /* reset the port */
  55.  ErrorCheck( SioReset(Port,BaudCode) );
  56.  printf("\nCOM%d @ 9600 Baud\n",1+Port);
  57.  /* set DTR and RTS */
  58.  ErrorCheck( SioDTR(Port,'S') );
  59.  ErrorCheck( SioRTS(Port,'S') );
  60.  printf("Enter terminal loop ( Type ESC to exit )\n");
  61.  /* enter terminal loop */
  62.  while(TRUE)
  63.      {/* was key pressed ? */
  64.       if(SioKeyPress())
  65.           {i = SioKeyRead();
  66.            if((char)i==ESC)
  67.               {/* restore COM port status & exit */
  68.                SioDone(Port);
  69.                exit(1);
  70.               }
  71.            else SioPutc(Port,(char)i);
  72.           } /* end if */
  73.       /* any incoming over serial port ? */
  74.       i = SioGetc(Port,0);
  75.       if(i>-1) SioCrtWrite((char)i);
  76.      } /* end while */
  77. } /* end main */
  78.  
  79. int ErrorCheck(int Code)
  80. {/* trap PCL error codes */
  81.  if(Code<0)
  82.      {SioError(Code);
  83.       SioDone(Port);
  84.       exit(1);
  85.      }
  86.  return(0);
  87. } /* end ErrorCheck */