共用方式為


SerialPort.ReadTimeout 屬性

定義

取得或設定讀取作業未完成時,發生逾時之前的毫秒數。

public:
 property int ReadTimeout { int get(); void set(int value); };
public int ReadTimeout { get; set; }
[System.ComponentModel.Browsable(true)]
public int ReadTimeout { get; set; }
member this.ReadTimeout : int with get, set
[<System.ComponentModel.Browsable(true)>]
member this.ReadTimeout : int with get, set
Public Property ReadTimeout As Integer

屬性值

讀取作業未完成時,發生逾時之前的毫秒數。

屬性

例外狀況

連接埠處於無效狀態。

-或-

嘗試設定基礎連接埠的狀態失敗。 例如,從這個 SerialPort 物件傳遞而來的參數無效。

讀取逾時值 (Time-Out Value) 小於零而且不等於 InfiniteTimeout

範例

下列程式代碼範例示範如何使用 SerialPort 類別,讓兩位使用者從以 Null 數據機纜線連接的兩部不同電腦聊天。 在此範例中,系統會在聊天之前提示使用者輸入埠設定和用戶名稱。 此程式代碼範例是針對 類別提供的較大程式碼範例的 SerialPort 一部分。

public:
    static void Main()
    {
        String^ name;
        String^ message;
        StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
        Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));

        // Create a new SerialPort object with default settings.
        _serialPort = gcnew SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort->PortName = SetPortName(_serialPort->PortName);
        _serialPort->BaudRate = SetPortBaudRate(_serialPort->BaudRate);
        _serialPort->Parity = SetPortParity(_serialPort->Parity);
        _serialPort->DataBits = SetPortDataBits(_serialPort->DataBits);
        _serialPort->StopBits = SetPortStopBits(_serialPort->StopBits);
        _serialPort->Handshake = SetPortHandshake(_serialPort->Handshake);

        // Set the read/write timeouts
        _serialPort->ReadTimeout = 500;
        _serialPort->WriteTimeout = 500;

        _serialPort->Open();
        _continue = true;
        readThread->Start();

        Console::Write("Name: ");
        name = Console::ReadLine();

        Console::WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console::ReadLine();

            if (stringComparer->Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort->WriteLine(
                    String::Format("<{0}>: {1}", name, message) );
            }
        }

        readThread->Join();
        _serialPort->Close();
    }

    static void Read()
    {
        while (_continue)
        {
            try
            {
                String^ message = _serialPort->ReadLine();
                Console::WriteLine(message);
            }
            catch (TimeoutException ^) { }
        }
    }
public static void Main()
{
    string name;
    string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    Thread readThread = new Thread(Read);

    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();

    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

    // Set the read/write timeouts
    _serialPort.ReadTimeout = 500;
    _serialPort.WriteTimeout = 500;

    _serialPort.Open();
    _continue = true;
    readThread.Start();

    Console.Write("Name: ");
    name = Console.ReadLine();

    Console.WriteLine("Type QUIT to exit");

    while (_continue)
    {
        message = Console.ReadLine();

        if (stringComparer.Equals("quit", message))
        {
            _continue = false;
        }
        else
        {
            _serialPort.WriteLine(
                String.Format("<{0}>: {1}", name, message));
        }
    }

    readThread.Join();
    _serialPort.Close();
}

public static void Read()
{
    while (_continue)
    {
        try
        {
            string message = _serialPort.ReadLine();
            Console.WriteLine(message);
        }
        catch (TimeoutException) { }
    }
}
Public Shared Sub Main()
    Dim name As String
    Dim message As String
    Dim stringComparer__1 As StringComparer = StringComparer.OrdinalIgnoreCase
    Dim readThread As New Thread(AddressOf Read)

    ' Create a new SerialPort object with default settings.
    _serialPort = New SerialPort()

    ' Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName)
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate)
    _serialPort.Parity = SetPortParity(_serialPort.Parity)
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits)
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits)
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

    ' Set the read/write timeouts
    _serialPort.ReadTimeout = 500
    _serialPort.WriteTimeout = 500

    _serialPort.Open()
    _continue = True
    readThread.Start()

    Console.Write("Name: ")
    name = Console.ReadLine()

    Console.WriteLine("Type QUIT to exit")

    While _continue
        message = Console.ReadLine()

        If stringComparer__1.Equals("quit", message) Then
            _continue = False
        Else
            _serialPort.WriteLine([String].Format("<{0}>: {1}", name, message))
        End If
    End While

    readThread.Join()
    _serialPort.Close()
End Sub

Public Shared Sub Read()
    While _continue
        Try
            Dim message As String = _serialPort.ReadLine()
            Console.WriteLine(message)
        Catch generatedExceptionName As TimeoutException
        End Try
    End While
End Sub

備註

讀取逾時值原本在 Win32 Communications API 中設定為 500 毫秒。 此屬性可讓您設定此值。 逾時可以設定為大於零的任何值,或設定為 InfiniteTimeout,在此情況下不會發生逾時。 InfiniteTimeout 是預設值。

注意

Unmanaged COMMTIMEOUTS 結構的使用者可能會預期將逾時值設定為零,以隱藏逾時。 不過,若要使用 ReadTimeout 屬性隱藏逾時,您必須指定 InfiniteTimeout

這個屬性不會影響 BeginRead 屬性所 BaseStream 傳回之數據流的方法。

適用於