Microsoft KB Archive/105011

From BetaArchive Wiki

How to Use WriteComm() and CloseComm() with FOXTOOLS.FLL

ID: Q105011

The information in this article applies to:

  • Microsoft FoxPro for Windows, versions 2.5x, 2.6x


WriteComm() is a function that can be called using FOXTOOLS.FLL in order to write to the COM or LPT ports. CloseComm() is another function that can be called using FOXTOOLS.FLL in order to close the COM or LPT ports. Below are complete descriptions of WriteComm() and CloseComm().


For information about using the serial port using Visual FoxPro, please see the following article in the Microsoft Knowledge Base:

Q139526 How to Send to the Serial Port by Using Mscomm32.ocx



The WriteComm() function writes to the specified communications device.

Function Syntax

WriteComm(<comm_id>, <str>, <str_len>)

   Argument       Description

   <comm_id>      Specifies the device to receive the bytes. The
                  OpenComm() function returns this value.

   <str>          Points to the buffer that contains the bytes to be
                  written. This is the variable s in the example

   <str_len>      Specifies the number of bytes to be written. 


The return value specifies the number of bytes written, if the function is successful. The return value is less than zero if an error occurs, making the absolute value of the return value the number of bytes written.


For serial ports, the WriteComm() function deletes data in the transmission queue if there is not enough room in the queue for the additional bytes. Applications should use the OpenComm() function to set the size of the transmission queue to an amount no smaller than the size of the largest expected output string.



The CloseComm() function closes the specified communications device and frees any memory allocated for the device's transmission and receiving queues. All characters in the output queue are sent before the communications device is closed.

Function Syntax


   Argument       Description

   <comm_id>      Specifies the device to be closed. The OpenComm()
                  function returns this value. In the example below
                  this is the variable com1. 


The return value is zero if the function is successful. Otherwise, it is less than zero.

Code Example

The following code example uses the WriteComm() and CloseComm() functions and FOXTOOLS.FLL to write to and close COM1. (This example can be found in FOXPROW\GOODIES\FOXTOOLS\DIALER.PRG.)

   *-------------------- dialer.prg ------------------------
   * Sample program to output to comm port
   * Uses FoxTools library for generic DLL access

   SET LIBRARY TO SYS(2004)+"foxtools.fll" ADDITIVE

   opencomm = REGFN("OpenComm", "CII", "I")
   writecomm = REGFN("WriteComm", "ICI", "I")
   closecomm = REGFN("CloseComm", "I", "I")

   com1 = CALLFN(opencomm, "COM1:", 100, 100)
   s = "ATDT 5551212" + chr(13)
   =CALLFN(writecomm, com1, s, len(s))

   WAIT WINDOW "Press any key to hang up"
   s = "ATH0" + chr(13)
   =CALLFN(writecomm, com1, s, len(s))

   =CALLFN(closecomm, com1)

   RELEASE LIBRARY SYS(2004)+"foxtools.fll" 


The above information can also be found in the Microsoft Windows Software Development Kit (SDK) "Programmer's Reference Volume 2: Functions" (describes the functions) and the "Programmer's Reference Volume 3: Messages, Structures, and Macros" (describes the returned values). The online Help file for the Professional Version of Visual C++ also contains this information.

Additional query words: FoxWin 2.50 2.50a 2.50b 2.60 2.60a fll dll

Keywords          : kbcode FxprgFoxtools 
Version           : 2.5x 2.6x
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: August 9, 1999
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.