home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / PCL4C50.ZIP / SIMPLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-23  |  2.9 KB  |  124 lines

  1. /*
  2. **  simple.c (11/11/95)
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <malloc.h>
  8. #include <dos.h>
  9. #include <string.h>
  10. #include <conio.h>
  11.  
  12. #include "pcl4c.h"
  13.  
  14. #define FALSE 0
  15. #define TRUE !FALSE
  16.  
  17. /*** Global Variables ***/
  18.  
  19. int Port = COM1;          /* Port COM1 */
  20. int BaudCode;             /* baud rate code ( index into BaudRate[] ) */
  21. char *BaudRate[10] =  {"300","600","1200","2400","4800","9600",
  22.                        "19200","38400","57600","115200"};
  23. char RxBuffer[128+16];
  24. char TxBuffer[128+16];
  25.  
  26. /*** local prototypes */
  27.  
  28. int BaudMatch(char *);
  29. int ErrorCheck(int);
  30.  
  31. /*** main ***/
  32.  
  33. void main(int argc, char *argv[])
  34. {char c;
  35.  int  i, rc;
  36.  char far *Ptr;
  37.  int  Seg;
  38.  if(argc!=3)
  39.    {printf("Usage: SIMPLE <port> <baud>\n");
  40.     exit(1);
  41.    }
  42.  /* get port number from command line */
  43.  Port = atoi(argv[1]) - 1;
  44.  if((Port<COM1) || (Port>COM20))
  45.      {printf("Port must be COM1 to COM20\n");
  46.       exit(1);
  47.      }
  48.  /* get baud rate from command line */
  49.  BaudCode = BaudMatch(argv[2]);
  50.  if(BaudCode<0)
  51.      {printf("Cannot recognize baud rate = %s\n",argv[2]);
  52.       exit(1);
  53.      }
  54.  /* setup 128 byte receive buffer */
  55.  Ptr = (char far *)RxBuffer;
  56.  Seg = FP_SEG(Ptr) + ((FP_OFF(Ptr)+15)>>4);
  57.  SioRxBuf(Port,Seg,Size128);
  58.  /* setup 128 byte transmit buffer */
  59.  Ptr = (char far *)TxBuffer;
  60.  Seg = FP_SEG(Ptr) + ((FP_OFF(Ptr)+15)>>4);
  61.  SioTxBuf(Port,Seg,Size128);
  62.  /* set port parmameters */
  63.  ErrorCheck( SioParms(Port,NoParity,OneStopBit,WordLength8) );
  64.  /* reset the port */
  65.  ErrorCheck( SioReset(Port,BaudCode) );
  66.  /* set DTR and RTS */
  67.  ErrorCheck( SioDTR(Port,'S') );
  68.  ErrorCheck( SioRTS(Port,'S') );
  69.  
  70. #if 0
  71.  /* set RTS/CTS flow control */
  72.  ErrorCheck( SioFlow(Port,18) );
  73.  /* Set FIFO level */
  74.  if(SioFIFO(Port,LEVEL_14)) printf("[16550]\n");
  75. #endif
  76.  
  77.  printf("\nEnter terminal loop ( Type ^Z to exit )\n");
  78.  /* enter terminal loop */
  79.  while(TRUE)
  80.      {if(SioBrkKey())
  81.         {/* restore COM port status & exit */
  82.          printf("BREAK\n");
  83.          SioDone(Port);
  84.          exit(2);
  85.         }
  86.       /* was key pressed ? */
  87.       if(kbhit())
  88.           {i = getch();
  89.            if((char)i==0x1a)
  90.               {/* restore COM port status & exit */
  91.                SioDone(Port);
  92.                exit(1);
  93.               }
  94.            else
  95.               {
  96.                SioPutc(Port,(char)i);
  97.               }
  98.           } /* end if */
  99.       /* any incoming over serial port ? */
  100.       i = SioGetc(Port,0);
  101.       if(i>-1) putch((char)i);
  102.       if(SioBrkSig(Port,'D')) printf("[BREAK detected]");
  103.      } /* end while */
  104. } /* end main */
  105.  
  106. int ErrorCheck(int Code)
  107. {/* trap PCL error codes */
  108.  if(Code<0)
  109.      {printf("ERROR %d:",Code);
  110.       SioError(Code);
  111.       SioDone(Port);
  112.       exit(1);
  113.      }
  114.  return(0);
  115. } /* end ErrorCheck */
  116.  
  117.  
  118. int BaudMatch(char *P)
  119. {int i;
  120.  /* find baud rate in table */
  121.  for(i=0;i<10;i++) if(strcmp(BaudRate[i],P)==0) return(i);
  122.  return(-1);
  123. }
  124.