home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MAXCOMM.LZH / term.c < prev   
C/C++ Source or Header  |  1992-05-29  |  2KB  |  77 lines

  1.  
  2. /* A simple terminal program,  using comm.dll.
  3.  *
  4.  * This program avoids polling by using a second thread.
  5.  *
  6.  * MSC 5/6:  cl /Ox /W3 /Lp /G2 term.c maxcomm.lib
  7.  */
  8.  
  9. #define INCL_NOPM
  10. #define INCL_DOS
  11. #define INCL_VIO
  12. #define INCL_DOSDEVIOCTL
  13. #include <os2.h>
  14. #include <conio.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "comm.h"
  19.  
  20. TID _loadds cdecl far BeginThread( void (cdecl _loadds far *func)(void *param),
  21.                    int StackSize,
  22.                    void far *param);
  23.  
  24. static int alive = 1;
  25.  
  26. #pragma check_stack(off)
  27.  
  28. void far cdecl _loadds readThread(void far *p)
  29. {
  30.     HCOMM hc = *(HCOMM far *)p;
  31.     USHORT bytes, bw;
  32.     BYTE buf[1000];
  33.     static char msg[] = "Error\r\n";
  34.  
  35.     while(alive){
  36.         if(!ComRxWait(hc, -1L)){         // wait for something.
  37.             ComRead(hc, buf, sizeof(buf), &bytes);
  38.             DosWrite(1, buf, bytes, &bw);
  39.         }
  40.         else{
  41.             VioWrtTTY(msg, strlen(msg), 0);
  42.             if(!ComIsOnline(hc))
  43.                 DosExit(EXIT_PROCESS, 0);
  44.         }
  45.     }
  46. }
  47.  
  48. void cdecl main(int argc, char **argv)
  49. {
  50.     HCOMM hcR;
  51.     USHORT rc;
  52.     int done = FALSE;
  53.     int c;
  54.  
  55.     rc = ComOpen(argv[1],  &hcR, 0, 0);
  56.     if(rc){
  57.         printf("SYS%04u: Couldn't open %s\n", rc, argv[1]);
  58.         exit(0);
  59.     }
  60.     ComSetBaudRate(hcR, 2400L, 'n', 8, 1);
  61.     BeginThread(readThread, 2048, &hcR);
  62.     if(argc>2)      /* if the user supplies a 2nd argument (any arg),  turn on
  63.                      * the carrier check. */
  64.         ComWatchDog(hcR, TRUE, 5);
  65.     while(!done){
  66.         c = getch();
  67.         if(c == 0xe0)
  68.             c = 0;
  69.         ComPutc(hcR, c);
  70.     if(c==0)    /* quit if a function key is pressed */
  71.         done = TRUE;
  72.     }
  73.     ComTxWait(hcR, -1L);
  74.     ComClose(hcR);
  75. }
  76.  
  77.