• 沒有找到結果。

專題部分範例碼互相對照:

COMMTIMEOUTS time_out ; //設定讀寫逾時參數的結構 time_out.ReadIntervalTimeout = MAXDWORD ;

//設為 MAXDWORD:如果沒有資料供讀取,ReadFile 函式將立即返回 time_out.ReadTotalTimeoutMultiplier = 0 ;

time_out.ReadTotalTimeoutConstant = 0 ; //不使用讀取總和時間來判斷是否讀取逾時 time_out.WriteTotalTimeoutMultiplier = 5 ; time_out.WriteTotalTimeoutConstant = 50 ;

//使用寫入總和時間來判斷是否寫入逾時,逾時 WriteFile 函式將立即返回 SetCommTimeouts( com2_handle , &time_out ) ; //設定 com2 讀寫逾時返回 COMMTIMEOUTS 結構說明:

The COMMTIMEOUTS structure is used in the SetCommTimeouts and GetCommTimeouts functions to set and query the time-out parameters for a communications device. The parameters determine the behavior of ReadFile, WriteFile, ReadFileEx, and WriteFileEx operations on the device.

typedef struct _COMMTIMEOUTS {

DWORD ReadIntervalTimeout; (MAXDWORD) DWORD ReadTotalTimeoutMultiplier; (0)

DWORD ReadTotalTimeoutConstant; (0) DWORD WriteTotalTimeoutMultiplier; (5) DWORD WriteTotalTimeoutConstant; (50) } COMMTIMEOUTS,

*LPCOMMTIMEOUTS;

Parameters

ReadIntervalTimeout time_out.ReadIntervalTimeout = MAXDWORD ;

//設為 MAXDWORD:如果沒有資料供讀取,ReadFile 函式將立即返回 Maximum time allowed to elapse between the arrival of two bytes on the communications line, in milliseconds. During a ReadFile operation, the time period begins when the first byte is received. If the interval between the arrival of any two bytes exceeds this amount, the ReadFile operation is completed and any buffered data is returned. A value of zero indicates that interval time-outs are not used.

A value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with the bytes that have already been received, even if no bytes have been received .

ReadTotalTimeoutMultiplier time_out.ReadTotalTimeoutMultiplier = 0 ; Multiplier used to calculate the total time-out period for read operations, in milliseconds. For each read operation, this value is multiplied by the requested number of bytes to be read.

ReadTotalTimeoutConstant time_out.ReadTotalTimeoutConstant = 0 ; Constant used to calculate the total time-out period for read operations, in milliseconds. For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes.

A value of zero for both the ReadTotalTimeoutMultiplier and

ReadTotalTimeoutConstant members indicates that total time-outs are not used for read operations. //不使用讀取總和時間來判斷是否讀取逾時

WriteTotalTimeoutMultiplier time_out.WriteTotalTimeoutMultiplier = 5 ; Multiplier used to calculate the total time-out period for write operations, in milliseconds. For each write operation, this value is multiplied by the number of bytes to be written.

WriteTotalTimeoutConstant time_out.WriteTotalTimeoutConstant = 50 ; //使用寫入總和時間來判斷是否寫入逾時,逾時 WriteFile 函式將立即返回 Constant used to calculate the total time-out period for write operations, in milliseconds. For each write operation, this value is added to the product of the WriteTotalTimeoutMultiplier member and the number of bytes to be written.

A value of zero for both the WriteTotalTimeoutMultiplier and

WriteTotalTimeoutConstant members indicates that total time-outs are not used for write operations .

更詳細的參數說明參考網頁

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/co mmtimeouts_str.asp

A.5 SetCommTimeouts

SetCommTimeouts( com2_handle , &time_out ) ; //設定 com2 讀寫逾時返回

The SetCommTimeouts function sets the time-out parameters for all read and write operations on a specified communications device.

BOOL SetCommTimeouts (

HANDLE hFile,

LPCOMMTIMEOUTS lpCommTimeouts );

Parameters hFile

[in] Handle to the communications device. The CreateFile function returns this handle.

lpCommTimeouts

[in] Pointer to a COMMTIMEOUTS structure that contains the new time-out values.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero.

A.6 ReadFile

ReadFile( com2_handle , buffer , read_bytes ,&read_bytes ,NULL);

//由 com2 讀取 1byte .

//因為 ReadFile 函式逾時的時候,不僅沒讀到資料(所以 buffer[0]沒變動)就返回, 還會把 read_bytes 內含值改為 0,因此 read_bytes!= 0 時表示有讀到資料 BOOL ReadFile

(

HANDLE hFile, LPVOID lpBuffer,

DWORD

nNumberOfBytesToRead,

LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped, );

函式參考網頁:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/readfile .asp

Parameters

hFile 對照範例 com2_handle [in] A handle to the file to be read.

The file handle must be created with the GENERIC_READ access right.

lpBuffe r 對照範例 buffer

[out] A pointer to the buffer that receives the data read from a file(PORT).

nNumberOfBytesToRead 對照範例 read_bytes [in] The number of bytes to be read from a file(PORT).

lpNumberOfBytesRead 對照範例 &read_bytes

[out] A pointer to the variable that receives the number of bytes read.

//因為 ReadFile 函式逾時的時候,不僅沒讀到資料(所以 buffer[0]沒變動)就返回, //還會把 read_bytes 內含值改為 0,因此 read_bytes!= 0 時表示有讀到資料

lpOverlapped 對照範例 NULL [in] A pointer to an OVERLAPPED structure.

This structure is required if hFile is created with FILE_FLAG_OVERLAPPED.

(在 CreateFile 函式中設定為 FILE_ATTRIBUTE_NORMAL)->

所以 lpOverlapped 對照範例 NULL Retrrn Values

The ReadFile function returns when one of the following conditions occur:

A write operation completes on the write end of the pipe.

The number of bytes requested is read.

An error occurs.

If the function succeeds, the return value is nonzero.

If the function fails, the return value is 0 (zero).

A.7 WriteFile lpNumberOfBytesWritten

LPOVERLAPPED lpOverlapped );

Parameters

hFile 對照範例 com2_handle

[in] Handle to the file. The file handle must have been created with the GENERIC_WRITE access right.

lpBuffer 對照範例 &key (key 可看成是寫入 port 的緩衝區變數) [in] Pointer to the buffer containing the data to be written to the file.

nNumberOfBytesToWrite 對照範例 read_bytes

[in] Number of bytes to be written to the file. (1 次寫入 port 的 byte 數)

lpNumberOfBytesWritten, 對照範例 &read_bytes

[out] Pointer to the variable that receives the number of bytes written

相關文件