home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / tk3 / comtalk / comport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  4.6 KB  |  162 lines

  1. /*
  2.    comport.c -- This file contains the sources for COM port manipulation.
  3.    Created by Microsoft Corporation, 1989
  4. */
  5. #define  INCL_DOSFILEMGR
  6. #define     INCL_DOSDEVICES
  7. #define     INCL_DOSDEVIOCTL
  8. #include <os2.h>
  9. #include "global.h"
  10. #include "comport.h"
  11. /*
  12.    Constants
  13. */
  14. #define    XON    0x11    /* Ctrl Q */
  15. #define    XOFF    0x13    /* Ctrl S */
  16. char    CRLF[2] = { 0x0d, 0x0a };
  17.  
  18. /*
  19.     Variables
  20. */
  21. DCBINFO        dcbinfo;    /* Device control block for Ioctl 53H, 73H */
  22. HFILE        hPort;
  23. LINECONTROL    lnctlBuf;
  24. int        rc;
  25. USHORT        usErrWord;
  26.  
  27. int ComFlush(void) {
  28. /*
  29.     Flush the COM port with Category 11 functions
  30. */
  31.     BYTE Data, Zero = 0;
  32.  
  33.     /* Call Category 11 Functions 1H, 2H  Flush Input, Output Buffers */
  34.     if (rc = DosDevIOCtl(&Data, &Zero, 0x01, 11, hPort)) return rc;
  35.     if (rc = DosDevIOCtl(&Data, &Zero, 0x02, 11, hPort)) return rc;
  36.     return 0;
  37. }
  38.  
  39. int ComInit(COM comTerm) {
  40. /*
  41.     Open the COM port according to the specifications
  42. */
  43.     USHORT action;
  44.  
  45.     /* Get File Handle for COM port (shared read/write access) */
  46.     if (rc = DosOpen(comTerm.szPort,&hPort, &action, 0L, 0, 0x0001, 0x0042, 0L))
  47.     return rc;
  48.  
  49.     /* Call Category 1 Function 41H   Set Baud Rate */
  50.     if (rc = DosDevIOCtl(NULL, &comTerm.usBaud, 0x41, 1, hPort)) return rc;
  51.  
  52.     /* Call Category 1 Function 42H   Set Line Characteristics */
  53.     lnctlBuf.bDataBits    = comTerm.bData;
  54.     lnctlBuf.bParity    = comTerm.bParity;
  55.     lnctlBuf.bStopBits    = comTerm.bStop - 20;    /* IDD_ONESTOP = 20 */
  56.     if (rc = DosDevIOCtl(NULL, &lnctlBuf, 0x42, 1, hPort)) return rc;
  57.  
  58.     /* Call Category 1 Function 73H   Query Device Control Block */
  59.     if (rc = DosDevIOCtl(&dcbinfo, 0L, 0x73, 1, hPort)) return rc;
  60.  
  61.     /*
  62.     Do we want software handshaking?
  63.     */
  64.     dcbinfo.fbFlowReplace    &= ~(0x03);    /* Clear bits 0 and 1 */
  65.     dcbinfo.fbFlowReplace    |=
  66.     (comTerm.fSoftware)    ? (MODE_AUTO_TRANSMIT | MODE_AUTO_RECEIVE) : 0;
  67.     /*
  68.     Do we want hardware handshaking?
  69.     */
  70.     /* Turn on DTR, if appropriate */
  71.     dcbinfo.fbCtlHndShake    &= ~(0x03);    /* Clear bits 0 and 1 */
  72.     dcbinfo.fbCtlHndShake    |= ((comTerm.fHardware) ? MODE_DTR_CONTROL : 0);
  73.  
  74.     /* Turn on RTS, if appropriate */
  75.     dcbinfo.fbFlowReplace    &= ~(0xc0);    /* Clear bits 6 and 7 */
  76.     dcbinfo.fbFlowReplace    |= ((comTerm.fHardware) ? MODE_RTS_CONTROL : 0);
  77.  
  78.     /* Adjust CTS output handshaking */
  79.     dcbinfo.fbCtlHndShake    &= ~MODE_CTS_HANDSHAKE;     /* Clear bit 3 */
  80.     dcbinfo.fbCtlHndShake    |= ((comTerm.fHardware)?MODE_CTS_HANDSHAKE:0);
  81.  
  82.     /* Adjust DSR output handshaking */
  83.     dcbinfo.fbCtlHndShake    &= ~MODE_DSR_HANDSHAKE;     /* Clear bit 4 */
  84.     dcbinfo.fbCtlHndShake    |= ((comTerm.fHardware)?MODE_DSR_HANDSHAKE:0);
  85.  
  86.     /* Turn off DCD output handshaking */
  87.     dcbinfo.fbCtlHndShake    &= ~MODE_DCD_HANDSHAKE;     /* Clear bit 5 */
  88.  
  89.     /* Adjust DSR input sensitivity */
  90.     dcbinfo.fbCtlHndShake    &= ~MODE_DSR_SENSITIVITY;   /* Clear bit 6 */
  91.     dcbinfo.fbCtlHndShake    |= ((comTerm.fHardware)?MODE_DSR_SENSITIVITY:0);
  92.     /*
  93.     Set the line to Wait for Character, Read mode
  94.     */
  95.     dcbinfo.fbTimeout        &= ~(0x06);    /* Clear bits, then set */
  96.     dcbinfo.fbTimeout        |= MODE_WAIT_READ_TIMEOUT;
  97.     dcbinfo.usReadTimeout    = -1;        /* Never! */
  98.  
  99.     /* Call Category 1 Function 53H   Set Device Control Block */
  100.     if (rc = DosDevIOCtl(0L, &dcbinfo, 0x53, 1, hPort)) return rc;
  101.  
  102.     /* Get ready to start */
  103.     return ComFlush();
  104. }
  105.  
  106. USHORT ComRead(Line pli) {
  107. /*
  108.     Reads all characters present
  109.     Returns:    0 if successful
  110.         nonzero (Dos Error or Com Error Word) if unsuccessful
  111. */
  112.     /* Read from the port... And snatch as many as you can! (blocking read) */
  113.     if (rc = DosRead(hPort, pli->szText, MAXLINELEN, &(pli->cch))) return rc;
  114.  
  115.     /* Check the COM Error Word */
  116.     if (rc = DosDevIOCtl(&usErrWord, NULL, 0x6d, 1, hPort)) return rc;
  117.  
  118.     /* ...then return it */
  119.     return usErrWord;
  120. }
  121.  
  122. int ComWrite(char ch) {
  123. /*
  124.     Write a character at a time
  125.  
  126.     Okay as long as you don't type too fast
  127. */
  128.     USHORT nCharsWritten;
  129.  
  130.     return DosWrite(hPort, &ch, 1, &nCharsWritten);
  131. }
  132.  
  133. int ComClose(void) {
  134. /*
  135.     Close the COM port
  136. */
  137.     if (rc = ComFlush()) return rc;
  138.     return DosClose(hPort);
  139.  
  140. int ComBreak(void) {
  141. /*
  142.     Set BREAK mode ON
  143. */
  144.     USHORT ComErr;
  145.  
  146.     /* Call Category 1 Function 4BH -- Set Break On */
  147.     return DosDevIOCtl(&ComErr, NULL, 0x4b, 1, hPort);
  148. }
  149.  
  150. int ComUnbreak(void) {
  151. /*
  152.     Set BREAK mode OFF
  153. */
  154.     USHORT ComErr;
  155.  
  156.     /* Call Category 1 Function 45H -- Set Break Off */
  157.     return DosDevIOCtl(&ComErr, NULL, 0x45, 1, hPort);
  158. }
  159.  
  160. int ComError(void) { return (int) usErrWord; }
  161.