SerialPort.Read 方法
定义
从 SerialPort 输入缓冲区中读取。Reads from the SerialPort input buffer.
重载
| Read(Byte[], Int32, Int32) |
从 SerialPort 输入缓冲区读取一些字节并将那些字节写入字节数组中指定的偏移量处。Reads a number of bytes from the SerialPort input buffer and writes those bytes into a byte array at the specified offset. |
| Read(Char[], Int32, Int32) |
从 SerialPort 输入缓冲区中读取一些字符,然后将这些字符写入字符数组中指定的偏移量处。Reads a number of characters from the SerialPort input buffer and writes them into an array of characters at a given offset. |
Read(Byte[], Int32, Int32)
从 SerialPort 输入缓冲区读取一些字节并将那些字节写入字节数组中指定的偏移量处。Reads a number of bytes from the SerialPort input buffer and writes those bytes into a byte array at the specified offset.
public:
int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public int Read (byte[] buffer, int offset, int count);
member this.Read : byte[] * int * int -> int
Public Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer
参数
- buffer
- Byte[]
将输入写入到其中的字节数组。The byte array to write the input to.
- offset
- Int32
要写入字节的 buffer 中的偏移量。The offset in buffer at which to write the bytes.
- count
- Int32
最多读取的字节数。The maximum number of bytes to read. 如果 count 大于输入缓冲区中的字节数,则读取较少的字节。Fewer bytes are read if count is greater than the number of bytes in the input buffer.
返回
读取的字节数。The number of bytes read.
例外
传递的 buffer 为 null。The buffer passed is null.
指定的端口未打开。The specified port is not open.
offset 或 count 参数超出了所传递的 buffer 的有效区域。The offset or count parameters are outside a valid region of the buffer being passed. offset 或 count 小于零。Either offset or count is less than zero.
offset 加上 count 大于 buffer 的长度。offset plus count is greater than the length of the buffer.
没有可以读取的字节。No bytes were available to read.
注解
如果需要在读取文本和从流中读取二进制数据之间进行切换,请选择一个协议,用于仔细定义文本和二进制数据之间的边界,如手动读取字节并对数据进行解码。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.
Read读取的字节数等于 count 但串行端口上仍有未读取的可用字节时,此方法不会阻止其他操作。The Read method does not block other operations when the number of bytes read equals count but there are still unread bytes available on the serial port.
适用于
Read(Char[], Int32, Int32)
从 SerialPort 输入缓冲区中读取一些字符,然后将这些字符写入字符数组中指定的偏移量处。Reads a number of characters from the SerialPort input buffer and writes them into an array of characters at a given offset.
public:
int Read(cli::array <char> ^ buffer, int offset, int count);
public int Read (char[] buffer, int offset, int count);
member this.Read : char[] * int * int -> int
Public Function Read (buffer As Char(), offset As Integer, count As Integer) As Integer
参数
- buffer
- Char[]
将输入写入到其中的字符数组。The character array to write the input to.
- offset
- Int32
要写入字符的 buffer 中的偏移量。The offset in buffer at which to write the characters.
- count
- Int32
最多读取的字符数。The maximum number of characters to read. 如果 count 大于输入缓冲区中的字符数,则读取较少的字符。Fewer characters are read if count is greater than the number of characters in the input buffer.
返回
读取的字符数。The number of characters read.
例外
offset 加上 count 大于缓冲区的长度。offset plus count is greater than the length of the buffer.
- 或 --or-
count 为 1 并且缓冲区中有一个代理项字符。count is 1 and there is a surrogate character in the buffer.
传递的 buffer 为 null。The buffer passed is null.
offset 或 count 参数超出了所传递的 buffer 的有效区域。The offset or count parameters are outside a valid region of the buffer being passed. offset 或 count 小于零。Either offset or count is less than zero.
指定的端口未打开。The specified port is not open.
没有可以读取的字符。No characters were available to read.
注解
使用此方法从串行端口读取字符。Use this method for reading characters from the serial port.
如果需要在读取文本和从流中读取二进制数据之间进行切换,请选择一个协议,用于仔细定义文本和二进制数据之间的边界,如手动读取字节并对数据进行解码。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 characters are available to read. BytesToRead属性可以指示存在要读取的字符,但在属性中包含的流可能无法访问这些字符,因为这些字符已经 BaseStream 缓冲到 SerialPort 类中。The BytesToRead property can indicate that there are characters to read, but these characters might not be accessible to the stream contained in the BaseStream property because they have been buffered to the SerialPort class.
Read读取的字节数等于 count 但串行端口上仍有未读取的可用字节时,此方法不会阻止其他操作。The Read method does not block other operations when the number of bytes read equals count but there are still unread bytes available on the serial port.