home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CENVIW9.ZIP / TERMINAL.CMM < prev    next >
Text File  |  1994-03-08  |  3KB  |  79 lines

  1. //***********************************************************************
  2. //*** Terminal - Very plain terminal program.  Simply opens the Port  ***
  3. //*** ver.1      and then sends keyboard to COM port, and COM port to ***
  4. //***            screen, forever.  This program demonstrates the use  ***
  5. //***            of Comm.lib.                                         ***
  6. //***********************************************************************
  7.  
  8. CommPortName = "COM2";  // Set this to your valid port
  9.  
  10. #include <Comm.lib>
  11.  
  12. // Open (try) the port
  13.    printf("Opening port \"%s\"...",CommPortName);
  14.    CommHandle = OpenComm(CommPortName,5000,1000);
  15.  
  16.    if ( CommHandle < 0 ) {
  17.       switch( CommHandle ) {
  18.          case IE_BADID:    error = "Invalid or unsupported ID";      break;
  19.          case IE_BAUDRATE: error = "Unsupported baud rate";          break;
  20.          case IE_BYTESIZE: error = "Invalid byte size";              break;
  21.          case IE_DEFAULT:  error = "Error in default parameters";    break;
  22.          case IE_HARDWARE: error = "Hardware Not Present";           break;
  23.          case IE_MEMORY:   error = "Unable to allocate queues";      break;
  24.          case IE_NOPEN:    error = "Device not open";                break;
  25.          case IE_OPEN:     error = "Device already open";            break;
  26.          default:          error = "Unknown Error";                  break;
  27.       }
  28.       printf("\n\aError %d opening port: %s.\n",CommHandle,error);
  29.       printf("Press any key to exit...");
  30.       getch();
  31.    } else {
  32.  
  33.       printf("\nComm port is open. You are in Terminal mode.\n");
  34.  
  35.       // Set up routine to Close port in case of sudden program exit
  36.       atexit("CloseCommHandle");
  37.  
  38.       // set port to 9600 baud, 8 bytes, 1 stop bit, no parity
  39.       GetCommState(CommHandle,dcb);
  40.       dcb.Parity = NOPARITY;
  41.       dcb.BaudRate = 9600;
  42.       dcb.StopBits = ONESTOPBIT;
  43.       dcb.ByteSize = 8;
  44.       SetCommState(dcb);
  45.  
  46.       for ( ; ; ) {  // forever
  47.  
  48.          // Read whatever characters are in keyboard into a buffer
  49.          if ( kbhit() ) {
  50.             WriteLen = 0;
  51.             do {
  52.                WriteBuf[WriteLen++] = byte(getch());
  53.             } while( kbhit() );
  54.             // write characters from buffer to comm port
  55.             if ( WriteLen != WriteComm(CommHandle,WriteBuf,WriteLen,WriteError)
  56.               || WriteError )
  57.                printf("\n\aWrite Error %04X\n",WriteError);
  58.          }
  59.  
  60.          // If any bytes available, then write to the screen
  61.          if ( (ReadLen = ReadComm(CommHandle,ReadBuf,200,ReadError)) )
  62.             fwrite(ReadBuf,ReadLen,stdout);
  63.          // If there was a read error then report it
  64.          if ( ReadError )
  65.             printf("\n\aRead Error %04X\n",ReadError);
  66.  
  67.       }
  68.  
  69.    }
  70.  
  71.  
  72. // This routine will be called to close port when program terminates
  73.    CloseCommHandle()
  74.    {
  75.       CloseComm(CommHandle);
  76.    }
  77.  
  78.  
  79.