win32 SetCommTimeouts 和 COMMTIMEOUTS

1. SetCommTimeouts

对通信设备的所有读和写操作设置超时时间

BOOL WINAPI SetCommTimeouts(
  _In_ HANDLE         hFile,
  _In_ LPCOMMTIMEOUTS lpCommTimeouts
);

参数

hFile [in]

A handle to the communications device. The CreateFile function returns this handle.

lpCommTimeouts [in]

A pointer to a COMMTIMEOUTS structure that contains the new time-out values.

返回值

成功 返回非0
失败 返回0,进一步错误信息call GetLastError.


2. COMMTIMEOUTS

包含通信设备的超时参数。
这些参数决定了 设备上ReadFile, WriteFile, ReadFileEx, and WriteFileEx 操作的 行为。

typedef struct _COMMTIMEOUTS {
  DWORD ReadIntervalTimeout;
  DWORD ReadTotalTimeoutMultiplier;
  DWORD ReadTotalTimeoutConstant;
  DWORD WriteTotalTimeoutMultiplier;
  DWORD WriteTotalTimeoutConstant;
} COMMTIMEOUTS, *LPCOMMTIMEOUTS;

成员

ReadIntervalTimeout

在通信线路上,允许的下一个字节到来之前的最大的时间间隔,单位是毫秒。
在任意两个字节之间的间隔,超过这个值,ReadFile 操作,将会返回,所有缓冲的数据,被返回到上层程序。
如果ReadIntervalTimeout = 0 表示不使用超时间隔检测功能。
如果
ReadTotalTimeoutConstant = MAXDWORD,
ReadTotalTimeoutMultiplier = 0
表示读操作会立即返回,并将当前已经接收到的数据返回到上层,如果没有任务数据,也立即返回

ReadTotalTimeoutMultiplier

用来计算总的读操作所用时间 的乘数因子。
对于每个读操作,这个值乘以需要读的字节数。
单位是毫秒

ReadTotalTimeoutConstant

一个常数
用来计算读操作的总的超时时间,单位为毫秒
For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes.
如果
ReadTotalTimeoutMultiplier = 0
ReadTotalTimeoutConstant= 0
表示在读操作中不使用total time-outs

WriteTotalTimeoutMultiplier

The 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

A 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.
如果
WriteTotalTimeoutMultiplier = 0
WriteTotalTimeoutConstant = 0
表示写操作中不使用 total time-outs

Remarks

If an application sets ReadIntervalTimeout and ReadTotalTimeoutMultiplier to MAXDWORD and sets ReadTotalTimeoutConstant to a value greater than zero and less than MAXDWORD, one of the following occurs when the ReadFile function is called:
If there are any bytes in the input buffer, ReadFile returns immediately with the bytes in the buffer.
If there are no bytes in the input buffer, ReadFile waits until a byte arrives and then returns immediately.
If no bytes arrive within the time specified by ReadTotalTimeoutConstant, ReadFile times out.

(不是很清晰)
20180605

猜你喜欢

转载自blog.csdn.net/wowocpp/article/details/80582921