Redigera

Dela via


SerialPort.Read Method

Definition

Reads from the SerialPort input buffer.

Overloads

Read(Byte[], Int32, Int32)

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)

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)

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

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

Parameters

buffer
Byte[]

The byte array to write the input to.

offset
Int32

The offset in buffer at which to write the bytes.

count
Int32

The maximum number of bytes to read. Fewer bytes are read if count is greater than the number of bytes in the input buffer.

Returns

The number of bytes read.

Exceptions

The buffer passed is null.

The specified port is not open.

The offset or count parameters are outside a valid region of the buffer being passed. Either offset or count is less than zero.

offset plus count is greater than the length of the buffer.

No bytes were available to read.

Remarks

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.

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. 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.

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.

Applies to

Read(Char[], Int32, Int32)

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

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

Parameters

buffer
Char[]

The character array to write the input to.

offset
Int32

The offset in buffer at which to write the characters.

count
Int32

The maximum number of characters to read. Fewer characters are read if count is greater than the number of characters in the input buffer.

Returns

The number of characters read.

Exceptions

offset plus count is greater than the length of the buffer.

-or-

count is 1 and there is a surrogate character in the buffer.

The buffer passed is null.

The offset or count parameters are outside a valid region of the buffer being passed. Either offset or count is less than zero.

The specified port is not open.

No characters were available to read.

Remarks

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.

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. 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.

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.

Applies to