win32 SetupComm 函数 PurgeComm 函数

1. SetupComm

初始化通信设备的通信参数

BOOL WINAPI SetupComm(
  _In_ HANDLE hFile,
  _In_ DWORD  dwInQueue,
  _In_ DWORD  dwOutQueue
);

参数

hFile [in]

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

dwInQueue [in]

The recommended size of the device’s internal input buffer, in bytes.

dwOutQueue [in]

The recommended size of the device’s internal output buffer, in bytes.

Return value

成功返回非0,
失败返回0,具体看GetLastError

Remarks

在进程使用CreateFile函数打开通信设备之后,但是还没有执行任何I/O操作,它可以调用SetupComm来设置设备的通信参数。如果不设置,设备使用默认的参数。
dwInQueue 和dwOutQueue 参数 为设备驱动的内部缓冲区,指定推荐的大小。
比如,YMODEM协议包很少会大于1024个字节。
所以,对于YMODEM通信,一个推荐的缓冲区大小是1200字节。
对于基于以太网的通信,推荐的大小的为1600字节,这种情况很少会大于一个Ethernet 帧的大小。

The device driver receives the recommended buffer sizes, but is free to use any input and output (I/O) buffering scheme, as long as it provides reasonable performance and data is not lost due to overrun (except under extreme circumstances). For example, the function can succeed even though the driver does not allocate a buffer, as long as some other portion of the system provides equivalent functionality.


2. PurgeComm

丢弃输出和输入缓冲区的所有字符。
可以中止在设备上的读或者写pending.
It can also terminate pending read or write operations on the resource.

参数

hFile [in]

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

dwFlags [in]

This parameter can be one or more of the following values.

ValueMeaning
PURGE_RXABORT0x0002Terminates all outstanding overlapped read operations and returns immediately, even if the read operations have not been completed.
PURGE_RXCLEAR0x0008Clears the input buffer (if the device driver has one).
PURGE_TXABORT0x0001Terminates all outstanding overlapped write operations and returns immediately, even if the write operations have not been completed.
PURGE_TXCLEAR0x0004Clears the output buffer (if the device driver has one).

返回值

成功返回非0
失败,返回0
更多信息 call GetLastError.

Remarks

一个线程如果使用PurgeComm来清空输出缓冲区,被删除的字符,将不会被传输出去。
如果想清空输出缓冲区的同时,确保其中的内容都被传送出去,可以调用FlushFileBuffers 函数。
这个函数一个同步操作的函数。
注意,FlushFileBuffers和流控有关,和写超时无关。
知道所有的pending的写操作都传输完毕,FlushFileBuffers才会返回。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值