SerialPort.ReadChar 方法
定义
从 SerialPort 输入缓冲区中同步读取一个字符。Synchronously reads one character from the SerialPort input buffer.
public:
int ReadChar();
public int ReadChar ();
member this.ReadChar : unit -> int
Public Function ReadChar () As Integer
返回
读取的字符。The character that was read.
例外
指定的端口未打开。The specified port is not open.
该操作未在超时时间到期之前完成。The operation did not complete before the time-out period ended.
- 或 --or-
在分配的超时时间内没有可用的字符。No character was available in the allotted time-out period.
注解
此方法基于编码读取一个完整字符。This method reads one complete character based on the encoding.
将和一起使用时,请务必小心 ReadByte ReadChar 。Use caution when using ReadByte and ReadChar together. 在读取字节和读取字符之间切换可能会导致读取额外的数据和/或其他意外行为。Switching between reading bytes and reading characters can cause extra data to be read and/or other unintended behavior. 如果需要在读取文本和从流中读取二进制数据之间进行切换,请选择一个协议,用于仔细定义文本和二进制数据之间的边界,如手动读取字节并对数据进行解码。If it is necessary to switch between reading text and reading binary data from the stream, select a protocol that carefully defines the boundary between text and binary data, such as manually reading bytes and decoding the data.
备注
由于 SerialPort 类会缓冲数据,并且属性中包含的流 BaseStream 不会,这两个可能与可读取的字节数有关。Because the SerialPort class buffers data, and the stream contained in the BaseStream property does not, the two might conflict about how many bytes are available to read. BytesToRead属性可以指示有个字节可供读取,但属性中包含的流可能无法访问这些字节,因为这些字节已经 BaseStream 缓冲到 SerialPort 类中。The BytesToRead property can indicate that there are bytes to read, but these bytes might not be accessible to the stream contained in the BaseStream property because they have been buffered to the SerialPort class.