# ComToFile

int ComToFile (int COMPort, int fileHandle, int count, int terminationByte);

## Purpose

Reads from the input queue of a specified COM port and writes the data to the file specified by fileHandle. ComToFile reads bytes from the input queue until it satisfies count, encounters terminationByte, a timeout occurs, or an error occurs. This function returns the number of bytes successfully written to the file.

ComToFile times out whenever the input queue remains empty for an entire timeout period. On a timeout, ComToFile returns the number of bytes actually written to the COM port and sets rs232err to -99. You can set the timeout period by calling SetComTime.

ComToFile returns an error code if you have not opened the port, if you pass an invalid parameter value, or if a file write error occurs.

| Note When executing a less than 8 bits transfer, CVI does not guarantee the value of the invalid high bits. For a 7 bits transfer, the value of the 8th bit is undefined. 7 bit value "0x60" can be passed back as "0x60" or "0xE0"(0x60|0x80). This depends on device driver. Most drivers set the value to 0.

## Parameters

Input
Name | Type | Description
COMPort | integer | A number that indicates the COM port on which to operate. This number is paired with deviceName, which represents the COM port, during the OpenComConfig function call. Default Value: 1—COM1 Valid Range: 1—1,000
fileHandle | integer | The file handle to which the data is written.
count | integer | The number of bytes to read from the selected port. If count is 0, ComToFile ignores it and terminates on terminationByte or on an error.
terminationByte | integer | The byte value used to terminate the read.
| Note The termination byte is never written to the file.
If terminationByte equals a carriage return and the character immediately following the carriage return is a linefeed, then both the carriage return and linefeed are discarded. If terminationByte equals a linefeed and the character immediately following the linefeed is a carriage return, then both the linefeed and the carriage return are discarded. If terminationByte is valid, the function stops when it encounters a byte that has the value of terminationByte; ComToFile removes the termination byte from the input queue and does not write it to the file.

## Return Value

Name | Type | Description
nbytes | integer | The actual number of bytes written to the file.
| Note This count does not include the termination byte.
If a timeout occurs, the function returns the number of bytes written. If an error occurs, the return value is a negative error code.