home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / TERMINAL.CMD < prev    next >
OS/2 REXX Batch file  |  1994-10-04  |  2KB  |  75 lines

  1. EXTPROC CEnvi2
  2. /*******************************************************************
  3.  *** Terminal.cmd - Simple terminal, sample demonstration of the ***
  4.  *** ver.1          Comm.lib serial communications library       ***
  5.  *******************************************************************/
  6.  
  7. CommPortName = "COM2";  // Set this to your valid port
  8.  
  9. #include <FileIO.lib>
  10. #include <DevIOCTL.lib>
  11. #include <Comm.lib>
  12.  
  13. // Open (try) the port
  14.    printf("Opening port \"%s\"...",CommPortName);
  15.    CommHandle = CommOpen(CommPortName,Error);
  16.  
  17.    if ( NULL == CommHandle ) {
  18.       printf("\n\aError %d: Unable to open port \"%s\"\n",Error,CommPortName);
  19.       printf("Press any key to exit...");
  20.       getch();
  21.    } else {
  22.  
  23.       printf("\nComm port is open. You are in Terminal mode.  Press ALT-X to  quit\n");
  24.  
  25.       // Set up routine to Close port in case of sudden program exit
  26.       atexit("CloseCommHandle");
  27.  
  28.       for ( ; ; ) {  // forever
  29.  
  30.          // Read whatever characters are in keyboard into a buffer
  31.          if ( 0 != (key = GetKey()) ) {
  32.             WriteLen = 0;
  33.             do {
  34.                WriteBuf[WriteLen++] = byte(key);
  35.             } while( 0 != (key = GetKey()) );
  36.             // write characters from buffer to comm port
  37.             if ( 0 != CommWrite(CommHandle,WriteBuf,WriteLen,BytesWritten)
  38.               || BytesWritten != WriteLen )
  39.                printf("\n\aWrite Error\n");
  40.          }
  41.  
  42.          // If any bytes available, then write to the screen
  43.          if ( 0 != (ReadError = CommRead(CommHandle,ReadBuf,200,BytesRead)) )
  44.             printf("\n\aRead Error %d\n",ReadError);
  45.          if ( BytesRead )
  46.             fwrite(ReadBuf,BytesRead,stdout);
  47.       }
  48.  
  49.    }
  50.  
  51.  
  52. // Routine to get keyboard keys, and skip weird ones; Exit with ALT_X
  53.    GetKey()
  54.    {
  55.       if ( kbhit() ) {
  56.          c = getch();
  57.          if ( 0 == c ) {
  58.             #define ALTX_CODE 0x2D
  59.             if ( ALTX_CODE == getch() )
  60.                exit(EXIT_SUCCESS);
  61.          }
  62.       } else
  63.          c = 0;
  64.       return c;
  65.    }
  66.  
  67.  
  68. // This routine will be called to close port when program terminates
  69.    CloseCommHandle()
  70.    {
  71.       CommClose(CommHandle);
  72.    }
  73.  
  74.  
  75.