HOWTO: Using COM Port Communication with FoxPro

ID: Q87808


The information in this article applies to:


SUMMARY

FoxPro supports communicating through COM1 and COM2 using the following low-level file I/O functions:


   FCHSIZE()
   FCLOSE()
   FGETS()
   FOPEN()
   FPUTS()
   FREAD()
   FWRITE() 


MORE INFORMATION

NOTE: On some peer-to-peer networks, a path must be prepended to the file name for the COM port. Communications port I/O is supported only through the MS-DOS file I/O functions. This means that the communications parameters for the port must be specified at the MS-DOS level using the MODE command before FoxPro is run. The following MODE command demonstrates how to specify 9600 baud, no parity, 8 data bits, and 1 stop bit for the COM1 communications port:


   MODE COM1: 9600,N,8,1 
For more information on the MODE command, see your MS-DOS user's guide.

To test a communications port at the MS-DOS level, issue the following command at the MS-DOS prompt of the host computer. Substitute the port number for <x> in the command below:

   COPY COM<x> CON 
This command echoes the information sent to the port to the screen.

If the screen does not show the correct information, communications at the MS-DOS level are not occurring correctly. Correct communications at the MS-DOS level are required before communications can occur in FoxPro.

Once the communications parameters have been properly specified, the FoxPro low-level I/O functions provide access to the port. The code sample below reads a message terminated by a CTRL+Z character:

   FIL = FOPEN("COM1",12)       && Open COM1 port.
   IF (FIL < 0)
      ? "Error opening COM1"
      RETURN
   ENDIF
   done = .F.
   st = ""
   DO WHILE !DONE
      CH = FREAD(FIL,1)        && Read one character from COM1
      IF (CH != CHR(26))       &&  while not CTRL+Z.
         ST = ST + CH          && Add character to string.
      ELSE
         DONE = .T.
      ENDIF
   ENDDO

   ? "Result string is: "
   ?? ST

   =FCLOSE(FIL)                && Close port. 
To test whether communications are working properly, do the following:

  1. Find a telephone number to test. Add a "9" if necessary to get an outside line. To add a pause after the 9, add a comma (","). If the number is long distance, add a "1". For example, you could use 9,12065551212 (the number for information).


  2. Type the following commands at the MS-DOS prompt, where <x> is the port number, and press ENTER at the end of each line:
    
          COPY CON COMx
          ATDT telephone_number_to_test
          CTRL+Z 


If the communication is successful, you will receive the message "1 file copied" and then be returned to an MS-DOS prompt. Then, you will hear the modem dialing.

NOTE: FoxPro does not provide a method to determine whether a character is waiting in the COM port buffer.

Additional query words:


Keywords          : FoxDos 
Version           : 2.0 2.5 2.5a
Platform          : MS-DOS 
Issue type        : 

Last Reviewed: August 9, 1999