home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MAXCOMM.LZH / comm.h next >
C/C++ Source or Header  |  1992-05-29  |  12KB  |  353 lines

  1. /*
  2.  *  Header file for COMM.DLL,  a high performance multithreading
  3.  *  library for OS/2 serial port i/o.
  4.  *
  5.  *  Copyright (C) 1990, 1991 A:WARE Inc.  All rights reserved.
  6.  *
  7.  * Comm.h, MaxComm.lib, and MaxComm.dll may be used and distributed freely for
  8.  * non-profit use, provided that they are distributed together and
  9.  * are not modified in any way. This means that any program that uses
  10.  * them has to be free, unless prior written persmission from A:WARE
  11.  * Inc. states otherwise.
  12.  *
  13.  * Inclusion with a for-profit/commercial program requires that you
  14.  * purchase the source code for MaxComm.dll.  No royalties.
  15.  * The cost is US$199.
  16.  *
  17.  * Contact A:WARE Inc:
  18.  *
  19.  * 6056 Cayeswood Ct, Ste 100
  20.  * Mississauga, Ontario
  21.  * Canada
  22.  * L5V 1B1
  23.  *
  24.  * (416)858-3222.
  25.  *
  26.  * Author: Peter Fitzsimmons, March 1990.
  27.  */
  28.  
  29. /*
  30.  * include os2.h or os2def.h before this file.
  31.  *
  32.  */
  33. #ifndef APIENTRY
  34. #include <os2def.h>
  35. #include <bsedos.h>
  36. #endif
  37.  
  38. typedef SHANDLE  HCOMM;
  39. #define COMMAPI  pascal far
  40.  
  41.  
  42. /*
  43.  * open comm device.  return 0 if successful,  OS/2 error if not.
  44.  *
  45.  * This function (or ComHRegister()) must be called before any
  46.  * other Com() function.
  47.  *
  48.  * Do not use this function if the port handle has been inherited from
  49.  * another process (use ComHRegister() instead).
  50.  *
  51.  * The comm handle (placed in pHcomm) is not a file handle,  and should
  52.  * not be used by Dos() functions.  If the file handle to the port is
  53.  * needed (to pass to a child process),  use the ComGetFH() function.
  54.  *
  55.  * If RxBufSize and/or TxBufSize are/is zero,  a default size is used.
  56.  *
  57.  
  58.   When using COM0x.SYS,  the default buffer size is the same as the
  59.   buffer size used by COM0x.SYS.  For Tx, this is the optimum size
  60.   to keep COM0x.SYS busy.  A buffer larger than this will not gain
  61.   any extra performance.  Therefore, the common urge to make the
  62.   transmit buffer large will not have the desired effect.  If there
  63.   was some way to make the REAL transmit buffer (the one in
  64.   COM0x.SYS) larger,  performance gains WOULD be seen,  but there is
  65.   no way to do this unless you have the source code for COM0x.SYS.
  66.   If you DO have the source (in the DDK), and you increase the
  67.   buffer sizes, use 0 for the XxBufSize and ComOpen will use your
  68.   larger buffers (ComOpen/Register uses the standard IOCTL call to
  69.   query the buffer sizes).
  70.  
  71.   RxBufSize, on the otherhand, does not affect performance of any
  72.   functions in the Com() module.  To avoid overflows however,  you
  73.   should set this to at least the size of the COM0x.SYS Rx buffer,
  74.   or use 0 so the default size if selected.  When the Rx buffer gets
  75.   full,  the speaker will sound (DosBeep(200, 10)).  There are two
  76.   solutions to this problem:  Make the Rx buffer larger (preferred),
  77.   or raise the priority of the thread calling ComRead()/ComGetc().
  78.  
  79.   All threads created by comm.dll run at the default priority. Since
  80.   com0?.sys is interrupt driven,  Reading/Writing to it at a high
  81.   priorty does not improve anything.  If the Rx thread is starved
  82.   for such a long time that com0?.sys buffer gets full,  it means
  83.   some other process on your computer is badly behaved (IT is
  84.   problably running at Time Critical class when it shouldn't be).
  85.   There are four ways to solve this (in order of preference):
  86.  
  87.   1) Insert DosSleep(1L) in the polling loops that are guilty (this
  88.   assumes you have source code), and/or have it run at NORMAL
  89.   priority class.
  90.  
  91.   2) decrease MAXWAIT in config.sys.
  92.  
  93.   3) Prior to calling ComOpen/ComHRegister, raise the priorty of the
  94.   current thread (or of the whole process).  The Rx/Tx threads will
  95.   inherhit this priority.
  96.  
  97.   4) set PRIORITY=ABSOLUTE in config.sys.
  98.  
  99.  
  100.   FYI:  OS/2 1.2 uses a default size of 1024 bytes for Rx, and 128
  101.         bytes for Tx.
  102.  
  103.  *
  104.  *
  105.  */
  106. USHORT COMMAPI ComOpen(PSZ PortName,
  107.                        HCOMM FAR *pHcomm,  /* pointer to var for Comm handle */
  108.                        USHORT RxBufSize,   /* desired size of Receive queue*/
  109.                        USHORT TxBufSize);  /* desired size of Transmit queue */
  110.  
  111. /*
  112.  * ComHRegister():  Register a comm handle opened by another process.
  113.  *
  114.  * This function (or ComOpen()) must be called before any
  115.  * other Com() function.
  116.  */
  117. USHORT COMMAPI _loadds ComHRegister(HFILE hf,
  118.                        HCOMM FAR *pHcomm,  /* pointer to var for Comm handle */
  119.                        USHORT RxBufSize,   /* desired size of receive queue*/
  120.                        USHORT TxBufSize);  /* desired size of xmit queue */
  121.  
  122.  
  123. /* ComClose():  Close and/or DeRegister comm handle.
  124.  *
  125.  * This function will not DosClose() the handle if the handle was not
  126.  * opened by this process with ComOpen() (ie: If ComHRegister() was used,
  127.  * ComClose() will not DosClose() the handle.  ComClose() must still be
  128.  * called though).
  129.  */
  130. USHORT COMMAPI ComClose(HCOMM hcomm);
  131.  
  132.  
  133. /*
  134.  * return TRUE if the device (comx, named pipe, etc) is considered
  135.  * in an "Online" state.
  136.  */
  137. USHORT COMMAPI ComIsOnline(HCOMM hcomm);
  138.  
  139. /*
  140.  * ComWrite(): Place many bytes in output queue.
  141.  *
  142.  * This function does not return until all of the bytes have been
  143.  * placed in the output queue,  or an error occurs.
  144.  *
  145.  * If the device becomes offline while this function is waiting for
  146.  * queue space, and the watchdog feature is enabled (see
  147.  * ComWatchDog()),  this function returns before completion.
  148.  *
  149.  */
  150. USHORT COMMAPI ComWrite(
  151.      HCOMM hc,                /* file handle             */
  152.      PVOID pvBuf,            /* pointer to buffer         */
  153.      USHORT cbBuf);                     /* number of bytes to write      */
  154.  
  155.  
  156. /*
  157.  * ComRead(): Read many bytes from input queue.
  158.  *
  159.  * This function will copy bytes from the input queue to pvBuf,
  160.  * until the input queue is empty, or 'cbBuf' bytes have been copied.
  161.  *
  162.  *
  163.  */
  164. USHORT COMMAPI ComRead(
  165.      HCOMM hc,                /* file handle               */
  166.      PVOID pvBuf,            /* pointer to buffer           */
  167.      USHORT cbBuf,            /* maximum bytes to read       */
  168.      PUSHORT pcbBytesRead);             /* pointer to variable receiving
  169.                                          * byte count */
  170.  
  171. /*
  172.  * ComGetc() : return next byte in input queue,  or -1 if there is none.
  173.  */
  174. SHORT COMMAPI ComGetc(HCOMM hc);
  175.  
  176. /*
  177.  * ComPeek():  the same as ComGetc(),  but the character is left in the
  178.  * input queue.
  179.  */
  180. SHORT COMMAPI ComPeek(HCOMM hc);
  181.  
  182. /*
  183.  * ComPutc():  Place one byte in output queue.    This function does not
  184.  * return until the byte is placed in the output queue, or an error occurs.
  185.  *
  186.  * If the device becomes offline while this function is waiting for
  187.  * queue space, and the watchdog feature is enabled (see
  188.  * ComWatchDog()),  this function returns before completion.
  189.  */
  190. USHORT COMMAPI ComPutc(HCOMM hc, SHORT c);
  191.  
  192. /*
  193.  * ComRxWait():  Wait for something to be placed into the input queue.
  194.  *
  195.  * If lTimeOut is -1L,    wait forever.  Otherwise, lTimeOut is the
  196.  * number of milliseconds to wait (or 0) before returning a timeout
  197.  * error (ERROR_SEM_TIMEOUT).
  198.  *
  199.  * If the device becomes offline while this function is waiting, and
  200.  * the watchdog feature is enabled (see ComWatchDog()),  this
  201.  * function returns before completion.
  202.  *
  203.  */
  204. USHORT COMMAPI ComRxWait(HCOMM hc, LONG lTimeOut);
  205.  
  206. /*
  207.  * ComTxWait():  Wait for output queue to empty.
  208.  *
  209.  * If lTimeOut is -1L,    wait forever.  Otherwise, lTimeOut is the
  210.  * number of milliseconds to wait (or 0) before returning a timeout
  211.  * error (ERROR_SEM_TIMEOUT).
  212.  *
  213.  * If the device becomes offline while this function is waiting, and
  214.  * the watchdog feature is enabled (see ComWatchDog()),  this
  215.  * function returns before completion.
  216.  */
  217. USHORT COMMAPI ComTxWait(HCOMM hc, LONG lTimeOut);
  218.  
  219.  
  220. /*
  221.  * return number of bytes in input queue.
  222.  *
  223.  */
  224. USHORT COMMAPI ComInCount(HCOMM hc);
  225.  
  226. /*
  227.  * return number of bytes in output queue.
  228.  *
  229.  */
  230. USHORT COMMAPI ComOutCount(HCOMM hc);
  231.  
  232. /*
  233.  * return space in output queue.
  234.  *
  235.  */
  236. USHORT COMMAPI ComOutSpace(HCOMM hc);
  237.  
  238.  
  239. #define COMM_PURGE_RX 1
  240. #define COMM_PURGE_TX 2
  241. #define COMM_PURGE_ALL    (COMM_PURGE_RX | COMM_PURGE_TX)
  242. /*
  243.  * ComPurge():    Purge (discard) i/o queue(s).
  244.  *
  245.  *  Where fsWhich is a combination of :
  246.  *         COMM_PURGE_RX    Purge input queue.
  247.  *
  248.  *         COMM_PURGE_TX    Purge output queue.
  249.  */
  250. USHORT COMMAPI ComPurge(HCOMM hc,  SHORT fsWhich);
  251.  
  252.  
  253. /*
  254.  * ComPause():
  255.  *
  256.  * This function causes the Rx and Tx threads to end, after all
  257.  * Tx activity has ended.  Any bytes still in the Rx queue are
  258.  * purged.  After calling this function,  it is safe for other
  259.  * processes to use the comm port file handle (see ComGetFH()).
  260.  *
  261.  */
  262. USHORT COMMAPI ComPause(HCOMM hc);
  263.  
  264. /*
  265.  * ComResume():
  266.  *
  267.  * This function creates new Rx and Tx threads,  and resets the state
  268.  * of the comm port to what is was when ComPause() was called.
  269.  *
  270.  * It is an error to call this function without calling ComPause().
  271.  *
  272.  */
  273. USHORT COMMAPI ComResume(HCOMM hc);
  274.  
  275. /*
  276.  *
  277.  * ComGetFH():  get the file handle (HFILE) the comm module is
  278.  * using.
  279.  *
  280.  * If the comm routines were initialized with ComHRegister(),  the
  281.  * handle returned by this function will be the same handle passed
  282.  * to ComHRegister().
  283.  *
  284.  * NOTE: Programs must not use this handle with any file system
  285.  * calls (DosRead, DosWrite, DosClose, etc).  It may be used with
  286.  * DosDevIOCTL() to query device dependant info.
  287.  *
  288.  * The intended use for this function is so that one process can
  289.  * pass the file handle to another process.  Don't forget to
  290.  * call ComPause() before spawning another process that uses the comm
  291.  * port.
  292.  */
  293. HFILE COMMAPI ComGetFH(HCOMM hc);
  294.  
  295. /*
  296.  * ComWatchDog():  Enable/disable the "online" watchdog.
  297.  *
  298.  * When the watchdog is enabled,  and the state of the comm handle
  299.  * is no longer "online" (in the case of a modem,  the carrier has dropped),
  300.  * any Com functions that are blocked (ComRxWait, ComTxWait) become
  301.  * unblocked,  within the period specified by the "Seconds" paramater.
  302.  *
  303.  * It is up to the application to notice that the device is no longer
  304.  * online,  after the blocked Com function returns.
  305.  *
  306.  * Obviously,  one must remember to disable the watchdog feature between
  307.  * phone calls.
  308.  *
  309.  * When a device is first initialized by the comm moduel (via ComOpen() or
  310.  * ComHRegister()) the watchdog feature is disabled.
  311.  *
  312.  * The "Seconds" paramater is ignored unless "OnOff" is TRUE.
  313.  *
  314.  * If "OnOff" is TRUE,  and "Seconds" is 0,  the current watchdog period
  315.  * is used.  When first initialized,  the watchdog period is 3 seconds.
  316.  */
  317. USHORT COMMAPI ComWatchDog(HCOMM hc, BOOL OnOff, USHORT Seconds);
  318.  
  319. /*******************************************************************
  320.  *                                   *
  321.  * Thus far, all functions listed are applicable to many       *
  322.  * device types,  including named pipes.  The 3 functions          *
  323.  * that follow, however, are specific to COM0x.SYS (rs232c ports). *
  324.  *                                   *
  325.  *******************************************************************/
  326.  
  327. /*
  328.  * Get/Set DBC.  See os/2 docs on "DCBINFO", used in a
  329.  * DosIOCTL(ASYNC_GETDCBINFO) call (ioctl category 1, function 73h)
  330.  *
  331.  *
  332.  * NOTE: ComSetDCB() ignores bits 0, 1 and 2 of the "fbTimeout"
  333.  * field of the DCBINFO structure. (if you are using the IBM toolkit,
  334.  * this is called "flags3" of the DCB).  These bits control the type
  335.  * of read timeout processing, and write timeout processing that the
  336.  * com01.sys device driver uses.  The Com() api insists that
  337.  * "write infinite timeout processing" NOT be enabled,  and
  338.  * "Wait-for-something read timeout processing" be in effect, so this
  339.  * function overrides the above mentioned bits to ensure this.
  340.  *
  341.  */
  342. #ifdef IOCTL_ASYNC
  343. USHORT COMMAPI ComGetDCB(HCOMM hc, PDCBINFO pdbc);
  344. USHORT COMMAPI ComSetDCB(HCOMM hc, PDCBINFO pdcb);
  345. #endif
  346.  
  347. USHORT COMMAPI ComSetBaudRate(HCOMM hcomm,
  348.      LONG bps,
  349.      CHAR parity,
  350.      USHORT databits,
  351.      USHORT stopbits);
  352.  
  353.