8.串口操作之API篇 PurgeComm ClearCommError

PurgeComm:清空缓冲区.
ClearCommError:返回串口错误和报告.(也可以查看缓冲区状态)

顺便提一下
SetCommBreak:将传输状态挂起,直到调用ClearCommBreak;
ClearCommBreak:恢复由SetCommBreak挂起的通信线路.

1 function PurgeComm(hFile: THandle; dwFlags: DWORD): BOOL; stdcall;   
2 function ClearCommError(hFile: THandle; var lpErrors: DWORD; lpStat: PComStat): BOOL; stdcall;

PurgeComm的dwFlags参数:
PURGE_TXABORT 终止所有正在进行的字符输出操作,完成一个正处于等待状态的重叠i/o操作,他将产生一个事件,指明完成了写操作
PURGE_RXABORT 终止所有正在进行的字符输入操作,完成一个正在进行中的重叠i/o操作,并带有已设置得适当事件
PURGE_TXCLEAR 这个命令指导设备驱动程序清除输出缓冲区,经常与PURGE_TXABORT 命令标志一起使用
PURGE_RXCLEAR 这个命令用于设备驱动程序清除输入缓冲区,经常与PURGE_RXABORT 命令标志一起使用

ClearCommError 的lpErrors参数:
CE_BREAK The hardware detected a break condition. 硬件检测到中断条件.
CE_DNS Windows 95 only: A parallel device is not selected.一个并行设备没被选中.
CE_FRAME The hardware detected a framing error.硬件检测到帧错误.
CE_IOE An I/O error occurred during communications with the device.设备通信发生一个I/O错误.
CE_MODE The requested mode is not supported, or the hFile parameter is invalid. If this value is specified, it is the only valid error.不被支持的模式,或者是无效的句柄.如果这个值被指定,则是唯一的有效错误.(我随便翻译的...)
CE_OOP Windows 95 only: A parallel device signaled that it is out of paper.并行设备发出出纸(缺纸?)信号.
CE_OVERRUN A character-buffer overrun has occurred. The next character is lost.字符缓冲区溢出,下一个字符将丢失.
CE_PTO Windows 95 only: A time-out occurred on a parallel device.并行设备发生超时错误.
CE_RXOVER An input buffer overflow has occurred. There is either no room in the input buffer, or a character was received after the end-of-file (EOF) character.接收缓冲区发生溢出错误.接收缓冲区没有足够的空间,或者有字符出现在了EOF字符后.
CE_RXPARITY The hardware detected a parity error.硬件检测到了奇偶校验错误.
CE_TXFULL The application tried to transmit a character, but the output buffer was full.应用程序尝试发送一个字符,但是发送缓冲区已经满了.

 1 type  
2 TComStateFlag = (fCtlHold, fDsrHold, fRlsHold, fXoffHold, fXOffSent, fEof,
3 fTxim); // not in Windows Headers
4 TComStateFlags = set of TComStateFlag;
5 _COMSTAT = record
6 Flags: TComStateFlags;
7 Reserved: array[0..2] of Byte;//预留
8 cbInQue: DWORD;//接收缓冲区字节数,未被ReadFile读取的字节
9 cbOutQue: DWORD;//发送缓冲区字节数.如果是非重叠I/O,则这个值为0.
10 end;
11 {$EXTERNALSYM _COMSTAT}
12 TComStat = _COMSTAT;
13 COMSTAT = _COMSTAT;
14 {$EXTERNALSYM COMSTAT}
15 PComStat = ^TComStat;

fCtsHold:Specifies whether transmission is waiting for the CTS (clear-to-send) signal to be sent. If this member is TRUE, transmission is waiting.
fDsrHold:Specifies whether transmission is waiting for the DSR (data-set-ready) signal to be sent. If this member is TRUE, transmission is waiting.
fRlsdHold:Specifies whether transmission is waiting for the RLSD (receive-line-signal-detect) signal to be sent. If this member is TRUE, transmission is waiting.
fXoffHold:Specifies whether transmission is waiting because the XOFF character was received. If this member is TRUE, transmission is waiting.
fXoffSent:Specifies whether transmission is waiting because the XOFF character was transmitted. If this member is TRUE, transmission is waiting. Transmission halts when the XOFF character is transmitted to a system that takes the next character as XON, regardless of the actual character.
fEof:Specifies whether the end-of-file (EOF) character has been received. If this member is TRUE, the EOF character has been received.
fTxim:If this member is TRUE, there is a character queued for transmission that has come to the communications device by way of the TransmitCommChar function. The communications device transmits such a character ahead of other characters in the device's output buffer.
fReserved:Reserved; do not use.
cbInQue:Specifies the number of bytes received by the serial provider but not yet read by a ReadFile operation.
cbOutQue:Specifies the number of bytes of user data remaining to be transmitted for all write operations. This value will be zero for a nonoverlapped write.

原文地址:https://www.cnblogs.com/solokey/p/2126601.html