home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / OS2TERM.ZIP / OS2TERM.C next >
Text File  |  1989-08-29  |  4KB  |  138 lines

  1. /*
  2.  *    OS2TERM.C - An interrupt-driven OS/2 terminal emulator
  3.  *
  4.  *    This program uses OS/2's COM0x.SYS serial device
  5.  *    driver to turn a PC into a simple terminal.  Incoming 
  6.  *    data is buffered in OS/2's 1,024-byte serial receive
  7.  *    queue and transferred to the program's data space
  8.  *    using wait-for-something reads.  DTR and RTS are asserted
  9.  *    but no handshaking is performed with the RS-232 control
  10.  *    pins.  Execution is terminated when ESC    is pressed.
  11.  *
  12.  *    Compile and link with:  cl -Lp -G2 -Zp os2term.c
  13.  *
  14.  *    Copyright (c) 1989 Ziff-Davis Publishing Co.
  15.  *    Written May 1989 for PC Magazine by Jeff Prosise
  16.  */
  17.  
  18. #define INCL_DOS
  19. #define INCL_SUB
  20. #define INCL_DOSDEVICES
  21.  
  22. #define SERIAL 0x01
  23. #define SETBAUDRATE 0x41
  24. #define SETLINECTRL 0x42
  25. #define SETDCBINFO 0x53
  26. #define GETDCBINFO 0x73
  27.  
  28. #define STACK_SIZE 2048
  29. #define BPS 9600
  30. #define KBD_HANDLE 0
  31. #define VIO_HANDLE 0
  32.  
  33. #include <os2.h>
  34. #include <stdio.h>
  35.  
  36. struct {
  37.     unsigned char DataBits;
  38.     unsigned char Parity;
  39.     unsigned char StopBits;
  40. } LineCtrl = {     8,        // 8 data bits
  41.         0,        // No parity
  42.         0  };        // 1 stop bit
  43.  
  44. struct {
  45.     unsigned short usWriteTimeout;
  46.     unsigned short usReadTimeout;
  47.     unsigned char bFlags1;
  48.     unsigned char bFlags2;
  49.     unsigned char bFlags3;
  50.     unsigned char bErrorReplacementChar;
  51.     unsigned char bBreakReplacementChar;
  52.     unsigned char bXONChar;
  53.     unsigned char bXOFFChar;
  54. } DCBInfo;
  55.  
  56. unsigned short hCom;        // COM handle
  57. unsigned char InBuffer[256];    // Input buffer
  58.  
  59. /*
  60.  *    The primary thread opens and initializes the COM
  61.  *    driver, starts a thread to monitor the serial port,
  62.  *    displays and outputs characters typed at the keyboard,
  63.  *    and terminates when ESC is pressed.
  64.  */
  65.  
  66. main()
  67. {
  68.     unsigned short usAction;
  69.     unsigned short usBaudRate = BPS;
  70.     unsigned ThreadID;
  71.     static char ComThdStk[STACK_SIZE];
  72.     struct _KBDKEYINFO KbdData;
  73.     unsigned short usBytesWritten;
  74.  
  75.     void far ComThread();
  76.  
  77.     /* Open and initialize COM1 */
  78.  
  79.     if (DosOpen("COM1", &hCom, &usAction, 0L, 0, 1, 0x12, 0L)) {
  80.         printf("COM1 not available or COM0x.SYS not loaded\n");
  81.         exit(1);
  82.     }
  83.  
  84.     /* Set data rate to 9600 bps and line format to N81 */
  85.  
  86.     DosDevIOCtl(0L, &usBaudRate, SETBAUDRATE, SERIAL, hCom);
  87.     DosDevIOCtl(0L, &LineCtrl, SETLINECTRL, SERIAL, hCom);
  88.  
  89.     /* Set Device Control Block parameters */
  90.  
  91.     DosDevIOCtl(&DCBInfo, 0L, GETDCBINFO, SERIAL, hCom);
  92.  
  93.     DCBInfo.usWriteTimeout = 6000;    // 60 second write timeout
  94.     DCBInfo.usReadTimeout = 6000;    // 60 second read timeout
  95.     DCBInfo.bFlags1 = 0x01;        // Enable DTR
  96.     DCBInfo.bFlags2 = 0x40;        // Enable RTS
  97.     DCBInfo.bFlags3 = 0x04;        // Wait-for-something reads
  98.  
  99.     DosDevIOCtl(0L, &DCBInfo, SETDCBINFO, SERIAL, hCom);
  100.  
  101.     /* Create a thread to monitor the serial port */
  102.  
  103.     DosCreateThread(ComThread, &ThreadID, ComThdStk+STACK_SIZE);
  104.  
  105.     /* Monitor the keyboard and output typed characters */
  106.  
  107.     do {
  108.         KbdCharIn(&KbdData, IO_WAIT, KBD_HANDLE);
  109.  
  110.         if ((KbdData.chChar != 0) && (KbdData.chChar != 0x1B)) {
  111.             VioWrtTTy(&KbdData.chChar, 1, VIO_HANDLE);
  112.             DosWrite(hCom, &KbdData.chChar, 1, &usBytesWritten);
  113.         }
  114.  
  115.     } while (KbdData.chChar != 0x1B);
  116.  
  117.     DosExit(EXIT_PROCESS, 0);
  118. }
  119.  
  120. #pragma check_stack (off)    // Disable stack checking
  121.  
  122. /*
  123.  *    This thread monitors COM1 for incoming bytes and writes
  124.  *    any it receives to the display screen.  If an error is
  125.  *    returned by DosRead(), the thread terminates itself.
  126.  */
  127.  
  128. void far ComThread()
  129. {
  130.     unsigned short usBytesRead;
  131.  
  132.     while (!DosRead(hCom, InBuffer, 256, &usBytesRead))
  133.         if (usBytesRead)
  134.             VioWrtTTy(InBuffer, usBytesRead, VIO_HANDLE);
  135.  
  136.     DosExit(EXIT_THREAD, 1);
  137. }
  138.