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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值