如何使用 .NET 检查 SerialPort 奇偶校验错误

How check SerialPort parity errors with .NET

.NET SerialPort class 有 Parity 属性。但是这个 class 不包含检测奇偶校验错误的方法。有什么方法可以检查奇偶校验错误并进行处理吗?或者这种检查和修复是在硬件级别进行的,开发人员不应该为此担心?

在规范中,通过在SerialErrorReceivedEventHandler, SerialPort.ErrorReceived Event中注册Delegate可以接收和处理

发生的错误通知给SerialErrorReceivedEventArgs.EventType Property

值由SerialError Enum表示。

Fields

Frame    8  The hardware detected a framing error.  
Overrun  2  A character-buffer overrun has occurred. The next character is lost.  
RXOver   1  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.  
RXParity 4  The hardware detected a parity error.  
TXFull 256  The application tried to transmit a character, but the output buffer was full.  

但是,请注意,您可能不会总是在严格的时间收到通知。
仅仅因为它写在规范中并不意味着它真的按照您的预期实现了。