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.
Value | Meaning | |
---|---|---|
PURGE_RXABORT | 0x0002 | Terminates all outstanding overlapped read operations and returns immediately, even if the read operations have not been completed. |
PURGE_RXCLEAR | 0x0008 | Clears the input buffer (if the device driver has one). |
PURGE_TXABORT | 0x0001 | Terminates all outstanding overlapped write operations and returns immediately, even if the write operations have not been completed. |
PURGE_TXCLEAR | 0x0004 | Clears the output buffer (if the device driver has one). |
返回值
成功返回非0
失败,返回0
更多信息 call GetLastError.
Remarks
一个线程如果使用PurgeComm来清空输出缓冲区,被删除的字符,将不会被传输出去。
如果想清空输出缓冲区的同时,确保其中的内容都被传送出去,可以调用FlushFileBuffers 函数。
这个函数一个同步操作的函数。
注意,FlushFileBuffers和流控有关,和写超时无关。
知道所有的pending的写操作都传输完毕,FlushFileBuffers才会返回。