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