home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / COMTALK.ZIP / COMPORT.C < prev    next >
C/C++ Source or Header  |  1989-02-08  |  5KB  |  161 lines

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