home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource1 / cenvew / terminal.cmm < prev    next >
Encoding:
Text File  |  1993-11-14  |  2.7 KB  |  76 lines

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