home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MONTE.ZIP / TERM / TERM.C < prev   
Text File  |  1993-03-05  |  3KB  |  93 lines

  1. /* term.c
  2.  
  3. Term.c is a simple, full-duplex asynchronous terminal program.
  4.  
  5. Thread 1 reads characters from the keyboard and writes them to the com port.
  6. This thread will block most of the time in the keyboard read.
  7.  
  8. Thread 2 reads characters from the com port and writes them to the screen.
  9. This thread will block most of the time in the read of the com port.
  10.  
  11. IBM C Set/2 does not support getch() for getting a single key from the
  12. keyboard, so I used KbdCharIn().  KdbCharIn() is 16-bit, so the compiler
  13. does a thunk at compile time.
  14.  
  15.  
  16. */
  17.  
  18.  
  19. #define INCL_KBD
  20. #define INCL_DOS
  21. #include <os2.h>
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <assert.h>
  26.  
  27.  
  28.  
  29. //----------------------------------------------------------------------------------
  30. // thread 2 code:
  31. // read com port and write it to standard out (predefined file handle 1)
  32.  
  33. #define LEN_BUFFER   1
  34.  
  35. void _Optlink thread2main ( void * pv  )
  36. {
  37.   APIRET     rc;
  38.   CHAR       achBuffer[ LEN_BUFFER + 1 ];
  39.   HFILE      hcom;
  40.   ULONG      ulByteCount;
  41.  
  42.   // com port handle passed as parameter to thread 2
  43.   hcom = *((PHFILE)pv);
  44.  
  45.   // read the com port and write the data to standard output
  46.   while( TRUE ) {
  47.     rc = DosRead( hcom, achBuffer, LEN_BUFFER, &ulByteCount );
  48.     assert( rc == 0 );
  49.  
  50.     achBuffer[ ulByteCount ] = 0;
  51.     rc = DosWrite( 1, achBuffer, ulByteCount, &ulByteCount );
  52.     assert( rc == 0 );
  53.   }
  54. }
  55.  
  56.  
  57.  
  58. //----------------------------------------------------------------------------------
  59. // thread 1: reads characters from the keyboard and writes them to the com port
  60.  
  61. int main( int argc, char *argv[] )
  62. {
  63.   APIRET         rc;
  64.   APIRET16       rc16;
  65.   HFILE          hcom;
  66.   KBDKEYINFO     keyinfo;
  67.   ULONG          ulByteCount, ulAction, idThread;
  68.  
  69.   // com port name is argument to program
  70.   if( argc != 2 ) {
  71.     printf( "give com port name on command line\n" );
  72.     return 1;
  73.   }
  74.  
  75.   // open com port for exclusive read/write access
  76.   rc = DosOpen( argv[ 1 ], &hcom, &ulAction, 0, 0, FILE_OPEN,
  77.     OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE | OPEN_FLAGS_FAIL_ON_ERROR, NULL );
  78.   printf( "DosOpen rc %d\n", rc );
  79.   assert( rc == 0 );
  80.  
  81.   // start thread that reads the com port; pass a pointer to the open comport handle
  82.   idThread = _beginthread( thread2main, NULL, 2*4096, (void *)&hcom  );
  83.   assert( idThread );
  84.  
  85.   // read a keyboard key and write it to the com port; no echo (full duplex)
  86.   while( TRUE ) {
  87.     rc16 = KbdCharIn( &keyinfo, IO_WAIT, 0 );
  88.     assert( rc16 == 0 );
  89.     rc = DosWrite( hcom, &keyinfo.chChar, 1, &ulByteCount );
  90.     assert( rc == 0 );
  91.   }
  92. }
  93.