SerialPort.ReadLine 메서드

정의

입력 버퍼에서 NewLine 값까지 읽습니다.

public:
 System::String ^ ReadLine();
public string ReadLine ();
member this.ReadLine : unit -> string
Public Function ReadLine () As String

반환

String

처음 발견되는 NewLine 값까지의 입력 버퍼 내용입니다.

예외

지정한 포트가 열려 있지 않은 경우

시간 제한이 끝나기 전에 작업이 완료되지 않은 경우

또는

읽은 바이트가 없는 경우

예제

다음 코드 예제에서는 두 사용자가 null 모뎀 케이블에 의해 연결 된 두 개의 별도 컴퓨터에서 채팅할 수 있도록 클래스를 사용 SerialPort 하는 방법을 보여 줍니다. 이 코드 예제는 일부에 대해 제공 된 더 큰 코드 예제는 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

설명

이 메서드는 값을 NewLine 반환 NewLine 하지 않지만 입력 버퍼에서 값이 제거됩니다.

기본적으로 메서드는 ReadLine 줄이 수신될 때까지 차단됩니다. 이 동작이 바람직하지 않은 경우 속성을 0이 아닌 값으로 설정 ReadTimeout 하여 포트에서 선을 사용할 수 없는 경우 메서드가 강제로 ReadLine throw TimeoutException 하도록 합니다.

텍스트 읽기와 스트림에서 이진 데이터 읽기 사이를 전환해야 하는 경우 수동으로 바이트 읽기 및 데이터 디코딩과 같이 텍스트와 이진 데이터 간의 경계를 신중하게 정의하는 프로토콜을 선택합니다.

참고

클래스는 SerialPort 데이터를 버퍼링하고 속성에 BaseStream 포함된 스트림은 데이터를 버퍼링하지 않으므로 두 클래스는 읽을 수 있는 바이트 수에 대해 충돌할 수 있습니다. 이 속성은 BytesToRead 읽을 바이트가 있음을 나타낼 수 있지만 이러한 바이트는 클래스에 버퍼링 SerialPort 되었기 때문에 속성에 포함된 스트림에 BaseStream 액세스할 수 없습니다.

적용 대상